org.opentox.toxotis.client.http
Class PostHttpClient

java.lang.Object
  extended by org.opentox.toxotis.client.AbstractClient
      extended by org.opentox.toxotis.client.http.AbstractHttpClient
          extended by org.opentox.toxotis.client.http.PostHttpClient
All Implemented Interfaces:
Closeable, IClient, IPostClient

public class PostHttpClient
extends AbstractHttpClient
implements IPostClient

A client used to perform POST operations. It is used to perform POST requests in a configurable way allowing users to specify the POSTed object and the various header parameters.

Author:
Pantelis Sopasakis, Charalampos Chomenides

Field Summary
 
Fields inherited from class org.opentox.toxotis.client.AbstractClient
bufferSize
 
Fields inherited from interface org.opentox.toxotis.client.IPostClient
METHOD
 
Fields inherited from interface org.opentox.toxotis.client.IClient
httpProtocol, httpSecureProtocol, URL_ENCODING
 
Constructor Summary
PostHttpClient()
           
PostHttpClient(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.
 PostHttpClient addPostParameter(String paramName, String paramValue)
          Add a parameter which will be posted to the target URI.
 String getContentType()
           
 ReentrantReadWriteLock.WriteLock getPostLock()
           
protected  HttpURLConnection initializeConnection(URI uri)
          Initialize a connection to the target URI
 void post()
          According to the the configuration of the PostHttpClient, performs a remote POST request to the server identified by the URI provided in the constructor.
 PostHttpClient setContentType(Media media)
          Specify the Content-type header value that is the MIME of the POSTed entity.
 PostHttpClient setContentType(String contentType)
          Specify the Content-type header value that is the MIME of the POSTed entity.
 PostHttpClient setPostable(File objectToPost)
          Set a file whose contents are to be posted to the remote server specified in the constructor of this class.
 IPostClient setPostable(InputStream inputStream)
           
 PostHttpClient setPostable(IStAXWritable staxWritable)
          Set a StAX-writeable component to be posted to the remote location
 PostHttpClient setPostable(com.hp.hpl.jena.ontology.OntModel model)
          Set an ontological data model which is to be posted to the remote location as application/rdf+xml.
 PostHttpClient setPostable(String string, boolean binary)
          Provide a POSTable object as a string.
 
Methods inherited from class org.opentox.toxotis.client.AbstractClient
authorize, close, connect, getConnection, getConnectionInputStream, getConnectionLock, getHeaderValues, getMediaType, getReadLock, getRemoteStream, getResponseCode, getResponseContentType, getResponseHeader, getResponseOntModel, getResponseOntModel, getResponseText, getResponseUriList, getUri, setConnection, setMediaType, setMediaType, setUri, setUri
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.opentox.toxotis.client.IClient
authorize, close, getConnectionLock, getMediaType, getReadLock, getRemoteStream, getResponseCode, getResponseContentType, getResponseHeader, getResponseOntModel, getResponseOntModel, getResponseText, getResponseUriList, getUri, setMediaType, setMediaType, setUri, setUri
 

Constructor Detail

PostHttpClient

public PostHttpClient()

PostHttpClient

public PostHttpClient(VRI vri)
Method Detail

getContentType

public String getContentType()
Specified by:
getContentType in interface IPostClient

setContentType

public PostHttpClient setContentType(String contentType)
Description copied from interface: IPostClient
Specify the Content-type header value that is the MIME of the POSTed entity. Alternatively users can invoke IClient.addHeaderParameter(java.lang.String, java.lang.String) using the Header RequestHeaders.CONTENT_TYPE.

Specified by:
setContentType in interface IPostClient
Parameters:
contentType - The MIME type corresponding to the Content-type value for the underlying request as a String.
Returns:
The current updated modifiable POST client.

setContentType

public PostHttpClient setContentType(Media media)
Description copied from interface: IPostClient
Specify the Content-type header value that is the MIME of the POSTed entity. Alternatively users can invoke IClient.addHeaderParameter(java.lang.String, java.lang.String) using the Header RequestHeaders.CONTENT_TYPE.

Specified by:
setContentType in interface IPostClient
Parameters:
media - The MIME type corresponding to the Content-type value for the underlying request as an instance of Media.
Returns:
The current updated modifiable POST client.

setPostable

public PostHttpClient setPostable(com.hp.hpl.jena.ontology.OntModel model)
Set an ontological data model which is to be posted to the remote location as application/rdf+xml. Invocations of this method set automatically the content-type to application/rdf+xml though it can be overridden afterwards.

Specified by:
setPostable in interface IPostClient
Parameters:
model - Ontological Model to be posted
Returns:
The PostHttpClient with the updated Ontological Model.

setPostable

public PostHttpClient setPostable(IStAXWritable staxWritable)
Set a StAX-writeable component to be posted to the remote location

Specified by:
setPostable in interface IPostClient
Parameters:
staxWritable - A StAX component that implements the interface IStAXWritable that will be posted to the remote server via the method write(OutputStream) that writes the component to an outputstream pointing to the remote stream
Returns:
The PostHttpClient with the updated writeable component.

setPostable

public PostHttpClient setPostable(File objectToPost)
Set a file whose contents are to be posted to the remote server specified in the constructor of this class. If the file is not found under the specified path, an IllegalArgumentException is thrown. Because the type of the file is in general unknown and it is not considered to be a good practise to deduce the file type from the file extension, it is up to the user to specify the content type of the posted object using the method setContentType. Since it is not possible to POST entities of different content types to an HTTP server, any invokation to this method will override any previous invokation of setPostable(OntModel) and setPostable(String).

Specified by:
setPostable in interface IPostClient
Parameters:
objectToPost - File whose contents are to be posted.
Returns:
This post client
Throws:
IllegalArgumentException - In case the provided file does not exist

setPostable

public PostHttpClient setPostable(String string,
                                  boolean binary)
Description copied from interface: IPostClient
Provide a POSTable object as a string. Keep in mind that the this string will not be URL-Encoded or by any means modified prior to the POST operation. It is also up to the user to specify a proper Content-type.

Specified by:
setPostable in interface IPostClient
Parameters:
string - String representation of an entity to be posted to a remote server.
binary - Specifies whether the data should be posted in binary format, i.e. as a sequence of bytes or as a character sequence.
Returns:
This post client with an updated value of the postable object.
See Also:
Collection of Media Types

addPostParameter

public PostHttpClient addPostParameter(String paramName,
                                       String paramValue)
Add a parameter which will be posted to the target URI. Once the parameter is submitted to the PostHttpClient, it is stored as URL-encoded using the UTF-8 encoding.

Specified by:
addPostParameter in interface IPostClient
Parameters:
paramName - Parameter name
paramValue - Parameter value
Returns:
This object
Throws:
NullPointerException - If paramName is null.

addHeaderParameter

public AbstractClient addHeaderParameter(String paramName,
                                         String paramValue)
Description copied from class: AbstractClient
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
Overrides:
addHeaderParameter in class AbstractClient
Parameters:
paramName - Name of the parameter which will be posted in the header
paramValue - Parameter value
Returns:
This object

initializeConnection

protected HttpURLConnection initializeConnection(URI uri)
                                          throws InternalServerError,
                                                 ConnectionException
Initialize a connection to the target URI

Specified by:
initializeConnection in class AbstractClient
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:
InternalServerError
ConnectionException

post

public void post()
          throws ServiceInvocationException
According to the the configuration of the PostHttpClient, performs a remote POST request to the server identified by the URI provided in the constructor. First, the protected method initializeConnection(URI) is invoked and then a DataOutputStream opens to transfer the data to the server.

Specified by:
post in interface IPostClient
Throws:
ToxOtisException - Encapsulates an IOException which might be thrown due to I/O errors during the data transaction.
ServiceInvocationException

getPostLock

public ReentrantReadWriteLock.WriteLock getPostLock()
Specified by:
getPostLock in interface IPostClient

setPostable

public IPostClient setPostable(InputStream inputStream)
Specified by:
setPostable in interface IPostClient


Copyright © 2011-2012 OpenTox. All Rights Reserved.