Adding initial version of the Web Service target adapter.

This commit is contained in:
Mark Fisher
2008-03-28 20:33:53 +00:00
parent 51d080a058
commit 2b1a542deb
2 changed files with 175 additions and 0 deletions

View File

@@ -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<Object>(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);
}
}
}
}

View File

@@ -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;
}
}
}