From 2b1a542deb6cf466dea2a391c8e38b3627b61653 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 28 Mar 2008 20:33:53 +0000 Subject: [PATCH] Adding initial version of the Web Service target adapter. --- .../AbstractWebServiceTargetAdapter.java | 100 ++++++++++++++++++ .../SimpleWebServiceTargetAdapter.java | 75 +++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/AbstractWebServiceTargetAdapter.java create mode 100644 spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/SimpleWebServiceTargetAdapter.java diff --git a/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/AbstractWebServiceTargetAdapter.java b/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/AbstractWebServiceTargetAdapter.java new file mode 100644 index 0000000000..36896729c4 --- /dev/null +++ b/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/AbstractWebServiceTargetAdapter.java @@ -0,0 +1,100 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.ws.adapter; + +import java.io.IOException; + +import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.Message; +import org.springframework.util.Assert; +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.WebServiceMessageFactory; +import org.springframework.ws.client.core.FaultMessageResolver; +import org.springframework.ws.client.core.WebServiceMessageCallback; +import org.springframework.ws.client.core.WebServiceTemplate; +import org.springframework.ws.soap.SoapMessage; +import org.springframework.ws.soap.client.core.SoapActionCallback; + +/** + * Base class for Web Service target channel adapters. + * + * @author Mark Fisher + */ +public abstract class AbstractWebServiceTargetAdapter implements MessageHandler { + + public static final String SOAP_ACTION_PROPERTY_KEY = "_ws.soapAction"; + + + private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); + + private volatile WebServiceMessageCallback requestCallback; + + + public AbstractWebServiceTargetAdapter(String uri) { + Assert.hasText(uri, "'uri' must not be empty or null"); + this.webServiceTemplate.setDefaultUri(uri); + } + + + public void setMessageFactory(WebServiceMessageFactory messageFactory) { + this.webServiceTemplate.setMessageFactory(messageFactory); + } + + public void setRequestCallback(WebServiceMessageCallback requestCallback) { + this.requestCallback = requestCallback; + } + + public void setFaultMessageResolver(FaultMessageResolver faultMessageResolver) { + this.webServiceTemplate.setFaultMessageResolver(faultMessageResolver); + } + + protected WebServiceTemplate getWebServiceTemplate() { + return this.webServiceTemplate; + } + + public final Message handle(Message message) { + Object responsePayload = this.doHandle(message.getPayload(), this.getRequestCallback(message)); + return (responsePayload != null ? new GenericMessage(responsePayload, message.getHeader()) : null); + } + + protected abstract Object doHandle(Object requestPayload, WebServiceMessageCallback requestCallback); + + private WebServiceMessageCallback getRequestCallback(Message requestMessage) { + if (this.requestCallback != null) { + return this.requestCallback; + } + String soapAction = requestMessage.getHeader().getProperty(SOAP_ACTION_PROPERTY_KEY); + return (soapAction != null) ? new TypeCheckingSoapActionCallback(soapAction) : null; + } + + + private static class TypeCheckingSoapActionCallback extends SoapActionCallback { + + TypeCheckingSoapActionCallback(String soapAction) { + super(soapAction); + } + + @Override + public void doWithMessage(WebServiceMessage message) throws IOException { + if (message instanceof SoapMessage) { + super.doWithMessage(message); + } + } + } + +} diff --git a/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/SimpleWebServiceTargetAdapter.java b/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/SimpleWebServiceTargetAdapter.java new file mode 100644 index 0000000000..cdd8ee17d5 --- /dev/null +++ b/spring-integration-ws/src/main/java/org/springframework/integration/ws/adapter/SimpleWebServiceTargetAdapter.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.ws.adapter; + +import java.io.IOException; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; + +import org.springframework.integration.message.MessageHandlingException; +import org.springframework.ws.client.core.SourceExtractor; +import org.springframework.ws.client.core.WebServiceMessageCallback; +import org.springframework.xml.transform.StringResult; +import org.springframework.xml.transform.StringSource; + +/** + * A target channel adapter for calling out to a Web Service. + * + * @author Mark Fisher + */ +public class SimpleWebServiceTargetAdapter extends AbstractWebServiceTargetAdapter { + + private final SourceExtractor sourceExtractor; + + + public SimpleWebServiceTargetAdapter(String uri) { + this(uri, null); + } + + public SimpleWebServiceTargetAdapter(String uri, SourceExtractor sourceExtractor) { + super(uri); + this.sourceExtractor = (sourceExtractor != null) ? sourceExtractor : new DefaultSourceExtractor(); + } + + + @Override + protected Object doHandle(Object requestPayload, WebServiceMessageCallback requestCallback) { + if (requestPayload instanceof Source) { + return this.getWebServiceTemplate().sendSourceAndReceive( + (Source) requestPayload, requestCallback, this.sourceExtractor); + } + if (requestPayload instanceof String) { + StringResult result = new StringResult(); + this.getWebServiceTemplate().sendSourceAndReceiveToResult( + new StringSource((String) requestPayload), requestCallback, result); + return result.toString(); + } + throw new MessageHandlingException("Unsupported payload type '" + requestPayload.getClass() + + "'. " + this.getClass().getName() + " only supports 'java.lang.String' and '" + Source.class.getName() + + "'. Consider using the '" + MarshallingWebServiceTargetAdapter.class.getName() + "' instead."); + } + + + private static class DefaultSourceExtractor implements SourceExtractor { + + public Object extractData(Source source) throws IOException, TransformerException { + return source; + } + } + +}