public class RemoteClient extends AbstractClient implements Cloneable
Can be used as a Script root object for HTTP methods via ScriptRemote
Generally remote URLs will be "data" webscripts (i.e. returning XML/JSON) called from web-tier script objects or directly from Java backed webscript methods.
Support for HTTP methods of GET, DELETE, PUT and POST of body content data. The Apache commons HttpClient library is used to provide superior handling of large POST body rather than the default JDK implementation.
A 'Response' is returned containing the response data stream as a String and the Status object representing the status code and error information if any. Methods supplying an InputStream will force a POST and methods supplying an OutputStream will stream the result directly to it (i.e. for a proxy) and will not generate a String response in the 'Response' object.
By default this bean has the id of 'connector.remoteclient' and is configured in spring-webscripts-application-context.xml found in the spring-webscripts project.
| Modifier and Type | Field and Description |
|---|---|
protected static String |
CHARSETEQUALS |
protected static int |
DEFAULT_BUFFERSIZE |
protected static int |
DEFAULT_CONNECT_TIMEOUT |
protected static int |
DEFAULT_MAX_REDIRECTS |
protected static int |
DEFAULT_POOLSIZE |
protected static int |
DEFAULT_READ_TIMEOUT |
protected static String |
DEFAULT_REQUEST_CONTENT_TYPE |
protected static String |
DEFAULT_TICKET_NAME |
protected static String |
HEADER_CONTENT_LENGTH |
protected static String |
HEADER_CONTENT_TYPE |
protected static String |
HEADER_COOKIE |
protected static String |
HEADER_SERVER |
protected static String |
HEADER_SET_COOKIE |
protected static String |
HEADER_TRANSFER_ENCODING |
static int |
SC_MOVED_PERMANENTLY |
static int |
SC_MOVED_TEMPORARILY |
static int |
SC_SEE_OTHER |
static int |
SC_TEMPORARY_REDIRECT |
protected static String |
X_WWW_FORM_URLENCODED |
endpoint| Constructor and Description |
|---|
RemoteClient() |
| Modifier and Type | Method and Description |
|---|---|
protected URL |
buildURL(String uri)
Build the URL object based on the supplied uri and configured endpoint.
|
Response |
call(String uri)
Call a remote WebScript uri.
|
Response |
call(String uri,
boolean buildResponseString,
InputStream in)
Call a remote WebScript uri.
|
Response |
call(String uri,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
Call a remote WebScript uri.
|
Response |
call(String uri,
InputStream in)
Call a remote WebScript uri.
|
Response |
call(String uri,
InputStream in,
OutputStream out)
Call a remote WebScript uri.
|
Response |
call(String uri,
OutputStream out)
Call a remote WebScript uri.
|
Response |
call(String uri,
String body)
Call a remote WebScript uri, passing the supplied body as a POST request (unless the
request method is set to override as say PUT).
|
Object |
clone()
Clone a RemoteClient and all the properties.
|
protected void |
copyResponseStreamOutput(URL url,
javax.servlet.http.HttpServletResponse res,
OutputStream out,
org.apache.http.HttpResponse response,
String contentType,
int bufferSize)
Copy response stream to the output
|
protected org.apache.http.client.HttpClient |
createHttpClient(URL url)
Create and configure an HttpClient per thread based on Pooled connection manager.
|
protected static org.apache.http.HttpHost |
createProxyHost(String hostProperty,
String portProperty,
int defaultPort)
Create HTTP proxy host for the given system host and port properties.
|
ConfigService |
getConfigService() |
Map<String,String> |
getCookies()
Gets the current set of cookies for state transfer.
|
int |
getPoolSize() |
String |
getRequestContentType() |
HttpMethod |
getRequestMethod() |
String |
getTicket()
Returns the authentication ticket
|
String |
getTicketName() |
void |
init() |
protected void |
processContentType(URL url,
javax.servlet.http.HttpServletResponse res,
org.apache.http.Header contentType)
Optional additional processing based on the contentType header
|
protected URL |
processResponse(URL url,
org.apache.http.HttpResponse response)
Pre-processes the response, propagating cookies and deciding whether a redirect is required
|
void |
setAllowHttpProxy(boolean allowHttpProxy) |
void |
setAllowHttpsProxy(boolean allowHttpsProxy) |
void |
setBufferSize(int bufferSize) |
void |
setCommitResponseOnAuthenticationError(boolean commitResponseOnAuthenticationError) |
void |
setConfigService(ConfigService configService) |
void |
setConnectTimeout(int connectTimeout) |
void |
setCookies(Map<String,String> cookies)
Provides a set of cookies for state transfer.
|
void |
setDefaultContentType(String defaultContentType) |
void |
setDefaultEncoding(String defaultEncoding) |
void |
setExceptionOnError(boolean exceptionOnError) |
void |
setHttpConnectionStalecheck(boolean httpConnectionStalecheck) |
void |
setHttpTcpNodelay(boolean httpTcpNodelay) |
void |
setMaxRedirects(int maxRedirects) |
void |
setPoolSize(int poolSize) |
void |
setReadTimeout(int readTimeout) |
void |
setRemoveRequestHeaders(Set<String> removeRequestHeaders) |
void |
setRemoveResponseHeaders(Set<String> removeResponseHeaders) |
void |
setRequestContentType(String contentType) |
void |
setRequestHeaders(Map<String,String> requestHeaders)
Configuration of custom request headers to be applied to each request.
|
void |
setRequestMethod(HttpMethod method) |
void |
setRequestProperties(Map<String,String> requestProperties)
Allows for additional request properties to be set onto this object
These request properties are applied to the connection when
the connection is called.
|
void |
setTicket(String ticket)
Sets the authentication ticket to use.
|
void |
setTicketName(String ticketName)
Sets the authentication ticket name to use.
|
void |
setUsernamePassword(String user,
String pass)
Basic HTTP auth.
|
getEndpoint, getURL, setEndpointprotected static final String HEADER_TRANSFER_ENCODING
protected static final String HEADER_CONTENT_LENGTH
protected static final String HEADER_CONTENT_TYPE
protected static final String HEADER_SET_COOKIE
protected static final String HEADER_COOKIE
protected static final String HEADER_SERVER
protected static final int DEFAULT_CONNECT_TIMEOUT
protected static final int DEFAULT_READ_TIMEOUT
protected static final int DEFAULT_BUFFERSIZE
protected static final int DEFAULT_MAX_REDIRECTS
protected static final int DEFAULT_POOLSIZE
protected static final String DEFAULT_TICKET_NAME
protected static final String DEFAULT_REQUEST_CONTENT_TYPE
protected static final String X_WWW_FORM_URLENCODED
protected static final String CHARSETEQUALS
public static final int SC_MOVED_TEMPORARILY
public static final int SC_MOVED_PERMANENTLY
public static final int SC_SEE_OTHER
public static final int SC_TEMPORARY_REDIRECT
public void init()
public Object clone() throws CloneNotSupportedException
This method is preferable in hot code to requesting a new copy of the "connector.remoteclient" bean from Spring - as the bean makes use of the prototype pattern and is quite expensive to create each time - also the Spring code has synchronization during the bean creation pattern which is additionally expensive during heavily threaded applications.
This clone method will only duplicate the non-stateful members of RemoteClient i.e. the same properties that would have been set by Spring during bean initialisation.
clone in class ObjectCloneNotSupportedExceptionpublic void setConfigService(ConfigService configService)
public ConfigService getConfigService()
public void setTicketName(String ticketName)
ticketName - Stringpublic String getTicketName()
public void setDefaultEncoding(String defaultEncoding)
defaultEncoding - the defaultEncoding to setpublic void setDefaultContentType(String defaultContentType)
defaultContentType - the defaultContentType to setpublic void setBufferSize(int bufferSize)
bufferSize - the bufferSize to setpublic void setConnectTimeout(int connectTimeout)
connectTimeout - the connectTimeout to setpublic void setReadTimeout(int readTimeout)
readTimeout - the readTimeout to setpublic void setMaxRedirects(int maxRedirects)
maxRedirects - the maxRedirects to setpublic int getPoolSize()
public void setPoolSize(int poolSize)
poolSize - the connection thread pool size to setpublic void setAllowHttpProxy(boolean allowHttpProxy)
allowHttpProxy - allowHttpProxy to setpublic void setAllowHttpsProxy(boolean allowHttpsProxy)
allowHttpsProxy - allowHttpsProxy to setpublic void setRemoveRequestHeaders(Set<String> removeRequestHeaders)
removeRequestHeaders - the removeRequestHeaders to setpublic void setRemoveResponseHeaders(Set<String> removeResponseHeaders)
removeResponseHeaders - the removeResponseHeaders to setpublic void setTicket(String ticket)
ticket - Stringpublic String getTicket()
public void setUsernamePassword(String user, String pass)
user - Stringpass - Stringpublic void setRequestContentType(String contentType)
contentType - the POST request "Content-Type" header value to set
NOTE: this value is reset to the defaultContentType value after a call() is made.public String getRequestContentType()
public void setRequestMethod(HttpMethod method)
method - the request Method to set i.e. one of GET/POST/PUT/DELETE etc.
if not set, GET will be assumed unless an InputStream is supplied during call()
in which case POST will be used unless the request method overrides it with PUT.
NOTE: this value is reset to the default of GET after a call() is made.public HttpMethod getRequestMethod()
public void setRequestProperties(Map<String,String> requestProperties)
requestProperties - map of request properties to setpublic void setRequestHeaders(Map<String,String> requestHeaders)
requestHeaders - map of request headers to setpublic void setCookies(Map<String,String> cookies)
cookies - the cookiespublic Map<String,String> getCookies()
public void setHttpTcpNodelay(boolean httpTcpNodelay)
httpTcpNodelay - Value for the http.tcp.nodelay setting - default is truepublic void setHttpConnectionStalecheck(boolean httpConnectionStalecheck)
httpConnectionStalecheck - Value for the http.connection.stalecheck setting - default is truepublic void setCommitResponseOnAuthenticationError(boolean commitResponseOnAuthenticationError)
commitResponseOnAuthenticationError - true to commit the response if a 401 error is returned, false otherwise.public void setExceptionOnError(boolean exceptionOnError)
exceptionOnError - true to throw an exception on a server 500 response - else return 500 code
in the usual Response object.public Response call(String uri)
uri - WebScript URI - for example /test/myscript?arg=valueResponsepublic Response call(String uri, String body)
uri - Uri to call on the endpointbody - Body of the POST request.Responsepublic Response call(String uri, InputStream in)
uri - WebScript URI - for example /test/myscript?arg=valuein - The optional InputStream to the call - if supplied a POST will be performedResponsepublic Response call(String uri, boolean buildResponseString, InputStream in)
uri - WebScript URI - for example /test/myscript?arg=valuebuildResponseString - True to build a String result automatically based on the response
encoding, false to instead return the InputStream in the Response.in - The optional InputStream to the call - if supplied a POST will be performedResponsepublic Response call(String uri, OutputStream out)
uri - WebScript URI - for example /test/myscript?arg=valueout - OutputStream to stream successful response to - will be closed automatically.
A response data string will not therefore be available in the Response object.
If remote call fails the OutputStream will not be modified or closed.Responsepublic Response call(String uri, InputStream in, OutputStream out)
uri - WebScript URI - for example /test/myscript?arg=valuein - The optional InputStream to the call - if supplied a POST will be performedout - OutputStream to stream response to - will be closed automatically.
A response data string will not therefore be available in the Response object.
If remote call returns a status code then any available error response will be
streamed into the output.
If remote call fails completely the OutputStream will not be modified or closed.Responsepublic Response call(String uri, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
uri - WebScript URI - for example /test/myscript?arg=valuereq - HttpServletRequest the request to retrieve input and headers etc. fromres - HttpServletResponse the response to stream response to - will be closed automatically.
A response data string will not therefore be available in the Response object.
The HTTP method to be used should be set via the setter otherwise GET will be assumed
and the InputStream will not be retrieve from the request.
If remote call returns a status code then any available error response will be
streamed into the response object.
If remote call fails completely the OutputStream will not be modified or closed.Responseprotected URL processResponse(URL url, org.apache.http.HttpResponse response) throws MalformedURLException
url - URL that was executedresponse - the executed HttpResponse from the methodMalformedURLExceptionprotected URL buildURL(String uri) throws MalformedURLException
uri - URI to build URL againstMalformedURLExceptionprotected void copyResponseStreamOutput(URL url, javax.servlet.http.HttpServletResponse res, OutputStream out, org.apache.http.HttpResponse response, String contentType, int bufferSize) throws IOException
url - The URL object that the response was retrieved fromres - The HttpServletResponse (can be null for in-memory response processing)out - The OutputStream to useresponse - The HttpResponse from the Method that was executed - will retrieve entity as streamcontentType - The ContentType value of the responsebufferSize - The buffer size to useIOExceptionprotected void processContentType(URL url, javax.servlet.http.HttpServletResponse res, org.apache.http.Header contentType)
url - Source URL that was requestedres - The response (unprocessed as yet)contentType - Content-Type header from the responseprotected org.apache.http.client.HttpClient createHttpClient(URL url)
url - URLprotected static org.apache.http.HttpHost createProxyHost(String hostProperty, String portProperty, int defaultPort)
hostProperty - StringportProperty - StringdefaultPort - intCopyright © 2005–2016 Alfresco Software. All rights reserved.