Class WebService

    • Constructor Detail

      • WebService

        public WebService()
    • Method Detail

      • toParameterString

        protected static String toParameterString​(Map<String,​String> parameters,
                                                  boolean startWithQuestionMark)
      • clearCache

        public void clearCache()
        Removes all items from the cache that are older than DEFAULT_MAX_AGE.
      • getRequestDelay

        public long getRequestDelay()
        Returns:
        request delay in ms
        See Also:
        waitForTurn()
      • setRequestDelay

        public void setRequestDelay​(long requestDelay)
        Parameters:
        requestDelay - request delay in ms
        See Also:
        waitForTurn()
      • setSSLSocketFactory

        public void setSSLSocketFactory​(SSLSocketFactory sslSocketFactory)
      • getCache

        public Cache getCache()
      • setCache

        public void setCache​(Cache cache)
      • getMaxAge

        public long getMaxAge()
        Returns max age in milliseconds.
        Returns:
        max age in ms
      • setMaxAge

        public void setMaxAge​(long maxAge)
        Sets max age in milliseconds.
        Parameters:
        maxAge - in ms
      • getReadTimeOut

        public int getReadTimeOut()
      • setReadTimeOut

        public void setReadTimeOut​(int readTimeOut)
      • getConnectTimeOut

        public int getConnectTimeOut()
      • setConnectTimeOut

        public void setConnectTimeOut​(int connectTimeOut)
      • getXmlObject

        protected <T> T getXmlObject​(String request,
                                     long maximumAge,
                                     TimeUnit timeUnit,
                                     Class<T> klass)
                              throws IOException,
                                     javax.xml.bind.DataBindingException
        Checks the Cache for the requested URL, issues the request if necessary and parses the answer using JAXB to create an appropriate unmarshalled object.
        Parameters:
        request - URL
        maximumAge - max age
        timeUnit - max age time unit
        klass - class for the object to parse using JAXB
        Returns:
        XmlObject
        Throws:
        IOException
        javax.xml.bind.DataBindingException
      • getJSONArray

        protected org.json.simple.JSONArray getJSONArray​(String request,
                                                         Charset responseCharset,
                                                         long maximumAge,
                                                         TimeUnit timeUnit)
                                                  throws IOException,
                                                         org.json.simple.parser.ParseException
        Utility method to get a JSON array from a webservice.
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        maximumAge - up to this age the cache is consulted
        timeUnit - max age time unit
        Returns:
        JSON array
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • getJSONObject

        protected org.json.simple.JSONObject getJSONObject​(String request,
                                                           Charset responseCharset,
                                                           long maximumAge,
                                                           TimeUnit timeUnit)
                                                    throws IOException,
                                                           org.json.simple.parser.ParseException
        Utility method to get a JSON dictionary from a webservice.
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        maximumAge - up to this age the cache is consulted
        timeUnit - max age time unit
        Returns:
        JSON dictionary
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • getJSONArray

        protected org.json.simple.JSONArray getJSONArray​(String request,
                                                         Charset responseCharset,
                                                         Map<String,​String> parameters)
                                                  throws IOException,
                                                         org.json.simple.parser.ParseException
        Utility method to post parameters and get a JSON array result from a webservice.
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        parameters - parameters to send
        Returns:
        JSON array
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • getJSONObject

        protected org.json.simple.JSONObject getJSONObject​(String request,
                                                           Map<String,​String> headers,
                                                           Charset responseCharset,
                                                           Map<String,​String> parameters)
                                                    throws IOException,
                                                           org.json.simple.parser.ParseException
        Utility method to post parameters and get a JSON dictionary result from a webservice.
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        parameters - parameters to send
        Returns:
        JSON dictionary
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • getJSONObjectWithPayload

        protected org.json.simple.JSONObject getJSONObjectWithPayload​(String request,
                                                                      Map<String,​String> headers,
                                                                      Charset responseCharset,
                                                                      org.json.simple.JSONObject payload)
                                                               throws IOException,
                                                                      org.json.simple.parser.ParseException
        Utility method to post some JSON and get a JSON dictionary result from a webservice.
        Parameters:
        request - request URL
        responseCharset - character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignored
        payload - JSON request payload
        Returns:
        JSON dictionary
        Throws:
        IOException - if something goes wrong
        org.json.simple.parser.ParseException - if the JSON is malformed
      • openInputStream

        protected InputStream openInputStream​(URL url,
                                              long maximumAge,
                                              TimeUnit timeUnit)
                                       throws IOException
        Opens the URL and provides an (cached) InputStream for reading it. getRequestDelay() is honored, if the result is not from the cache.
        Parameters:
        url - url to read
        maximumAge - max age
        timeUnit - max age unit
        Returns:
        InputStream
        Throws:
        IOException
      • readURL

        protected byte[] readURL​(URL url,
                                 long maximumAge,
                                 TimeUnit timeUnit)
                          throws IOException
        Opens the URL per HTTP-GET and returns the content as a byte array. The result may come from the cache - if not getRequestDelay() is honored.
        Parameters:
        url - url to read
        maximumAge - max age
        timeUnit - max age unit
        Returns:
        InputStream
        Throws:
        IOException
      • useCacheOnly

        protected boolean useCacheOnly​(URL url)
        Allows to suppress the actual request.
        Parameters:
        url - url
        Returns:
        true, if we only want to look stuff up in the cache, but never want to retrieve the actual URL
      • cacheResource

        protected boolean cacheResource​(URL url,
                                        byte[] content)
        Allows turning caching for a specific resource and its content on and off.
        Parameters:
        url - url
        content - resource content
        Returns:
        by default this method returns true
      • setNextAllowedConnection

        protected void setNextAllowedConnection​(long nextTimestamp)
      • openConnection

        protected URLConnection openConnection​(URL url,
                                               String requestMethod,
                                               Map<String,​String> requestHeaders,
                                               InputStream requestBody)
                                        throws IOException
        Opens URLConnection and logs errors.
        Parameters:
        url - URL to connect to
        requestMethod - HTTP method, defaults to GET
        requestHeaders - optional headers to sent with the request
        requestBody - optional body to send in the body of the request
        Returns:
        opened URL connection
        Throws:
        IOException
      • handleStatusCode

        protected void handleStatusCode​(HttpURLConnection httpURLConnection,
                                        int statusCode)
                                 throws IOException
        Handles standard responses to status codes.
        Parameters:
        httpURLConnection - http url connection
        statusCode - status code
        Throws:
        IOException
      • waitForTurn

        protected void waitForTurn()
                            throws IOException
        Waits until this thread is allowed to send a request to the webservice. Contrary to privateWaitForTurn() this method is already synchronized. This is useful for web services that we are not allowed to talk to more than so and so many times per minute.
        Throws:
        IOException
        See Also:
        getRequestDelay()
      • getUserAgent

        public String getUserAgent()
        Currently used HTTP user agent.
        Returns:
        user agent
      • setUserAgent

        public void setUserAgent​(String userAgent)
        Allows setting of user agent.
        Parameters:
        userAgent - user agent
      • isAcceptGzipContentEncoding

        public boolean isAcceptGzipContentEncoding()
        Does this client accept gzip as HTTP content encoding?
        Returns:
        true or false
      • setAcceptGzipContentEncoding

        public void setAcceptGzipContentEncoding​(boolean acceptGzipContentEncoding)
        Set this to true, if you want to tell the server that gzip encoded responses are welcome.
        Parameters:
        acceptGzipContentEncoding - true or false