org.springframework.extensions.webscripts.connector
Class RemoteClient

java.lang.Object
  extended by org.springframework.extensions.webscripts.connector.AbstractClient
      extended by org.springframework.extensions.webscripts.connector.RemoteClient
All Implemented Interfaces:
Client

public class RemoteClient
extends AbstractClient

Remote client for for accessing data from remote URLs.

Can be used as a Script root object for simple HTTP requests.

Generally remote URLs will be "data" webscripts (i.e. returning XML/JSON) called from web-tier script objects and will be housed within an Alfresco Repository server.

Supports HTTP methods of GET, DELETE, PUT and POST of body content data.

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 and not generate a response in the 'Response' object.

Author:
Kevin Roast

Field Summary
static int SC_MOVED_PERMANENTLY
           
static int SC_MOVED_TEMPORARILY
           
static int SC_REMOTE_CONN_NOHOST
           
static int SC_REMOTE_CONN_TIMEOUT
           
static int SC_SEE_OTHER
           
static int SC_TEMPORARY_REDIRECT
           
 
Fields inherited from class org.springframework.extensions.webscripts.connector.AbstractClient
endpoint
 
Constructor Summary
RemoteClient(String endpoint)
          Construction
RemoteClient(String endpoint, String defaultEncoding)
          Construction
 
Method Summary
 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).
 Map<String,String> getCookies()
          Gets the current set of cookies for state transfer.
 String getTicket()
          Returns the authentication ticket
 String getTicketName()
           
protected  URL processResponse(URL url, org.apache.commons.httpclient.HttpMethod method)
          Pre-processes the response, propagating cookies and deciding whether a redirect is required
 void setCookies(Map<String,String> cookies)
          Provides a set of cookies for state transfer.
 void setRequestContentType(String contentType)
           
 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.
 
Methods inherited from class org.springframework.extensions.webscripts.connector.AbstractClient
getEndpoint, getURL
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SC_REMOTE_CONN_TIMEOUT

public static final int SC_REMOTE_CONN_TIMEOUT
See Also:
Constant Field Values

SC_REMOTE_CONN_NOHOST

public static final int SC_REMOTE_CONN_NOHOST
See Also:
Constant Field Values

SC_MOVED_TEMPORARILY

public static final int SC_MOVED_TEMPORARILY
See Also:
Constant Field Values

SC_MOVED_PERMANENTLY

public static final int SC_MOVED_PERMANENTLY
See Also:
Constant Field Values

SC_SEE_OTHER

public static final int SC_SEE_OTHER
See Also:
Constant Field Values

SC_TEMPORARY_REDIRECT

public static final int SC_TEMPORARY_REDIRECT
See Also:
Constant Field Values
Constructor Detail

RemoteClient

public RemoteClient(String endpoint)
Construction

Parameters:
endpoint - HTTP API endpoint of remote Alfresco server webapp For example http://servername:8080/alfresco

RemoteClient

public RemoteClient(String endpoint,
                    String defaultEncoding)
Construction

Parameters:
endpoint - HTTP API endpoint of remote Alfresco server webapp For example http://servername:8080/alfresco
defaultEncoding - Encoding to use when converting responses that do not specify one
Method Detail

setTicket

public void setTicket(String ticket)
Sets the authentication ticket to use. Will be used for all future call() requests.

Parameters:
ticket -

getTicket

public String getTicket()
Returns the authentication ticket

Returns:

setTicketName

public void setTicketName(String ticketName)
Sets the authentication ticket name to use. Will be used for all future call() requests. This allows the ticket mechanism to be repurposed for non-Alfresco implementations that may require similar argument passing

Parameters:
ticket -

getTicketName

public String getTicketName()
Returns:
the authentication ticket name to use

setUsernamePassword

public void setUsernamePassword(String user,
                                String pass)
Basic HTTP auth. Will be used for all future call() requests.

Parameters:
user -
pass -

setRequestContentType

public void setRequestContentType(String contentType)
Parameters:
requestContentType - the POST request "Content-Type" header value to set NOTE: this value is reset to the default of GET after a call() is made.

setRequestMethod

public void setRequestMethod(HttpMethod method)
Parameters:
requestMethod - 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.

setRequestProperties

public 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. Will be used for all future call() requests.

Parameters:
requestProperties -

setCookies

public void setCookies(Map<String,String> cookies)
Provides a set of cookies for state transfer. This set of cookies is maintained through any redirects followed by the client (e.g. redirect through SSO host).

Parameters:
cookies - the cookies

getCookies

public Map<String,String> getCookies()
Gets the current set of cookies for state transfer. This set of cookies is maintained through any redirects followed by the client (e.g. redirect through SSO host).

Returns:
the cookies

call

public Response call(String uri)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url. This API is generally called from a script host.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
Returns:
Response object from the call Response

call

public 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).

Parameters:
uri - Uri to call on the endpoint
body - Body of the POST request.
Returns:
Response object from the call Response

call

public Response call(String uri,
                     InputStream in)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
in - The optional InputStream to the call - if supplied a POST will be performed
Returns:
Response object from the call Response

call

public Response call(String uri,
                     boolean buildResponseString,
                     InputStream in)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
buildResponseString - 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 performed
Returns:
Response object from the call Response

call

public Response call(String uri,
                     OutputStream out)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
out - 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.
Returns:
Response object from the call Response

call

public Response call(String uri,
                     InputStream in,
                     OutputStream out)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
in - The optional InputStream to the call - if supplied a POST will be performed
out - 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.
Returns:
Response object from the call Response

call

public Response call(String uri,
                     javax.servlet.http.HttpServletRequest req,
                     javax.servlet.http.HttpServletResponse res)
Call a remote WebScript uri. The endpoint as supplied in the constructor will be used as the prefix for the full WebScript url.

Parameters:
uri - WebScript URI - for example /test/myscript?arg=value
req - HttpServletRequest the request to retrieve input and headers etc. from
res - 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.
Returns:
Response object from the call Response

processResponse

protected URL processResponse(URL url,
                              org.apache.commons.httpclient.HttpMethod method)
                       throws MalformedURLException
Pre-processes the response, propagating cookies and deciding whether a redirect is required

Parameters:
method - the executed method
Throws:
MalformedURLException


Copyright © 2009 SpringSource, Inc. All Rights Reserved.