Package com.tagtraum.core
Class WebService
- java.lang.Object
-
- com.tagtraum.core.WebService
-
- Direct Known Subclasses:
AcousticBrainz
,Amazon
,Bandcamp
,Beatport
,CoverartArchive
,DBpedia
,Discogs
,LastFM
,OnlineDB
,Wikidata
public class WebService extends Object
Base class for interaction with web services. Supports proxies, caching, timeouts, SSL etc.- Author:
- Hendrik Schreiber
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
HTTP_METHOD_GET
protected static String
HTTP_METHOD_POST
protected static String
HTTP_METHOD_PUT
protected static org.slf4j.Logger
LOG
-
Constructor Summary
Constructors Constructor Description WebService()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
cacheResource(URL url, byte[] content)
Allows turning caching for a specific resource and its content on and off.void
clearCache()
Removes all items from the cache that are older thanDEFAULT_MAX_AGE
.Cache
getCache()
int
getConnectTimeOut()
DocumentBuilderFactory
getDomFactory()
protected org.json.simple.JSONArray
getJSONArray(String request, Charset responseCharset, long maximumAge, TimeUnit timeUnit)
Utility method to get a JSON array from a webservice.protected org.json.simple.JSONArray
getJSONArray(String request, Charset responseCharset, Map<String,String> parameters)
Utility method to post parameters and get a JSON array result from a webservice.protected org.json.simple.JSONObject
getJSONObject(String request, Charset responseCharset, long maximumAge, TimeUnit timeUnit)
Utility method to get a JSON dictionary from a webservice.protected org.json.simple.JSONObject
getJSONObject(String request, Map<String,String> headers, Charset responseCharset, Map<String,String> parameters)
Utility method to post parameters and get a JSON dictionary result from a webservice.protected org.json.simple.JSONObject
getJSONObjectWithPayload(String request, Map<String,String> headers, Charset responseCharset, org.json.simple.JSONObject payload)
Utility method to post some JSON and get a JSON dictionary result from a webservice.long
getMaxAge()
Returns max age in milliseconds.int
getReadTimeOut()
long
getRequestDelay()
SSLSocketFactory
getSSLSocketFactory()
String
getUserAgent()
Currently used HTTP user agent.protected Document
getXmlDocument(URL url, long maximumAge, TimeUnit timeUnit)
Reads a URL and parses the return document usinggetDomFactory()
.protected <T> T
getXmlObject(String request, long maximumAge, TimeUnit timeUnit, Class<T> klass)
Checks the Cache for the requested URL, issues the request if necessary and parses the answer using JAXB to create an appropriate unmarshalled object.protected void
handleStatusCode(HttpURLConnection httpURLConnection, int statusCode)
Handles standard responses to status codes.boolean
isAcceptGzipContentEncoding()
Does this client acceptgzip
as HTTP content encoding?protected URLConnection
openConnection(URL url)
Opens URLConnection and logs errors.protected URLConnection
openConnection(URL url, String requestMethod, Map<String,String> requestHeaders, InputStream requestBody)
Opens URLConnection and logs errors.protected InputStream
openInputStream(URL url, long maximumAge, TimeUnit timeUnit)
Opens the URL and provides an (cached) InputStream for reading it.protected byte[]
readURL(URL url, long maximumAge, TimeUnit timeUnit)
Opens the URL per HTTP-GET and returns the content as a byte array.void
setAcceptGzipContentEncoding(boolean acceptGzipContentEncoding)
Set this totrue
, if you want to tell the server thatgzip
encoded responses are welcome.void
setCache(Cache cache)
void
setConnectTimeOut(int connectTimeOut)
void
setDomFactory(DocumentBuilderFactory domFactory)
void
setMaxAge(long maxAge)
Sets max age in milliseconds.protected void
setNextAllowedConnection(long nextTimestamp)
void
setReadTimeOut(int readTimeOut)
void
setRequestDelay(long requestDelay)
void
setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
void
setUserAgent(String userAgent)
Allows setting of user agent.protected static String
toParameterString(Map<String,String> parameters, boolean startWithQuestionMark)
protected boolean
useCacheOnly(URL url)
Allows to suppress the actual request.protected void
waitForTurn()
Waits until this thread is allowed to send a request to the webservice.
-
-
-
Field Detail
-
LOG
protected static final org.slf4j.Logger LOG
-
HTTP_METHOD_POST
protected static final String HTTP_METHOD_POST
- See Also:
- Constant Field Values
-
HTTP_METHOD_GET
protected static final String HTTP_METHOD_GET
- See Also:
- Constant Field Values
-
HTTP_METHOD_PUT
protected static final String HTTP_METHOD_PUT
- See Also:
- Constant Field Values
-
-
Method Detail
-
toParameterString
protected static String toParameterString(Map<String,String> parameters, boolean startWithQuestionMark)
-
getDomFactory
public DocumentBuilderFactory getDomFactory()
-
setDomFactory
public void setDomFactory(DocumentBuilderFactory domFactory)
-
clearCache
public void clearCache()
Removes all items from the cache that are older thanDEFAULT_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()
-
getSSLSocketFactory
public SSLSocketFactory getSSLSocketFactory()
-
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
- URLmaximumAge
- max agetimeUnit
- max age time unitklass
- class for the object to parse using JAXB- Returns:
- XmlObject
- Throws:
IOException
javax.xml.bind.DataBindingException
-
getXmlDocument
protected Document getXmlDocument(URL url, long maximumAge, TimeUnit timeUnit) throws SAXException, ParserConfigurationException, IOException
Reads a URL and parses the return document usinggetDomFactory()
.- Parameters:
url
- urlmaximumAge
- max age for cachetimeUnit
- time unit for maximum age- Returns:
- XML document
- Throws:
SAXException
- if parsing failsParserConfigurationException
- if we can't create a parserIOException
- if something on the IO level goes wrong
-
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 URLresponseCharset
- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredmaximumAge
- up to this age the cache is consultedtimeUnit
- max age time unit- Returns:
- JSON array
- Throws:
IOException
- if something goes wrongorg.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 URLresponseCharset
- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredmaximumAge
- up to this age the cache is consultedtimeUnit
- max age time unit- Returns:
- JSON dictionary
- Throws:
IOException
- if something goes wrongorg.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 URLresponseCharset
- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredparameters
- parameters to send- Returns:
- JSON array
- Throws:
IOException
- if something goes wrongorg.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 URLresponseCharset
- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredparameters
- parameters to send- Returns:
- JSON dictionary
- Throws:
IOException
- if something goes wrongorg.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 URLresponseCharset
- character set to use when decoding the response, i.e. the character set communicated in the corresponding HTTP header is ignoredpayload
- JSON request payload- Returns:
- JSON dictionary
- Throws:
IOException
- if something goes wrongorg.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 readmaximumAge
- max agetimeUnit
- 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 notgetRequestDelay()
is honored.- Parameters:
url
- url to readmaximumAge
- max agetimeUnit
- 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
- urlcontent
- resource content- Returns:
- by default this method returns
true
-
setNextAllowedConnection
protected void setNextAllowedConnection(long nextTimestamp)
-
openConnection
protected URLConnection openConnection(URL url) throws IOException
Opens URLConnection and logs errors.- Parameters:
url
- URL to connect to- Returns:
- opened URL connection
- Throws:
IOException
- See Also:
openConnection(URL, String, Map, InputStream)
-
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 torequestMethod
- HTTP method, defaults to GETrequestHeaders
- optional headers to sent with the requestrequestBody
- 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 connectionstatusCode
- status code- Throws:
IOException
-
waitForTurn
protected void waitForTurn() throws IOException
Waits until this thread is allowed to send a request to the webservice. Contrary toprivateWaitForTurn()
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 acceptgzip
as HTTP content encoding?- Returns:
true
orfalse
-
setAcceptGzipContentEncoding
public void setAcceptGzipContentEncoding(boolean acceptGzipContentEncoding)
Set this totrue
, if you want to tell the server thatgzip
encoded responses are welcome.- Parameters:
acceptGzipContentEncoding
-true
orfalse
-
-