Class RemoteClient

  • All Implemented Interfaces:
    java.lang.Cloneable, Client

    public class RemoteClient
    extends AbstractClient
    implements java.lang.Cloneable
    Remote client bean for retrieving data from URL resources.

    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.

    Version:
    5.0 Note since Alfresco 5.0 this was rewritten against Apache HttpClient 4.3
    Author:
    Kevin Roast
    • Constructor Detail

      • RemoteClient

        public RemoteClient()
    • Method Detail

      • init

        public void init()
      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Clone a RemoteClient and all the properties.

        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.

        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • setConfigService

        public void setConfigService​(org.springframework.extensions.config.ConfigService configService)
      • getConfigService

        public org.springframework.extensions.config.ConfigService getConfigService()
      • setTicketName

        public void setTicketName​(java.lang.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:
        ticketName - String
      • getTicketName

        public java.lang.String getTicketName()
        Returns:
        the authentication ticket name to use
      • setDefaultEncoding

        public void setDefaultEncoding​(java.lang.String defaultEncoding)
        Parameters:
        defaultEncoding - the defaultEncoding to set
      • setDefaultContentType

        public void setDefaultContentType​(java.lang.String defaultContentType)
        Parameters:
        defaultContentType - the defaultContentType to set
      • setBufferSize

        public void setBufferSize​(int bufferSize)
        Parameters:
        bufferSize - the bufferSize to set
      • setConnectTimeout

        public void setConnectTimeout​(int connectTimeout)
        Parameters:
        connectTimeout - the connectTimeout to set
      • setReadTimeout

        public void setReadTimeout​(int readTimeout)
        Parameters:
        readTimeout - the readTimeout to set
      • setMaxRedirects

        public void setMaxRedirects​(int maxRedirects)
        Parameters:
        maxRedirects - the maxRedirects to set
      • getPoolSize

        public int getPoolSize()
        Returns:
        the connection thread pool size
      • setPoolSize

        public void setPoolSize​(int poolSize)
        Parameters:
        poolSize - the connection thread pool size to set
      • setAllowHttpProxy

        public void setAllowHttpProxy​(boolean allowHttpProxy)
        Parameters:
        allowHttpProxy - allowHttpProxy to set
      • setAllowHttpsProxy

        public void setAllowHttpsProxy​(boolean allowHttpsProxy)
        Parameters:
        allowHttpsProxy - allowHttpsProxy to set
      • setRemoveRequestHeaders

        public void setRemoveRequestHeaders​(java.util.Set<java.lang.String> removeRequestHeaders)
        Parameters:
        removeRequestHeaders - the removeRequestHeaders to set
      • setRemoveResponseHeaders

        public void setRemoveResponseHeaders​(java.util.Set<java.lang.String> removeResponseHeaders)
        Parameters:
        removeResponseHeaders - the removeResponseHeaders to set
      • setTicket

        public void setTicket​(java.lang.String ticket)
        Sets the authentication ticket to use. Will be used for all future call() requests.
        Parameters:
        ticket - String
      • getTicket

        public java.lang.String getTicket()
        Returns the authentication ticket
        Returns:
        String
      • setUsernamePassword

        public void setUsernamePassword​(java.lang.String user,
                                        java.lang.String pass)
        Basic HTTP auth. Will be used for all future call() requests.
        Parameters:
        user - String
        pass - String
      • setRequestContentType

        public void setRequestContentType​(java.lang.String contentType)
        Parameters:
        contentType - the POST request "Content-Type" header value to set NOTE: this value is reset to the defaultContentType value after a call() is made.
      • getRequestContentType

        public java.lang.String getRequestContentType()
      • setRequestMethod

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

        public HttpMethod getRequestMethod()
        Returns:
        the current Request Method
      • setRequestProperties

        public void setRequestProperties​(java.util.Map<java.lang.String,​java.lang.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 - map of request properties to set
      • setRequestHeaders

        public void setRequestHeaders​(java.util.Map<java.lang.String,​java.lang.String> requestHeaders)
        Configuration of custom request headers to be applied to each request. The request properties set programmatically above at runtime will augment and override these configuration defaults.
        Parameters:
        requestHeaders - map of request headers to set
      • setCookies

        public void setCookies​(java.util.Map<java.lang.String,​java.lang.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 java.util.Map<java.lang.String,​java.lang.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
      • setHttpTcpNodelay

        public void setHttpTcpNodelay​(boolean httpTcpNodelay)
        Parameters:
        httpTcpNodelay - Value for the http.tcp.nodelay setting - default is true
      • setHttpConnectionStalecheck

        public void setHttpConnectionStalecheck​(boolean httpConnectionStalecheck)
        Parameters:
        httpConnectionStalecheck - Value for the http.connection.stalecheck setting - default is true
      • setCommitResponseOnAuthenticationError

        public void setCommitResponseOnAuthenticationError​(boolean commitResponseOnAuthenticationError)
        Parameters:
        commitResponseOnAuthenticationError - true to commit the response if a 401 error is returned, false otherwise.
      • setExceptionOnError

        public void setExceptionOnError​(boolean exceptionOnError)
        Parameters:
        exceptionOnError - true to throw an exception on a server 500 response - else return 500 code in the usual Response object.
      • call

        public Response call​(java.lang.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​(java.lang.String uri,
                             java.lang.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​(java.lang.String uri,
                             java.io.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​(java.lang.String uri,
                             boolean buildResponseString,
                             java.io.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​(java.lang.String uri,
                             java.io.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​(java.lang.String uri,
                             java.io.InputStream in,
                             java.io.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​(java.lang.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 java.net.URL processResponse​(java.net.URL url,
                                               org.apache.http.HttpResponse response)
                                        throws java.net.MalformedURLException
        Pre-processes the response, propagating cookies and deciding whether a redirect is required
        Parameters:
        url - URL that was executed
        response - the executed HttpResponse from the method
        Throws:
        java.net.MalformedURLException
      • buildURL

        protected java.net.URL buildURL​(java.lang.String uri)
                                 throws java.net.MalformedURLException
        Build the URL object based on the supplied uri and configured endpoint. Ticket will be appiled as an argument if available.
        Parameters:
        uri - URI to build URL against
        Returns:
        the URL object representing the call.
        Throws:
        java.net.MalformedURLException
      • copyResponseStreamOutput

        protected void copyResponseStreamOutput​(java.net.URL url,
                                                javax.servlet.http.HttpServletResponse res,
                                                java.io.OutputStream out,
                                                org.apache.http.HttpResponse response,
                                                java.lang.String contentType,
                                                int bufferSize)
                                         throws java.io.IOException
        Copy response stream to the output
        Parameters:
        url - The URL object that the response was retrieved from
        res - The HttpServletResponse (can be null for in-memory response processing)
        out - The OutputStream to use
        response - The HttpResponse from the Method that was executed - will retrieve entity as stream
        contentType - The ContentType value of the response
        bufferSize - The buffer size to use
        Throws:
        java.io.IOException
      • processContentType

        protected void processContentType​(java.net.URL url,
                                          javax.servlet.http.HttpServletResponse res,
                                          org.apache.http.Header contentType)
        Optional additional processing based on the contentType header
        Parameters:
        url - Source URL that was requested
        res - The response (unprocessed as yet)
        contentType - Content-Type header from the response
      • createHttpClient

        protected org.apache.http.client.HttpClient createHttpClient​(java.net.URL url)
        Create and configure an HttpClient per thread based on Pooled connection manager. Proxy route will be applied the client based on current settings.
        Parameters:
        url - URL
        Returns:
        HttpClient
      • createProxyHost

        protected static org.apache.http.HttpHost createProxyHost​(java.lang.String hostProperty,
                                                                  java.lang.String portProperty,
                                                                  int defaultPort)
        Create HTTP proxy host for the given system host and port properties. If the properties are not set, no proxy will be created.
        Parameters:
        hostProperty - String
        portProperty - String
        defaultPort - int
        Returns:
        HttpHost if appropriate properties have been set, null otherwise