Class WebServiceTemplate
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean,WebServiceOperations
WebServiceMessage instances.
Code using this class need only implement callback interfaces, provide Source objects to read data from, or
use the pluggable Marshaller support. For invoking the marshalling methods,
the marshaller and unmarshaller properties
must be set.
This template uses a SoapFaultMessageResolver to handle fault response messages. Another
FaultMessageResolver can be defined with with faultMessageResolver property. If this property is set to null, no fault resolving is performed.
This template uses the following algorithm for sending and receiving.
- Call
createConnection(). - Call
createWebServiceMessage()on the registered message factory to create a request message. - Invoke
doWithMessage()on the request callback, if any. This step stores content in the request message, based onSource, marshalling, etc. - Invoke
handleRequest()on the registeredinterceptors. Interceptors are executed in order. If any of the interceptors creates a response message in the message context, skip to step 7. - Call
send()on the connection. - Call
hasError()to check if the connection has an error. For an HTTP transport, a status code other than2xxindicates an error. However, since a status code of 500 can also indicate a SOAP fault, the template verifies whether the error is not a fault. - If the connection has an error, call the
handleError()method, which by default throws aWebServiceTransportException. - If the connection has no error, continue with the next step.
- Invoke
receiveon the connection to read the response message, if any. - If no response was received, return
nullorfalse - Call
hasFault()to determine whether the response has a fault. If it has, callClientInterceptor.handleFault(MessageContext)and thehandleFault()method. - Otherwise, invoke
ClientInterceptor.handleResponse(MessageContext)andextractData()on the response extractor, ordoWithMessageon the response callback. - Call to
closeon the connection.
- Since:
- 1.0.0
- Author:
- Arjen Poutsma
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringLog category to use for message tracing.protected static final org.apache.commons.logging.LogAdditional logger to use for received message tracing.protected static final org.apache.commons.logging.LogAdditional logger to use for sent message tracing.Fields inherited from class org.springframework.xml.transform.TransformerObjectSupport
logger -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newWebServiceTemplateusing default settings.WebServiceTemplate(org.springframework.oxm.Marshaller marshaller) Creates a newWebServiceTemplatewith the given marshaller.WebServiceTemplate(org.springframework.oxm.Marshaller marshaller, org.springframework.oxm.Unmarshaller unmarshaller) Creates a newMarshallingMethodEndpointAdapterwith the given marshaller and unmarshaller.WebServiceTemplate(WebServiceMessageFactory messageFactory) Creates a newWebServiceTemplatebased on the given message factory. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> TdoSendAndReceive(MessageContext messageContext, WebServiceConnection connection, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) Sends and receives aMessageContext.Returns the default URI to be used on operations that do not have a URI parameter.Returns the destination provider used on operations that do not have a URI parameter.Returns the fault message resolver for this template.Returns the client interceptors to apply to all web service invocations made by this template.org.springframework.oxm.MarshallerReturns the marshaller for this template.org.springframework.oxm.UnmarshallerReturns the unmarshaller for this template.protected ObjecthandleError(WebServiceConnection connection, WebServiceMessage request) Handles an error on the given connection.protected ObjecthandleFault(WebServiceConnection connection, MessageContext messageContext) Handles an fault in the given response message.protected booleanhasError(WebServiceConnection connection, WebServiceMessage request) Determines whether the given connection or message context has an error.protected booleanhasFault(WebServiceConnection connection, WebServiceMessage response) Determines whether the given connection or message has a fault.protected voidInitialize the default implementations for the template's strategies:SoapFaultMessageResolver,SaajSoapMessageFactory, andHttpUrlConnectionMessageSender.marshalSendAndReceive(Object requestPayload) Sends a web service message that contains the given payload, marshalled by the configuredMarshaller.marshalSendAndReceive(Object requestPayload, WebServiceMessageCallback requestCallback) Sends a web service message that contains the given payload, marshalled by the configuredMarshaller.marshalSendAndReceive(String uri, Object requestPayload) Sends a web service message that contains the given payload, marshalled by the configuredMarshaller.marshalSendAndReceive(String uri, Object requestPayload, WebServiceMessageCallback requestCallback) Sends a web service message that contains the given payload, marshalled by the configuredMarshaller.booleansendAndReceive(String uri, WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback) Sends a web service message that can be manipulated with the given request callback, handling the response with a response callback.<T> TsendAndReceive(String uriString, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) Sends a web service message that can be manipulated with the given callback, reading the result with aWebServiceMessageExtractor.booleansendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback) Sends a web service message that can be manipulated with the given request callback, handling the response with a response callback.<T> TsendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) Sends a web service message that can be manipulated with the given callback, reading the result with aWebServiceMessageExtractor.<T> TsendSourceAndReceive(String uri, Source requestPayload, SourceExtractor<T> responseExtractor) Sends a web service message that contains the given payload, reading the result with aSourceExtractor.<T> TsendSourceAndReceive(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor) Sends a web service message that contains the given payload, reading the result with aSourceExtractor.<T> TsendSourceAndReceive(Source requestPayload, SourceExtractor<T> responseExtractor) Sends a web service message that contains the given payload, reading the result with aSourceExtractor.<T> TsendSourceAndReceive(Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor) Sends a web service message that contains the given payload, reading the result with aSourceExtractor.booleansendSourceAndReceiveToResult(String uri, Source requestPayload, Result responseResult) Sends a web service message that contains the given payload.booleansendSourceAndReceiveToResult(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult) Sends a web service message that contains the given payload.booleansendSourceAndReceiveToResult(Source requestPayload, Result responseResult) Sends a web service message that contains the given payload.booleansendSourceAndReceiveToResult(Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult) Sends a web service message that contains the given payload.voidsetCheckConnectionForError(boolean checkConnectionForError) Indicates whether the connection should be checked for error indicators (true), or whether these should be ignored (false).voidsetCheckConnectionForFault(boolean checkConnectionForFault) Indicates whether the connection should be checked for fault indicators (true), or whether we should rely on themessageonly (false).voidsetDefaultUri(String uri) Set the default URI to be used on operations that do not have a URI parameter.voidsetDestinationProvider(DestinationProvider destinationProvider) Set the destination provider URI to be used on operations that do not have a URI parameter.voidsetFaultMessageResolver(FaultMessageResolver faultMessageResolver) Sets the fault resolver for this template.final voidsetInterceptors(ClientInterceptor[] interceptors) Sets the client interceptors to apply to all web service invocations made by this template.voidsetMarshaller(org.springframework.oxm.Marshaller marshaller) Sets the marshaller for this template.voidsetUnmarshaller(org.springframework.oxm.Unmarshaller unmarshaller) Sets the unmarshaller for this template.Methods inherited from class org.springframework.ws.client.support.WebServiceAccessor
afterPropertiesSet, createConnection, getMessageFactory, getMessageSenders, setMessageFactory, setMessageSender, setMessageSendersMethods inherited from class org.springframework.xml.transform.TransformerObjectSupport
createTransformer, getTransformerFactory, newTransformerFactory, setTransformerFactoryClass, transform
-
Field Details
-
MESSAGE_TRACING_LOG_CATEGORY
Log category to use for message tracing.- See Also:
-
sentMessageTracingLogger
protected static final org.apache.commons.logging.Log sentMessageTracingLoggerAdditional logger to use for sent message tracing. -
receivedMessageTracingLogger
protected static final org.apache.commons.logging.Log receivedMessageTracingLoggerAdditional logger to use for received message tracing.
-
-
Constructor Details
-
WebServiceTemplate
public WebServiceTemplate()Creates a newWebServiceTemplateusing default settings. -
WebServiceTemplate
Creates a newWebServiceTemplatebased on the given message factory.- Parameters:
messageFactory- the message factory to use
-
WebServiceTemplate
public WebServiceTemplate(org.springframework.oxm.Marshaller marshaller) Creates a newWebServiceTemplatewith the given marshaller. If the givenMarshalleralso implements theUnmarshallerinterface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.Note that all
Marshallerimplementations in Spring also implement theUnmarshallerinterface, so that you can safely use this constructor.- Parameters:
marshaller- object used as marshaller and unmarshaller- Throws:
IllegalArgumentException- whenmarshallerdoes not implement theUnmarshallerinterface- Since:
- 2.0.3
-
WebServiceTemplate
public WebServiceTemplate(org.springframework.oxm.Marshaller marshaller, org.springframework.oxm.Unmarshaller unmarshaller) Creates a newMarshallingMethodEndpointAdapterwith the given marshaller and unmarshaller.- Parameters:
marshaller- the marshaller to useunmarshaller- the unmarshaller to use- Since:
- 2.0.3
-
-
Method Details
-
getDefaultUri
Returns the default URI to be used on operations that do not have a URI parameter. -
setDefaultUri
Set the default URI to be used on operations that do not have a URI parameter.Typically, either this property is set, or
setDestinationProvider(DestinationProvider), but not both.- See Also:
-
marshalSendAndReceive(Object)marshalSendAndReceive(Object,WebServiceMessageCallback)sendSourceAndReceiveToResult(Source,Result)sendSourceAndReceiveToResult(Source,WebServiceMessageCallback,Result)sendSourceAndReceive(Source,SourceExtractor)sendSourceAndReceive(Source,WebServiceMessageCallback,SourceExtractor)sendAndReceive(WebServiceMessageCallback,WebServiceMessageCallback)
-
getDestinationProvider
Returns the destination provider used on operations that do not have a URI parameter. -
setDestinationProvider
Set the destination provider URI to be used on operations that do not have a URI parameter.Typically, either this property is set, or
setDefaultUri(String), but not both.- See Also:
-
marshalSendAndReceive(Object)marshalSendAndReceive(Object,WebServiceMessageCallback)sendSourceAndReceiveToResult(Source,Result)sendSourceAndReceiveToResult(Source,WebServiceMessageCallback,Result)sendSourceAndReceive(Source,SourceExtractor)sendSourceAndReceive(Source,WebServiceMessageCallback,SourceExtractor)sendAndReceive(WebServiceMessageCallback,WebServiceMessageCallback)
-
getMarshaller
public org.springframework.oxm.Marshaller getMarshaller()Returns the marshaller for this template. -
setMarshaller
public void setMarshaller(org.springframework.oxm.Marshaller marshaller) Sets the marshaller for this template. -
getUnmarshaller
public org.springframework.oxm.Unmarshaller getUnmarshaller()Returns the unmarshaller for this template. -
setUnmarshaller
public void setUnmarshaller(org.springframework.oxm.Unmarshaller unmarshaller) Sets the unmarshaller for this template. -
getFaultMessageResolver
Returns the fault message resolver for this template. -
setFaultMessageResolver
Sets the fault resolver for this template. Default is theSoapFaultMessageResolver, but may be set tonullto disable fault handling. -
setCheckConnectionForError
public void setCheckConnectionForError(boolean checkConnectionForError) Indicates whether the connection should be checked for error indicators (true), or whether these should be ignored (false). The default istrue.When using an HTTP transport, this property defines whether to check the HTTP response status code is in the 2xx Successful range. Both the SOAP specification and the WS-I Basic Profile define that a Web service must return a "200 OK" or "202 Accepted" HTTP status code for a normal response. Setting this property to
falseallows this template to deal with non-conforming services. -
setCheckConnectionForFault
public void setCheckConnectionForFault(boolean checkConnectionForFault) Indicates whether the connection should be checked for fault indicators (true), or whether we should rely on themessageonly (false). The default istrue.When using an HTTP transport, this property defines whether to check the HTTP response status code for fault indicators. Both the SOAP specification and the WS-I Basic Profile define that a Web service must return a "500 Internal Server Error" HTTP status code if the response envelope is a Fault. Setting this property to
falseallows this template to deal with non-conforming services. -
getInterceptors
Returns the client interceptors to apply to all web service invocations made by this template.- Returns:
- array of endpoint interceptors, or
nullif none
-
setInterceptors
Sets the client interceptors to apply to all web service invocations made by this template.- Parameters:
interceptors- array of endpoint interceptors, ornullif none
-
initDefaultStrategies
protected void initDefaultStrategies()Initialize the default implementations for the template's strategies:SoapFaultMessageResolver,SaajSoapMessageFactory, andHttpUrlConnectionMessageSender.- Throws:
org.springframework.beans.factory.BeanInitializationException- in case of initalization errors- See Also:
-
marshalSendAndReceive
Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, marshalled by the configuredMarshaller. Returns the unmarshalled payload of the response message, if any.This will only work with a default uri specified!
- Specified by:
marshalSendAndReceivein interfaceWebServiceOperations- Parameters:
requestPayload- the object to marshal into the request message payload- Returns:
- the unmarshalled payload of the response message, or
nullif no response is given - See Also:
-
marshalSendAndReceive
Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, marshalled by the configuredMarshaller. Returns the unmarshalled payload of the response message, if any.- Specified by:
marshalSendAndReceivein interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the object to marshal into the request message payload- Returns:
- the unmarshalled payload of the response message, or
nullif no response is given - See Also:
-
marshalSendAndReceive
public Object marshalSendAndReceive(Object requestPayload, WebServiceMessageCallback requestCallback) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, marshalled by the configuredMarshaller. Returns the unmarshalled payload of the response message, if any. The given callback allows changing of the request message after the payload has been marshalled to it.This will only work with a default uri specified!
- Specified by:
marshalSendAndReceivein interfaceWebServiceOperations- Parameters:
requestPayload- the object to marshal into the request message payloadrequestCallback- callback to change message, can benull- Returns:
- the unmarshalled payload of the response message, or
nullif no response is given - See Also:
-
marshalSendAndReceive
public Object marshalSendAndReceive(String uri, Object requestPayload, WebServiceMessageCallback requestCallback) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, marshalled by the configuredMarshaller. Returns the unmarshalled payload of the response message, if any. The given callback allows changing of the request message after the payload has been marshalled to it.- Specified by:
marshalSendAndReceivein interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the object to marshal into the request message payloadrequestCallback- callback to change message, can benull- Returns:
- the unmarshalled payload of the response message, or
nullif no response is given - See Also:
-
sendSourceAndReceiveToResult
Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload. Writes the response, if any, to the givenResult.This will only work with a default uri specified!
- Specified by:
sendSourceAndReceiveToResultin interfaceWebServiceOperations- Parameters:
requestPayload- the payload of the request messageresponseResult- the result to write the response payload to- Returns:
trueif a response was received;falseotherwise
-
sendSourceAndReceiveToResult
public boolean sendSourceAndReceiveToResult(String uri, Source requestPayload, Result responseResult) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload. Writes the response, if any, to the givenResult.- Specified by:
sendSourceAndReceiveToResultin interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the payload of the request messageresponseResult- the result to write the response payload to- Returns:
trueif a response was received;falseotherwise
-
sendSourceAndReceiveToResult
public boolean sendSourceAndReceiveToResult(Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload. Writes the response, if any, to the givenResult.The given callback allows changing of the request message after the payload has been written to it.
This will only work with a default uri specified!
- Specified by:
sendSourceAndReceiveToResultin interfaceWebServiceOperations- Parameters:
requestPayload- the payload of the request messagerequestCallback- callback to change message, can benullresponseResult- the result to write the response payload to- Returns:
trueif a response was received;falseotherwise
-
sendSourceAndReceiveToResult
public boolean sendSourceAndReceiveToResult(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload. Writes the response, if any, to the givenResult.The given callback allows changing of the request message after the payload has been written to it.
- Specified by:
sendSourceAndReceiveToResultin interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the payload of the request messagerequestCallback- callback to change message, can benullresponseResult- the result to write the response payload to- Returns:
trueif a response was received;falseotherwise
-
sendSourceAndReceive
Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, reading the result with aSourceExtractor.This will only work with a default uri specified!
- Specified by:
sendSourceAndReceivein interfaceWebServiceOperations- Parameters:
requestPayload- the payload of the request messageresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
SourceExtractor
-
sendSourceAndReceive
public <T> T sendSourceAndReceive(String uri, Source requestPayload, SourceExtractor<T> responseExtractor) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, reading the result with aSourceExtractor.- Specified by:
sendSourceAndReceivein interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the payload of the request messageresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
SourceExtractor
-
sendSourceAndReceive
public <T> T sendSourceAndReceive(Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, reading the result with aSourceExtractor.The given callback allows changing of the request message after the payload has been written to it.
This will only work with a default uri specified!
- Specified by:
sendSourceAndReceivein interfaceWebServiceOperations- Parameters:
requestPayload- the payload of the request messagerequestCallback- callback to change message, can benullresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
SourceExtractor
-
sendSourceAndReceive
public <T> T sendSourceAndReceive(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor) Description copied from interface:WebServiceOperationsSends a web service message that contains the given payload, reading the result with aSourceExtractor.The given callback allows changing of the request message after the payload has been written to it.
- Specified by:
sendSourceAndReceivein interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestPayload- the payload of the request messagerequestCallback- callback to change message, can benullresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
SourceExtractor
-
sendAndReceive
public boolean sendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback) Description copied from interface:WebServiceOperationsSends a web service message that can be manipulated with the given request callback, handling the response with a response callback.This will only work with a default uri specified!
- Specified by:
sendAndReceivein interfaceWebServiceOperations- Parameters:
requestCallback- the callback to be used for manipulating the request messageresponseCallback- the callback to be used for manipulating the response message- Returns:
trueif a response was received;falseotherwise
-
sendAndReceive
public boolean sendAndReceive(String uri, WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback) Description copied from interface:WebServiceOperationsSends a web service message that can be manipulated with the given request callback, handling the response with a response callback.- Specified by:
sendAndReceivein interfaceWebServiceOperations- Parameters:
uri- the URI to send the message torequestCallback- the callback to be used for manipulating the request messageresponseCallback- the callback to be used for manipulating the response message- Returns:
trueif a response was received;falseotherwise
-
sendAndReceive
public <T> T sendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) Description copied from interface:WebServiceOperationsSends a web service message that can be manipulated with the given callback, reading the result with aWebServiceMessageExtractor.This will only work with a default uri specified!
- Specified by:
sendAndReceivein interfaceWebServiceOperations- Parameters:
requestCallback- the requestCallback to be used for manipulating the request messageresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
WebServiceMessageExtractor
-
sendAndReceive
public <T> T sendAndReceive(String uriString, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) Description copied from interface:WebServiceOperationsSends a web service message that can be manipulated with the given callback, reading the result with aWebServiceMessageExtractor.- Specified by:
sendAndReceivein interfaceWebServiceOperations- Parameters:
uriString- the URI to send the message torequestCallback- the requestCallback to be used for manipulating the request messageresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
WebServiceMessageExtractor
-
doSendAndReceive
protected <T> T doSendAndReceive(MessageContext messageContext, WebServiceConnection connection, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) throws IOException Sends and receives aMessageContext. Sends therequest message, and received to therepsonse message. Invocates the definedinterceptorsas part of the process.- Parameters:
messageContext- the message contextconnection- the connection to userequestCallback- the requestCallback to be used for manipulating the request messageresponseExtractor- object that will extract results- Returns:
- an arbitrary result object, as returned by the
WebServiceMessageExtractor - Throws:
WebServiceClientException- if there is a problem sending or receiving the messageIOException- in case of I/O errors
-
hasError
protected boolean hasError(WebServiceConnection connection, WebServiceMessage request) throws IOException Determines whether the given connection or message context has an error.This implementation checks the
connectionfirst. If it indicates an error, it makes sure that it is not afault.- Parameters:
connection- the connection (possibly aFaultAwareWebServiceConnectionrequest- the response message (possibly aFaultAwareWebServiceMessage- Returns:
trueif the connection has an error;falseotherwise- Throws:
IOException- in case of I/O errors
-
handleError
protected Object handleError(WebServiceConnection connection, WebServiceMessage request) throws IOException Handles an error on the given connection. The default implementation throws aWebServiceTransportException.- Parameters:
connection- the erroneous connectionrequest- the corresponding request message- Returns:
- the object to be returned from
sendAndReceive(String,WebServiceMessageCallback, WebServiceMessageExtractor), if any - Throws:
IOException
-
hasFault
protected boolean hasFault(WebServiceConnection connection, WebServiceMessage response) throws IOException Determines whether the given connection or message has a fault.This implementation checks the
connectionif thecheckConnectionForFaultproperty is true, and defaults to themessageotherwise.- Parameters:
connection- the connection (possibly aFaultAwareWebServiceConnectionresponse- the response message (possibly aFaultAwareWebServiceMessage- Returns:
trueif either the connection or the message has a fault;falseotherwise- Throws:
IOException- in case of I/O errors
-
handleFault
protected Object handleFault(WebServiceConnection connection, MessageContext messageContext) throws IOException Handles an fault in the given response message. The default implementation invokes thefault resolverif registered, or invokeshandleError(WebServiceConnection, WebServiceMessage)otherwise.- Parameters:
connection- the faulty connectionmessageContext- the message context- Returns:
- the object to be returned from
sendAndReceive(String,WebServiceMessageCallback, WebServiceMessageExtractor), if any - Throws:
IOException
-