org.opentox.toxotis.client
Class AbstractClient

java.lang.Object
  extended by org.opentox.toxotis.client.AbstractClient
All Implemented Interfaces:
Closeable, IClient
Direct Known Subclasses:
AbstractHttpClient, AbstractHttpsClient

public abstract class AbstractClient
extends Object
implements IClient

An abstract class providing necessary methods for the implementation of a HTTP client.

Author:
Pantelis Sopasakis, Charalampos Chomenides

Field Summary
protected static int bufferSize
          Size of a buffer used to download the data from the remote server
 
Fields inherited from interface org.opentox.toxotis.client.IClient
httpProtocol, httpSecureProtocol, URL_ENCODING
 
Constructor Summary
AbstractClient()
           
AbstractClient(VRI vri)
           
 
Method Summary
 AbstractClient addHeaderParameter(String paramName, String paramValue)
          Note: if the parameter name (paramName) is either 'Accept' or 'Content-type', this method will override setMediaType and setContentType respectively.
 AbstractClient authorize(AuthenticationToken token)
          Provide an authentication token to the client.
 void close()
          Closes this stream and releases any system resources associated with it.
protected  HttpURLConnection connect(URI uri)
           
protected  HttpURLConnection getConnection()
           
protected  InputStream getConnectionInputStream()
           
 ReentrantReadWriteLock.WriteLock getConnectionLock()
          Returns the connection lock.
protected  Map<String,String> getHeaderValues()
           
 String getMediaType()
          Retrieve the specified media type which is the value for the Accept HTTP Header.
 ReentrantReadWriteLock.ReadLock getReadLock()
          Returns the read-lock of the client.
 InputStream getRemoteStream()
          Get the body of the HTTP response as InputStream.
 int getResponseCode()
          Get the HTTP status of the response
 String getResponseContentType()
          Returns the content-type of the response.
 String getResponseHeader(String header)
          The header of the response.
 com.hp.hpl.jena.ontology.OntModel getResponseOntModel()
          If possible, get the ontological model provided in the response.
 com.hp.hpl.jena.ontology.OntModel getResponseOntModel(String specification)
          If possible, get the ontological model provided in the response.
 String getResponseText()
          Get the response body as a String in the format specified in the Accept header of the request.
 Set<VRI> getResponseUriList()
          Get the response of the remote service as a Set of URIs.
 VRI getUri()
          Get the target URI.
protected abstract  HttpURLConnection initializeConnection(URI uri)
          Initiate a connection to the remote location identified by the provided URI and using the already specified header parameters.
 void setConnection(HttpURLConnection con)
           
 AbstractClient setMediaType(Media mediaType)
          Specify the mediatype to be used in the Accept header providing an instance of Media.
 AbstractClient setMediaType(String mediaType)
          Specify the mediatype to be used in the Accept header.
 AbstractClient setUri(String uri)
          Provide the target URI as a String
 AbstractClient setUri(VRI vri)
          Set the URI on which the GET method is applied.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufferSize

protected static final int bufferSize
Size of a buffer used to download the data from the remote server

See Also:
Constant Field Values
Constructor Detail

AbstractClient

public AbstractClient()

AbstractClient

public AbstractClient(VRI vri)
Method Detail

getConnection

protected HttpURLConnection getConnection()

setConnection

public void setConnection(HttpURLConnection con)

getHeaderValues

protected Map<String,String> getHeaderValues()

getConnectionLock

public ReentrantReadWriteLock.WriteLock getConnectionLock()
Description copied from interface: IClient
Returns the connection lock. While a connection is being established this lock is held by the client. You can monitor it to know exactly when the connection has been established.

Specified by:
getConnectionLock in interface IClient
Returns:
The connection Write Lock.

getReadLock

public ReentrantReadWriteLock.ReadLock getReadLock()
Description copied from interface: IClient
Returns the read-lock of the client. As long as the client reads the response that is returned from the remote location, this lock is being held by the client. Use this lock to know exactly when the client starts and stops reading from the response stream.

Specified by:
getReadLock in interface IClient
Returns:
The read-lock.

getUri

public VRI getUri()
Get the target URI.

Specified by:
getUri in interface IClient
Returns:
The target URI

getMediaType

public String getMediaType()
Retrieve the specified media type which is the value for the Accept HTTP Header.

Specified by:
getMediaType in interface IClient
Returns:
The accepted media type.

addHeaderParameter

public AbstractClient addHeaderParameter(String paramName,
                                         String paramValue)
                                  throws NullPointerException
Note: if the parameter name (paramName) is either 'Accept' or 'Content-type', this method will override setMediaType and setContentType respectively. In general it is not advisable that you choose this method for setting values to these headers. Once the parameter name and its value are submitted to the client, they are encoded using the standard UTF-8 encoding.

Specified by:
addHeaderParameter in interface IClient
Parameters:
paramName - Name of the parameter which will be posted in the header
paramValue - Parameter value
Returns:
This object
Throws:
NullPointerException - If any of the arguments is null

authorize

public AbstractClient authorize(AuthenticationToken token)
Provide an authentication token to the client. This token is given to the remote server to verify the client's identity. The remote service in turn asks the openSSO service whether the underlying request is allowed for the client with the given token. Services have also access to the client's data such as username, name and email which might be stored on the server side to provide accounting facilities.

Specified by:
authorize in interface IClient
Parameters:
token - Authentication token which will be provided in the request's header. Authentication/Authorization follow RFC's guidelines according to which the token is provided using the Header Authorization.
Returns:
This object with an updated header.

initializeConnection

protected abstract HttpURLConnection initializeConnection(URI uri)
                                                   throws ServiceInvocationException
Initiate a connection to the remote location identified by the provided URI and using the already specified header parameters.

Parameters:
uri - The location to which the HTTP connection should be made.
Returns:
An instance of HttpURLConnection that is used to perform the remote HTTP request.
Throws:
ToxOtisException - In case an error status code is received from the remote service or an I/O exception is thrown due to communication problems with the remote server.
ServiceInvocationException

connect

protected HttpURLConnection connect(URI uri)
                             throws ServiceInvocationException
Throws:
ServiceInvocationException

getConnectionInputStream

protected InputStream getConnectionInputStream()
                                        throws ConnectionException
Throws:
ConnectionException

getRemoteStream

public InputStream getRemoteStream()
                            throws ServiceInvocationException
Get the body of the HTTP response as InputStream.

Specified by:
getRemoteStream in interface IClient
Returns:
InputStream for the remote HTTP response
Throws:
ServiceInvocationException - In case an error status code is received from the remote location.
ConnectionException - In case no connection is feasible to the remote resource. Stream cannot open and the generated connection is null
IOException - In case some communication error occurs during the transmission of the data.

getResponseText

public String getResponseText()
                       throws ServiceInvocationException
Get the response body as a String in the format specified in the Accept header of the request.

Specified by:
getResponseText in interface IClient
Returns:
String consisting of the response body (in a MediaType which results from content negotiation, taking into account the Accept header of the request)
Throws:
ToxOtisException - In case some communication, server or request error occurs.
ServiceInvocationException

getResponseOntModel

public com.hp.hpl.jena.ontology.OntModel getResponseOntModel()
                                                      throws ServiceInvocationException
If possible, get the ontological model provided in the response. This assumes that the Accept Header of the request has value application/rdf+xml or at least some other rdf-related MIME (like for example application/x-turtle.

Specified by:
getResponseOntModel in interface IClient
Returns:
The ontological model from the response body.
Throws:
ToxOtisException - A ToxOtisException is thrown in case the server did not provide a valid (syntactically correct) ontological model, or in case some communication error will arise.
ServiceInvocationException

getResponseOntModel

public com.hp.hpl.jena.ontology.OntModel getResponseOntModel(String specification)
                                                      throws ServiceInvocationException
If possible, get the ontological model provided in the response. This assumes that the Accept Header of the request has value application/rdf+xml or at least some other rdf-related MIME (like for example application/x-turtle.

Specified by:
getResponseOntModel in interface IClient
Parameters:
specification - the langauge of the serialization; null selects the default, that is "RDF/XML". Also available: "TTL", "N-TRIPLE" and "N3".
Returns:
The ontological model from the response body.
Throws:
ServiceInvocationException - A ServiceInvocationException is thrown in case the server did not provide a valid (syntactically correct) ontological model, or in case some communication error will arise.

getResponseCode

public int getResponseCode()
                    throws ServiceInvocationException
Get the HTTP status of the response

Specified by:
getResponseCode in interface IClient
Returns:
Response status code.
Throws:
ToxOtisException - In case the connection cannot be established because a ToxOtisException is thrown from the method initializeConnection(URI).
IOException - In case some communication error with the remote location occurs during the transaction of data.
ServiceInvocationException

setMediaType

public AbstractClient setMediaType(String mediaType)
Specify the mediatype to be used in the Accept header.

Specified by:
setMediaType in interface IClient
Parameters:
mediaType - Accepted mediatype
See Also:
RequestHeaders.ACCEPT

setMediaType

public AbstractClient setMediaType(Media mediaType)
Specify the mediatype to be used in the Accept header providing an instance of Media.

Specified by:
setMediaType in interface IClient
Parameters:
mediaType - Accepted mediatype
See Also:
RequestHeaders.ACCEPT

setUri

public AbstractClient setUri(VRI vri)
Set the URI on which the GET method is applied.

Specified by:
setUri in interface IClient
Parameters:
vri - The URI that will be used by the client to perform the remote connection.

setUri

public AbstractClient setUri(String uri)
                      throws URISyntaxException
Provide the target URI as a String

Specified by:
setUri in interface IClient
Parameters:
uri - The target URI as a String.
Throws:
URISyntaxException - In case the provided URI is syntactically incorrect.

close

public void close()
           throws IOException
Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

Specified by:
close in interface Closeable
Specified by:
close in interface IClient
Throws:
IOException - if an I/O error occurs

getResponseUriList

public Set<VRI> getResponseUriList()
                            throws ServiceInvocationException
Get the response of the remote service as a Set of URIs. The media type of the request, as specified by the Accept header is set to text/uri-list.

Specified by:
getResponseUriList in interface IClient
Returns:
Set of URIs returned by the remote service.
Throws:
ToxOtisException - In case some I/O communication error inhibits the transimittance of data between the client and the server or a some stream cannot close.
ServiceInvocationException

getResponseContentType

public String getResponseContentType()
                              throws ServiceInvocationException
Description copied from interface: IClient
Returns the content-type of the response. This is a short-cut for #getResponseHeader("Content-type").

Specified by:
getResponseContentType in interface IClient
Returns:
The content-type of the response.
Throws:
ServiceInvocationException - A ServiceInvocationException is thrown in case this header value is not accessible as for example if the remote server is not reachable.

getResponseHeader

public String getResponseHeader(String header)
                         throws ServiceInvocationException
Description copied from interface: IClient
The header of the response.

Specified by:
getResponseHeader in interface IClient
Parameters:
header - Name of the header.
Returns:
The value of the header as it is specified in the response.
Throws:
ServiceInvocationException - A ServiceInvocationException is thrown is the response headers are not accessible for any reason including, but not limited to, connectivity issues.


Copyright © 2011-2012 OpenTox. All Rights Reserved.