Class MetacatClient

java.lang.Object
edu.ucsb.nceas.metacat.client.MetacatClient
All Implemented Interfaces:
Metacat

public class MetacatClient extends Object implements Metacat
This interface provides methods for initializing and logging in to a Metacat server, and then querying, reading, transforming, inserting, updating and deleting documents from that server.
  • Constructor Details

    • MetacatClient

      protected MetacatClient()
      Constructor to create a new instance. Protected because instances should only be created by the factory MetacatFactory.
  • Method Details

    • main

      public static void main(String[] args)
    • login

      public String login(String username, String password) throws MetacatAuthException, MetacatInaccessibleException
      Method used to log in to a metacat server. Implementations will need to cache a cookie value to make the session persistent. Each time a call is made to one of the other methods (e.g., read), the cookie will need to be passed back to the metacat server along with the request.
      Specified by:
      login in interface Metacat
      Parameters:
      username - the username of the user, like an LDAP DN
      password - the password for that user for authentication
      Returns:
      the response string from metacat in XML format
      Throws:
      MetacatAuthException - when the username/password could not be authenticated
      MetacatInaccessibleException
    • getloggedinuserinfo

      public String getloggedinuserinfo() throws MetacatInaccessibleException
      Method used to log in to a metacat server. Implementations will need to cache a cookie value to make the session persistent. Each time a call is made to one of the other methods (e.g., read), the cookie will need to be passed back to the metacat server along with the request.
      Specified by:
      getloggedinuserinfo in interface Metacat
      Parameters:
      username - the username of the user, like an LDAP DN
      password - the password for that user for authentication
      Returns:
      the response string from metacat in XML format
      Throws:
      MetacatAuthException - when the username/password could not be authenticated
      MetacatInaccessibleException
    • logout

      Method used to log out a metacat server. The Metacat server will end the session when this call is invoked.
      Specified by:
      logout in interface Metacat
      Returns:
      the response string from metacat in XML format
      Throws:
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException
    • validateSession

      public String validateSession(String sessionId) throws MetacatAuthException, MetacatInaccessibleException
      Method used to log in to a metacat server. Implementations will need to cache a cookie value to make the session persistent. Each time a call is made to one of the other methods (e.g., read), the cookie will need to be passed back to the metacat server along with the request.
      Parameters:
      username - the username of the user, like an LDAP DN
      password - the password for that user for authentication
      Returns:
      the response string from metacat in XML format
      Throws:
      MetacatAuthException - when the username/password could not be authenticated
      MetacatInaccessibleException
    • isAuthorized

      public String isAuthorized(String resourceLsid, String permission, String sessionId) throws MetacatAuthException, MetacatInaccessibleException
      Method used to log in to a metacat server. Implementations will need to cache a cookie value to make the session persistent. Each time a call is made to one of the other methods (e.g., read), the cookie will need to be passed back to the metacat server along with the request.
      Parameters:
      username - the username of the user, like an LDAP DN
      password - the password for that user for authentication
      Returns:
      the response string from metacat in XML format
      Throws:
      MetacatAuthException - when the username/password could not be authenticated
      MetacatInaccessibleException
    • read

      Read an XML document from the metacat server session, accessed by docid, and returned as a Reader.
      Specified by:
      read in interface Metacat
      Parameters:
      docid - the identifier of the document to be read
      Returns:
      a Reader for accessing the document
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
      DocumentNotFoundException
    • readInlineData

      Read inline data from the metacat server session, accessed by inlinedataid and returned as a Reader.
      Specified by:
      readInlineData in interface Metacat
      Parameters:
      inlinedataid - the identifier of the data to be read
      Returns:
      a Reader for accessing the document
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
    • query

      public Reader query(Reader xmlQuery) throws MetacatInaccessibleException, IOException
      Query the metacat document store with the given metacat-compatible query document and default qformat xml, and return the result set as a Reader.
      Specified by:
      query in interface Metacat
      Parameters:
      xmlQuery - a Reader for accessing the XML version of the query
      Returns:
      a Reader for accessing the result set
      Throws:
      MetacatInaccessibleException
      IOException
    • query

      public Reader query(Reader xmlQuery, String qformat) throws MetacatInaccessibleException, IOException
      Query the metacat document store with the given metacat-compatible query document and qformat, and return the result set as a Reader.
      Specified by:
      query in interface Metacat
      Parameters:
      xmlQuery - a Reader for accessing the XML version of the query
      qformat - the format of return doc. It can be xml, knb, lter and etal.
      Returns:
      a Reader for accessing the result set
      Throws:
      MetacatInaccessibleException
      IOException
    • insert

      Insert an XML document into the repository.
      Specified by:
      insert in interface Metacat
      Parameters:
      docid - the docid to insert the document
      xmlDocument - a Reader for accessing the XML document to be inserted
      schema - a Reader for accessing the DTD or XML Schema for the document
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
      IOException - when there is an error reading the xml document
    • update

      Update an XML document in the repository.
      Specified by:
      update in interface Metacat
      Parameters:
      docid - the docid to update
      xmlDocument - a Reader for accessing the XML text to be updated
      schema - a Reader for accessing the DTD or XML Schema for the document
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
      IOException - when there is an error reading the xml document
    • upload

      Upload a data document into the repository. Data files are stored on metacat and may be in any format (binary or text), but they are all treated as if they were binary. Data files are not searched by the query() methods because they are not loaded into the XML store because they are not XML documents. The File parameter is used to determine a name for the uploaded document.
      Specified by:
      upload in interface Metacat
      Parameters:
      docid - the identifier to be used for the document
      file - the File to be uploaded
      document - a InputStream containing the data to be uploaded
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
      IOException - when there is an error reading the xml document
    • upload

      public String upload(String docid, String filename, InputStream fileData, int size) throws InsufficientKarmaException, MetacatException, IOException, MetacatInaccessibleException
      Upload a data document into the repository. Data files are stored on metacat and may be in any format (binary or text), but they are all treated as if they were binary. Data files are not searched by the query() methods because they are not loaded into the XML store because they are not XML documents. The name for the document is set explicitly using the filename parameter.
      Specified by:
      upload in interface Metacat
      Parameters:
      docid - the identifier to be used for the document
      filename - the name to be used in the MIME description of the uploaded file
      document - a InputStream containing the data to be uploaded
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
      IOException - when there is an error reading the xml document
    • delete

      Delete an XML document in the repository.
      Specified by:
      delete in interface Metacat
      Parameters:
      docid - the docid to delete
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
    • getAccessControl

      get the access control info for a given document id.
      Specified by:
      getAccessControl in interface Metacat
      Parameters:
      _docid - the docid of the document for which the access should be applied.
      Returns:
      the metacat access xml
      Throws:
      InsufficientKarmaException
      MetacatException
      MetacatInaccessibleException
    • setAccess

      public String setAccess(String docid, String principal, String permission, String permType, String permOrder) throws InsufficientKarmaException, MetacatException, MetacatInaccessibleException
      set the access on an XML document in the repository.
      Specified by:
      setAccess in interface Metacat
      Parameters:
      _docid - the docid of the document for which the access should be applied.
      _principal - the document's principal
      _permission - the access permission to be applied to the docid {e.g. read,write,all}
      _permType - the permission type to be applied to the document {e.g. allow or deny}
      _permOrder - the order that the document's permissions should be processed {e.g. denyFirst or allowFirst}
      Returns:
      the metacat response message
      Throws:
      InsufficientKarmaException - when the user has insufficent rights for the operation
      MetacatInaccessibleException - when the metacat server can not be reached or does not respond
      MetacatException - when the metacat server generates another error
    • setAccess

      Set access for a given doc id. The access is represented in an access block of xml. All existing access will be replaced with the access provided in the access block.
      Specified by:
      setAccess in interface Metacat
      Parameters:
      docid - the doc id for the doc we want to update
      accessBlock - the xml access block. This is the same structure as that returned by the getdocumentinfo action in metacat.
      Returns:
      a string holding the response xml
      Throws:
      InsufficientKarmaException
      MetacatException
      MetacatInaccessibleException
    • setMetacatUrl

      public void setMetacatUrl(String metacatUrl)
      When the MetacatFactory creates an instance it needs to set the MetacatUrl to which connections should be made.
      Specified by:
      setMetacatUrl in interface Metacat
      Parameters:
      metacatUrl - the URL for the metacat server
    • getSessionId

      public String getSessionId()
      Get the session identifier for this session. This is only valid if the login methods has been called successfully for this Metacat object beforehand.
      Specified by:
      getSessionId in interface Metacat
    • setSessionId

      public void setSessionId(String sessionId)
      Set the session identifier for this session. This identifier was previously established with a call to login. To continue to use the same session, set the session id before making a call to one of the metacat access methods (e.g., read, query, insert, etc.).
      Specified by:
      setSessionId in interface Metacat
      Parameters:
      String - the sessionId from a previously established session
    • getNewestDocRevision

      public int getNewestDocRevision(String docId) throws MetacatException
      The method will return the latest revision in metacat server for a given document id. If some error happens, this method will throw an exception.
      Specified by:
      getNewestDocRevision in interface Metacat
      Parameters:
      docId - String the given docid you want to use. the docid it self can have or haven't revision number
      Throws:
      MetacatException
    • getLastDocid

      public String getLastDocid(String scope) throws MetacatException
      Return the highest document id for a given scope. This is used by clients to make it easier to determine the next free identifier in a sequence for a given scope.
      Specified by:
      getLastDocid in interface Metacat
      Parameters:
      scope - String the scope to use for looking up the latest id
      Throws:
      MetacatException - when an error occurs
    • getAllDocids

      public Vector getAllDocids(String scope) throws MetacatException
      return a list of all docids that match a given scope. if scope is null return all docids registered in the system
      Specified by:
      getAllDocids in interface Metacat
      Parameters:
      scope - String the scope to use to limit the docid query
      Throws:
      MetacatException - when an error occurs
    • isRegistered

      public boolean isRegistered(String docid) throws MetacatException
      return true of the given docid is registered, false if not
      Specified by:
      isRegistered in interface Metacat
      Parameters:
      scope - String the scope to use to limit the docid query
      Throws:
      MetacatException - when an error occurs
    • sendParameters

      public InputStream sendParameters(Properties prop) throws Exception
      Send a request to Metacat. An alternative to the sentData method. Allows for sending multiple parameters with the same name, different names, or any combo. Send properties where the entry key contains the "param key", and the entry value contains the "param value". Constraint: no multi-valued parameters are supported
      Parameters:
      args - Properties of the parameters to be sent to Metacat, where, key = param key value = param value
      Returns:
      InputStream as returned by Metacat
      Throws:
      Exception - thrown
    • sendParametersInverted

      public InputStream sendParametersInverted(Properties prop) throws Exception
      Send a request to Metacat. NOTE: Send properties where the entry key contains the "param value", and the entry value contains the "param name". Constraint: param values must be unique.
      Parameters:
      args - Properties of the parameters to be sent to Metacat, where, key = param value value = param name
      Returns:
      InputStream as returned by Metacat
      Throws:
      Exception - thrown
    • getMetacatClient

      public static MetacatClient getMetacatClient(javax.servlet.http.HttpServletRequest request) throws MetacatInaccessibleException
      JSP API: This is a convenience method to reduce the amount of code in a Metacat Client JSP. It handles creating/reusing an instance of a MetacatClient.
      Parameters:
      request - Since this is intended to be used by a JSP, it is passed the available "request" variable (the HttpServletRequest).
      Returns:
      MetacatClient instance.
      Throws:
      MetacatInaccessibleException - Received by MetacatFactory.
    • getEncoding

      public String getEncoding()
      Description copied from interface: Metacat
      Returns the character encoding used used when communicating with Metacat.
      Specified by:
      getEncoding in interface Metacat
      Returns:
      character encoding name
    • setEncoding

      public void setEncoding(String encoding)
      Description copied from interface: Metacat
      Returns the character encoding used used when communicating with Metacat.
      Specified by:
      setEncoding in interface Metacat
      Parameters:
      encoding - The encoding (i.e. "UTF-8")