fedora.client
Class FedoraClient

java.lang.Object
  extended by fedora.client.FedoraClient
All Implemented Interfaces:
Constants

public class FedoraClient
extends java.lang.Object
implements Constants

General-purpose utility class for Fedora clients. Provides methods to get SOAP stubs for Fedora APIs. Also serves as one-stop-shopping for issuing HTTP requests using Apache's HttpClient. Provides option for client to handle HTTP redirects (notably 302 status that occurs with SSL auto-redirects at server.)

Version:
$Id: FedoraClient.java 7540 2008-07-17 14:19:01Z bbranan $
Author:
cwilper@cs.cornell.edu, payette@cs.cornell.edu

Nested Class Summary
 class FedoraClient.SOAPEndpoint
          Class for storing a Fedora SOAP endpoint, which consists of an endpoint name and a URL.
 
Nested classes/interfaces inherited from interface fedora.common.Constants
Constants.FedoraHome
 
Field Summary
static java.lang.String FEDORA_URI_PREFIX
           
 boolean FOLLOW_REDIRECTS
          Whether to automatically follow HTTP redirects.
static boolean FORCE_LOG4J_CONFIGURATION
          Deprecated. 
 int MAX_CONNECTIONS_PER_HOST
          Maxiumum http connections per host (for REST calls only).
 int MAX_TOTAL_CONNECTIONS
          Maxiumum total http connections (for REST calls only).
 int SOCKET_TIMEOUT_SECONDS
          Seconds to wait while waiting for data over the socket (SO_TIMEOUT).
 int TIMEOUT_SECONDS
          Seconds to wait before a connection is established.
 
Fields inherited from interface fedora.common.Constants
ACTION, BDEF, BMECH, DATASTREAM, DC, DISSEMINATOR, ENVIRONMENT, FEDORA, FEDORA_HOME, FEDORA_SYSTEM_DEF_PID, FEDORA_SYSTEM_DEF_URI, HTTP_REQUEST, MODEL, OBJECT, RDF, RECOVERY, RELS_EXT, RESOURCE, SUBJECT, TUCANA, VIEW, XSD
 
Constructor Summary
FedoraClient(java.lang.String baseURL, java.lang.String user, java.lang.String pass)
           
 
Method Summary
 HttpInputStream get(java.lang.String locator, boolean failIfNotOK)
          Get an HTTP resource with the response as an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'.
 HttpInputStream get(java.lang.String locator, boolean failIfNotOK, boolean followRedirects)
          Get an HTTP resource with the response as an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'.
 HttpInputStream get(java.net.URL url, boolean failIfNotOK)
          Get an HTTP resource with the response as an InputStream, given a URL.
 HttpInputStream get(java.net.URL url, boolean failIfNotOK, boolean followRedirects)
          Get an HTTP resource with the response as an InputStream, given a URL.
 FedoraAPIA getAPIA()
          Get a new SOAP stub for API-A.
 java.net.URL getAPIAEndpointURL()
           
 FedoraAPIM getAPIM()
          Get a new SOAP stub for API-M.
 java.net.URL getAPIMEndpointURL()
           
static java.util.List getCompatibleServerVersions()
           
 org.apache.commons.httpclient.HttpClient getHttpClient()
           
 java.util.Date getLastModifiedDate(java.lang.String locator)
           
 java.lang.String getResponseAsString(java.lang.String locator, boolean failIfNotOK, boolean followRedirects)
          Get an HTTP resource with the response as a String instead of an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'.
 java.util.Date getServerDate()
          Return the current date as reported by the Fedora server.
 java.lang.String getServerVersion()
          Get the version reported by the remote Fedora server.
 org.trippi.TupleIterator getTuples(java.util.Map params)
          Get tuples from the remote resource index.
 java.lang.String getUploadURL()
          Get the URL to which API-M upload requests will be sent.
static java.lang.String getVersion()
           
 void reloadPolicies()
           
 java.lang.String uploadFile(java.io.File file)
          Upload the given file to Fedora's upload interface via HTTP POST.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FEDORA_URI_PREFIX

public static final java.lang.String FEDORA_URI_PREFIX
See Also:
Constant Field Values

FORCE_LOG4J_CONFIGURATION

@Deprecated
public static boolean FORCE_LOG4J_CONFIGURATION
Deprecated. 
Should FedoraClient take over log4j configuration?

Deprecated as of Fedora 2.2

FedoraClient no longer takes over Log4J configuration, and setting this value has no effect. Applications that use this class should configure Log4J themselves, or make a log4j.xml or log4j.properties file available from the runtime CLASSPATH.


TIMEOUT_SECONDS

public int TIMEOUT_SECONDS
Seconds to wait before a connection is established.


SOCKET_TIMEOUT_SECONDS

public int SOCKET_TIMEOUT_SECONDS
Seconds to wait while waiting for data over the socket (SO_TIMEOUT).


MAX_CONNECTIONS_PER_HOST

public int MAX_CONNECTIONS_PER_HOST
Maxiumum http connections per host (for REST calls only).


MAX_TOTAL_CONNECTIONS

public int MAX_TOTAL_CONNECTIONS
Maxiumum total http connections (for REST calls only).


FOLLOW_REDIRECTS

public boolean FOLLOW_REDIRECTS
Whether to automatically follow HTTP redirects.

Constructor Detail

FedoraClient

public FedoraClient(java.lang.String baseURL,
                    java.lang.String user,
                    java.lang.String pass)
             throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException
Method Detail

getHttpClient

public org.apache.commons.httpclient.HttpClient getHttpClient()

uploadFile

public java.lang.String uploadFile(java.io.File file)
                            throws java.io.IOException
Upload the given file to Fedora's upload interface via HTTP POST.

Returns:
the temporary id which can then be passed to API-M requests as a URL. It will look like uploaded://123
Throws:
java.io.IOException

getUploadURL

public java.lang.String getUploadURL()
                              throws java.io.IOException
Get the URL to which API-M upload requests will be sent.

Throws:
java.io.IOException

get

public HttpInputStream get(java.lang.String locator,
                           boolean failIfNotOK)
                    throws java.io.IOException
Get an HTTP resource with the response as an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'. This method will follow redirects if FOLLOW_REDIRECTS is true. Note that if the HTTP response has no body, the InputStream will be empty. The success of a request can be checked with getResponseCode(). Usually you'll want to see a 200. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes.

Parameters:
locator - A URL, relative Fedora URL, or Fedora URI that we want to do an HTTP GET upon
failIfNotOK - boolean value indicating if an exception should be thrown if we do NOT receive an HTTP 200 response (OK)
Returns:
HttpInputStream the HTTP response
Throws:
java.io.IOException

get

public HttpInputStream get(java.net.URL url,
                           boolean failIfNotOK)
                    throws java.io.IOException
Get an HTTP resource with the response as an InputStream, given a URL. This method will follow redirects if FOLLOW_REDIRECTS is true. Note that if the HTTP response has no body, the InputStream will be empty. The success of a request can be checked with getResponseCode(). Usually you'll want to see a 200. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes.

Parameters:
url - A URL that we want to do an HTTP GET upon
failIfNotOK - boolean value indicating if an exception should be thrown if we do NOT receive an HTTP 200 response (OK)
Returns:
HttpInputStream the HTTP response
Throws:
java.io.IOException

get

public HttpInputStream get(java.lang.String locator,
                           boolean failIfNotOK,
                           boolean followRedirects)
                    throws java.io.IOException
Get an HTTP resource with the response as an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'. Note that if the HTTP response has no body, the InputStream will be empty. The success of a request can be checked with getResponseCode(). Usually you'll want to see a 200. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes.

Parameters:
locator - A URL, relative Fedora URL, or Fedora URI that we want to do an HTTP GET upon
failIfNotOK - boolean value indicating if an exception should be thrown if we do NOT receive an HTTP 200 response (OK)
followRedirects - boolean value indicating whether HTTP redirects should be handled in this method, or be passed along so that they can be handled later.
Returns:
HttpInputStream the HTTP response
Throws:
java.io.IOException

get

public HttpInputStream get(java.net.URL url,
                           boolean failIfNotOK,
                           boolean followRedirects)
                    throws java.io.IOException
Get an HTTP resource with the response as an InputStream, given a URL. Note that if the HTTP response has no body, the InputStream will be empty. The success of a request can be checked with getResponseCode(). Usually you'll want to see a 200. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes.

Parameters:
url - A URL that we want to do an HTTP GET upon
failIfNotOK - boolean value indicating if an exception should be thrown if we do NOT receive an HTTP 200 response (OK)
followRedirects - boolean value indicating whether HTTP redirects should be handled in this method, or be passed along so that they can be handled later.
Returns:
HttpInputStream the HTTP response
Throws:
java.io.IOException

getResponseAsString

public java.lang.String getResponseAsString(java.lang.String locator,
                                            boolean failIfNotOK,
                                            boolean followRedirects)
                                     throws java.io.IOException
Get an HTTP resource with the response as a String instead of an InputStream, given a resource locator that either begins with 'info:fedora/' , 'http://', or '/'.

Parameters:
locator - A URL, relative Fedora URL, or Fedora URI that we want to do an HTTP GET upon
failIfNotOK - boolean value indicating if an exception should be thrown if we do NOT receive an HTTP 200 response (OK)
followRedirects - boolean value indicating whether HTTP redirects should be handled in this method, or be passed along so that they can be handled later.
Returns:
String the HTTP response as a string
Throws:
java.io.IOException

getAPIA

public FedoraAPIA getAPIA()
                   throws java.lang.Exception
Get a new SOAP stub for API-A. If the baseURL for this FedoraClient specifies "http", regular HTTP communication will be attempted first. If the server redirects this client to use HTTPS instead, the redirect will be followed and SSL will be used automatically.

Throws:
java.lang.Exception

getAPIAEndpointURL

public java.net.URL getAPIAEndpointURL()
                                throws java.io.IOException
Throws:
java.io.IOException

getAPIM

public FedoraAPIM getAPIM()
                   throws java.lang.Exception
Get a new SOAP stub for API-M. If the baseURL for this FedoraClient specifies "http", regular HTTP communication will be attempted first. If the server redirects this client to use HTTPS instead, the redirect will be followed and SSL will be used automatically.

Throws:
java.lang.Exception

getAPIMEndpointURL

public java.net.URL getAPIMEndpointURL()
                                throws java.io.IOException
Throws:
java.io.IOException

getVersion

public static java.lang.String getVersion()

getCompatibleServerVersions

public static java.util.List getCompatibleServerVersions()

getServerVersion

public java.lang.String getServerVersion()
                                  throws java.io.IOException
Get the version reported by the remote Fedora server.

Throws:
java.io.IOException

getServerDate

public java.util.Date getServerDate()
                             throws java.io.IOException
Return the current date as reported by the Fedora server.

Throws:
java.io.IOException - if the HTTP Date header is not provided by the server for any reason, or it is in the wrong format.

getLastModifiedDate

public java.util.Date getLastModifiedDate(java.lang.String locator)
                                   throws java.io.IOException
Throws:
java.io.IOException

reloadPolicies

public void reloadPolicies()
                    throws java.io.IOException
Throws:
java.io.IOException

getTuples

public org.trippi.TupleIterator getTuples(java.util.Map params)
                                   throws java.io.IOException
Get tuples from the remote resource index. The map contains String values for parameters that should be passed to the service. Two parameters are required: 1) lang 2) query Two parameters to the risearch service are implied: 1) type = tuples 2) format = sparql See http://www.fedora.info/download/2.0/userdocs/server/webservices/risearch/#app.tuples

Throws:
java.io.IOException