public class WebServiceTemplate extends WebServiceAccessor implements 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.
createConnection()
.createWebServiceMessage()
on the registered message factory to create a request message.doWithMessage()
on the request callback, if any. This
step stores content in the request message, based on Source
, marshalling, etc.handleRequest()
on the registered interceptors
. Interceptors are executed in order. If any of the interceptors
creates a response message in the message context, skip to step 7.send()
on the connection.hasError()
to check if the connection has an error. For an HTTP
transport, a status code other than 2xx
indicates 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.handleError()
method, which by default throws a WebServiceTransportException
.receive
on the connection to read the
response message, if any.null
or
false
hasFault()
to determine
whether the response has a fault. If it has, call ClientInterceptor.handleFault(MessageContext)
and the
handleFault()
method.ClientInterceptor.handleResponse(MessageContext)
and extractData()
on the response extractor, or
doWithMessage
on the response callback.close
on the connection.Modifier and Type | Field and Description |
---|---|
static String |
MESSAGE_TRACING_LOG_CATEGORY
Log category to use for message tracing.
|
protected static org.apache.commons.logging.Log |
receivedMessageTracingLogger
Additional logger to use for received message tracing.
|
protected static org.apache.commons.logging.Log |
sentMessageTracingLogger
Additional logger to use for sent message tracing.
|
logger
Constructor and Description |
---|
WebServiceTemplate()
Creates a new
WebServiceTemplate using default settings. |
WebServiceTemplate(org.springframework.oxm.Marshaller marshaller)
Creates a new
WebServiceTemplate with the given marshaller. |
WebServiceTemplate(org.springframework.oxm.Marshaller marshaller,
org.springframework.oxm.Unmarshaller unmarshaller)
Creates a new
MarshallingMethodEndpointAdapter with the given marshaller and unmarshaller. |
WebServiceTemplate(WebServiceMessageFactory messageFactory)
Creates a new
WebServiceTemplate based on the given message factory. |
Modifier and Type | Method and Description |
---|---|
protected <T> T |
doSendAndReceive(MessageContext messageContext,
WebServiceConnection connection,
WebServiceMessageCallback requestCallback,
WebServiceMessageExtractor<T> responseExtractor)
Sends and receives a
MessageContext . |
String |
getDefaultUri()
Returns the default URI to be used on operations that do not have a URI parameter.
|
DestinationProvider |
getDestinationProvider()
Returns the destination provider used on operations that do not have a URI parameter.
|
FaultMessageResolver |
getFaultMessageResolver()
Returns the fault message resolver for this template.
|
ClientInterceptor[] |
getInterceptors()
Returns the client interceptors to apply to all web service invocations made by this template.
|
org.springframework.oxm.Marshaller |
getMarshaller()
Returns the marshaller for this template.
|
org.springframework.oxm.Unmarshaller |
getUnmarshaller()
Returns the unmarshaller for this template.
|
protected Object |
handleError(WebServiceConnection connection,
WebServiceMessage request)
Handles an error on the given connection.
|
protected Object |
handleFault(WebServiceConnection connection,
MessageContext messageContext)
Handles an fault in the given response message.
|
protected boolean |
hasError(WebServiceConnection connection,
WebServiceMessage request)
Determines whether the given connection or message context has an error.
|
protected boolean |
hasFault(WebServiceConnection connection,
WebServiceMessage response)
Determines whether the given connection or message has a fault.
|
protected void |
initDefaultStrategies()
Initialize the default implementations for the template's strategies:
SoapFaultMessageResolver , SaajSoapMessageFactory , and HttpUrlConnectionMessageSender . |
Object |
marshalSendAndReceive(Object requestPayload)
Sends a web service message that contains the given payload, marshalled by the configured
Marshaller . |
Object |
marshalSendAndReceive(Object requestPayload,
WebServiceMessageCallback requestCallback)
Sends a web service message that contains the given payload, marshalled by the configured
Marshaller . |
Object |
marshalSendAndReceive(String uri,
Object requestPayload)
Sends a web service message that contains the given payload, marshalled by the configured
Marshaller . |
Object |
marshalSendAndReceive(String uri,
Object requestPayload,
WebServiceMessageCallback requestCallback)
Sends a web service message that contains the given payload, marshalled by the configured
Marshaller . |
boolean |
sendAndReceive(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> T |
sendAndReceive(String uriString,
WebServiceMessageCallback requestCallback,
WebServiceMessageExtractor<T> responseExtractor)
Sends a web service message that can be manipulated with the given callback, reading the result with a
WebServiceMessageExtractor . |
boolean |
sendAndReceive(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> T |
sendAndReceive(WebServiceMessageCallback requestCallback,
WebServiceMessageExtractor<T> responseExtractor)
Sends a web service message that can be manipulated with the given callback, reading the result with a
WebServiceMessageExtractor . |
<T> T |
sendSourceAndReceive(Source requestPayload,
SourceExtractor<T> responseExtractor)
Sends a web service message that contains the given payload, reading the result with a
SourceExtractor . |
<T> T |
sendSourceAndReceive(Source requestPayload,
WebServiceMessageCallback requestCallback,
SourceExtractor<T> responseExtractor)
Sends a web service message that contains the given payload, reading the result with a
SourceExtractor . |
<T> T |
sendSourceAndReceive(String uri,
Source requestPayload,
SourceExtractor<T> responseExtractor)
Sends a web service message that contains the given payload, reading the result with a
SourceExtractor . |
<T> T |
sendSourceAndReceive(String uri,
Source requestPayload,
WebServiceMessageCallback requestCallback,
SourceExtractor<T> responseExtractor)
Sends a web service message that contains the given payload, reading the result with a
SourceExtractor . |
boolean |
sendSourceAndReceiveToResult(Source requestPayload,
Result responseResult)
Sends a web service message that contains the given payload.
|
boolean |
sendSourceAndReceiveToResult(Source requestPayload,
WebServiceMessageCallback requestCallback,
Result responseResult)
Sends a web service message that contains the given payload.
|
boolean |
sendSourceAndReceiveToResult(String uri,
Source requestPayload,
Result responseResult)
Sends a web service message that contains the given payload.
|
boolean |
sendSourceAndReceiveToResult(String uri,
Source requestPayload,
WebServiceMessageCallback requestCallback,
Result responseResult)
Sends a web service message that contains the given payload.
|
void |
setCheckConnectionForError(boolean checkConnectionForError)
Indicates whether the connection should be checked for error
indicators (
true ), or whether these should be ignored (false ). |
void |
setCheckConnectionForFault(boolean checkConnectionForFault)
Indicates whether the connection should be checked for
fault indicators (
true ), or whether we should rely on the message only (false ). |
void |
setDefaultUri(String uri)
Set the default URI to be used on operations that do not have a URI parameter.
|
void |
setDestinationProvider(DestinationProvider destinationProvider)
Set the destination provider URI to be used on operations that do not have a URI parameter.
|
void |
setFaultMessageResolver(FaultMessageResolver faultMessageResolver)
Sets the fault resolver for this template.
|
void |
setInterceptors(ClientInterceptor[] interceptors)
Sets the client interceptors to apply to all web service invocations made by this template.
|
void |
setMarshaller(org.springframework.oxm.Marshaller marshaller)
Sets the marshaller for this template.
|
void |
setUnmarshaller(org.springframework.oxm.Unmarshaller unmarshaller)
Sets the unmarshaller for this template.
|
afterPropertiesSet, createConnection, getMessageFactory, getMessageSenders, setMessageFactory, setMessageSender, setMessageSenders
createTransformer, getTransformerFactory, newTransformerFactory, setTransformerFactoryClass, transform
public static final String MESSAGE_TRACING_LOG_CATEGORY
protected static final org.apache.commons.logging.Log sentMessageTracingLogger
protected static final org.apache.commons.logging.Log receivedMessageTracingLogger
public WebServiceTemplate()
WebServiceTemplate
using default settings.public WebServiceTemplate(WebServiceMessageFactory messageFactory)
WebServiceTemplate
based on the given message factory.messageFactory
- the message factory to usepublic WebServiceTemplate(org.springframework.oxm.Marshaller marshaller)
WebServiceTemplate
with the given marshaller. If the given Marshaller
also implements the Unmarshaller
interface, it is used for both marshalling and
unmarshalling. Otherwise, an exception is thrown.
Note that all Marshaller
implementations in Spring also implement the Unmarshaller
interface,
so that you can safely use this constructor.
marshaller
- object used as marshaller and unmarshallerIllegalArgumentException
- when marshaller
does not implement the Unmarshaller
interfacepublic WebServiceTemplate(org.springframework.oxm.Marshaller marshaller, org.springframework.oxm.Unmarshaller unmarshaller)
MarshallingMethodEndpointAdapter
with the given marshaller and unmarshaller.marshaller
- the marshaller to useunmarshaller
- the unmarshaller to usepublic String getDefaultUri()
public void setDefaultUri(String uri)
Typically, either this property is set, or setDestinationProvider(DestinationProvider)
, but not both.
marshalSendAndReceive(Object)
,
marshalSendAndReceive(Object,WebServiceMessageCallback)
,
sendSourceAndReceiveToResult(Source,Result)
,
sendSourceAndReceiveToResult(Source,WebServiceMessageCallback,Result)
,
sendSourceAndReceive(Source,SourceExtractor)
,
sendSourceAndReceive(Source,WebServiceMessageCallback,SourceExtractor)
,
sendAndReceive(WebServiceMessageCallback,WebServiceMessageCallback)
public DestinationProvider getDestinationProvider()
public void setDestinationProvider(DestinationProvider destinationProvider)
Typically, either this property is set, or setDefaultUri(String)
, but not both.
marshalSendAndReceive(Object)
,
marshalSendAndReceive(Object,WebServiceMessageCallback)
,
sendSourceAndReceiveToResult(Source,Result)
,
sendSourceAndReceiveToResult(Source,WebServiceMessageCallback,Result)
,
sendSourceAndReceive(Source,SourceExtractor)
,
sendSourceAndReceive(Source,WebServiceMessageCallback,SourceExtractor)
,
sendAndReceive(WebServiceMessageCallback,WebServiceMessageCallback)
public org.springframework.oxm.Marshaller getMarshaller()
public void setMarshaller(org.springframework.oxm.Marshaller marshaller)
public org.springframework.oxm.Unmarshaller getUnmarshaller()
public void setUnmarshaller(org.springframework.oxm.Unmarshaller unmarshaller)
public FaultMessageResolver getFaultMessageResolver()
public void setFaultMessageResolver(FaultMessageResolver faultMessageResolver)
SoapFaultMessageResolver
, but may be
set to null
to disable fault handling.public void setCheckConnectionForError(boolean checkConnectionForError)
true
), or whether these should be ignored (false
). The default is
true
.
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 false
allows this template to deal with non-conforming services.
public void setCheckConnectionForFault(boolean checkConnectionForFault)
true
), or whether we should rely on the message
only (false
). The default is true
.
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
false
allows this template to deal with non-conforming services.
public ClientInterceptor[] getInterceptors()
null
if nonepublic final void setInterceptors(ClientInterceptor[] interceptors)
interceptors
- array of endpoint interceptors, or null
if noneprotected void initDefaultStrategies()
SoapFaultMessageResolver
, SaajSoapMessageFactory
, and HttpUrlConnectionMessageSender
.org.springframework.beans.factory.BeanInitializationException
- in case of initalization errorssetFaultMessageResolver(FaultMessageResolver)
,
WebServiceAccessor.setMessageFactory(WebServiceMessageFactory)
,
WebServiceAccessor.setMessageSender(WebServiceMessageSender)
public Object marshalSendAndReceive(Object requestPayload)
WebServiceOperations
Marshaller
. Returns the unmarshalled payload of the response message, if any.
This will only work with a default uri specified!
marshalSendAndReceive
in interface WebServiceOperations
requestPayload
- the object to marshal into the request message payloadnull
if no response is givensetMarshaller(org.springframework.oxm.Marshaller)
,
setUnmarshaller(org.springframework.oxm.Unmarshaller)
public Object marshalSendAndReceive(String uri, Object requestPayload)
WebServiceOperations
Marshaller
. Returns the unmarshalled payload of the response message, if any.marshalSendAndReceive
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the object to marshal into the request message payloadnull
if no response is givensetMarshaller(org.springframework.oxm.Marshaller)
,
setUnmarshaller(org.springframework.oxm.Unmarshaller)
public Object marshalSendAndReceive(Object requestPayload, WebServiceMessageCallback requestCallback)
WebServiceOperations
Marshaller
. 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!
marshalSendAndReceive
in interface WebServiceOperations
requestPayload
- the object to marshal into the request message payloadrequestCallback
- callback to change message, can be null
null
if no response is givensetMarshaller(org.springframework.oxm.Marshaller)
,
setUnmarshaller(org.springframework.oxm.Unmarshaller)
public Object marshalSendAndReceive(String uri, Object requestPayload, WebServiceMessageCallback requestCallback)
WebServiceOperations
Marshaller
. 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.marshalSendAndReceive
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the object to marshal into the request message payloadrequestCallback
- callback to change message, can be null
null
if no response is givensetMarshaller(org.springframework.oxm.Marshaller)
,
setUnmarshaller(org.springframework.oxm.Unmarshaller)
public boolean sendSourceAndReceiveToResult(Source requestPayload, Result responseResult)
WebServiceOperations
Result
.
This will only work with a default uri specified!
sendSourceAndReceiveToResult
in interface WebServiceOperations
requestPayload
- the payload of the request messageresponseResult
- the result to write the response payload totrue
if a response was received; false
otherwisepublic boolean sendSourceAndReceiveToResult(String uri, Source requestPayload, Result responseResult)
WebServiceOperations
Result
.sendSourceAndReceiveToResult
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the payload of the request messageresponseResult
- the result to write the response payload totrue
if a response was received; false
otherwisepublic boolean sendSourceAndReceiveToResult(Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult)
WebServiceOperations
Result
.
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!
sendSourceAndReceiveToResult
in interface WebServiceOperations
requestPayload
- the payload of the request messagerequestCallback
- callback to change message, can be null
responseResult
- the result to write the response payload totrue
if a response was received; false
otherwisepublic boolean sendSourceAndReceiveToResult(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, Result responseResult)
WebServiceOperations
Result
.
The given callback allows changing of the request message after the payload has been written to it.
sendSourceAndReceiveToResult
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the payload of the request messagerequestCallback
- callback to change message, can be null
responseResult
- the result to write the response payload totrue
if a response was received; false
otherwisepublic <T> T sendSourceAndReceive(Source requestPayload, SourceExtractor<T> responseExtractor)
WebServiceOperations
SourceExtractor
.
This will only work with a default uri specified!
sendSourceAndReceive
in interface WebServiceOperations
requestPayload
- the payload of the request messageresponseExtractor
- object that will extract resultsSourceExtractor
public <T> T sendSourceAndReceive(String uri, Source requestPayload, SourceExtractor<T> responseExtractor)
WebServiceOperations
SourceExtractor
.sendSourceAndReceive
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the payload of the request messageresponseExtractor
- object that will extract resultsSourceExtractor
public <T> T sendSourceAndReceive(Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor)
WebServiceOperations
SourceExtractor
.
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!
sendSourceAndReceive
in interface WebServiceOperations
requestPayload
- the payload of the request messagerequestCallback
- callback to change message, can be null
responseExtractor
- object that will extract resultsSourceExtractor
public <T> T sendSourceAndReceive(String uri, Source requestPayload, WebServiceMessageCallback requestCallback, SourceExtractor<T> responseExtractor)
WebServiceOperations
SourceExtractor
.
The given callback allows changing of the request message after the payload has been written to it.
sendSourceAndReceive
in interface WebServiceOperations
uri
- the URI to send the message torequestPayload
- the payload of the request messagerequestCallback
- callback to change message, can be null
responseExtractor
- object that will extract resultsSourceExtractor
public boolean sendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback)
WebServiceOperations
This will only work with a default uri specified!
sendAndReceive
in interface WebServiceOperations
requestCallback
- the callback to be used for manipulating the request messageresponseCallback
- the callback to be used for manipulating the response messagetrue
if a response was received; false
otherwisepublic boolean sendAndReceive(String uri, WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback)
WebServiceOperations
sendAndReceive
in interface WebServiceOperations
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 messagetrue
if a response was received; false
otherwisepublic <T> T sendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor)
WebServiceOperations
WebServiceMessageExtractor
.
This will only work with a default uri specified!
sendAndReceive
in interface WebServiceOperations
requestCallback
- the requestCallback to be used for manipulating the request messageresponseExtractor
- object that will extract resultsWebServiceMessageExtractor
public <T> T sendAndReceive(String uriString, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor)
WebServiceOperations
WebServiceMessageExtractor
.sendAndReceive
in interface WebServiceOperations
uriString
- the URI to send the message torequestCallback
- the requestCallback to be used for manipulating the request messageresponseExtractor
- object that will extract resultsWebServiceMessageExtractor
protected <T> T doSendAndReceive(MessageContext messageContext, WebServiceConnection connection, WebServiceMessageCallback requestCallback, WebServiceMessageExtractor<T> responseExtractor) throws IOException
MessageContext
. Sends the request message
, and
received to the repsonse message
. Invocates the defined interceptors
as part of the process.messageContext
- the message contextconnection
- the connection to userequestCallback
- the requestCallback to be used for manipulating the request messageresponseExtractor
- object that will extract resultsWebServiceMessageExtractor
WebServiceClientException
- if there is a problem sending or receiving the messageIOException
- in case of I/O errorsprotected boolean hasError(WebServiceConnection connection, WebServiceMessage request) throws IOException
This implementation checks the connection
first. If it indicates an
error, it makes sure that it is not a fault
.
connection
- the connection (possibly a FaultAwareWebServiceConnection
request
- the response message (possibly a FaultAwareWebServiceMessage
true
if the connection has an error; false
otherwiseIOException
- in case of I/O errorsprotected Object handleError(WebServiceConnection connection, WebServiceMessage request) throws IOException
WebServiceTransportException
.connection
- the erroneous connectionrequest
- the corresponding request messagesendAndReceive(String,WebServiceMessageCallback,
WebServiceMessageExtractor)
, if anyIOException
protected boolean hasFault(WebServiceConnection connection, WebServiceMessage response) throws IOException
This implementation checks the connection
if the checkConnectionForFault
property is true, and defaults to the message
otherwise.
connection
- the connection (possibly a FaultAwareWebServiceConnection
response
- the response message (possibly a FaultAwareWebServiceMessage
true
if either the connection or the message has a fault; false
otherwiseIOException
- in case of I/O errorsprotected Object handleFault(WebServiceConnection connection, MessageContext messageContext) throws IOException
fault resolver
if registered, or invokes handleError(WebServiceConnection,
WebServiceMessage)
otherwise.connection
- the faulty connectionmessageContext
- the message contextsendAndReceive(String,WebServiceMessageCallback,
WebServiceMessageExtractor)
, if anyIOException
Copyright © 2020 Pivotal Software. All rights reserved.