Class ClientUtil


  • public class ClientUtil
    extends java.lang.Object
    Utility functions (static public methods) used in Rest Clients.
    Since:
    JCMS-6.0
    Author:
    dissert
    • Constructor Summary

      Constructors 
      Constructor Description
      ClientUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addHttpHeader​(org.restlet.data.Request request, java.lang.String key, java.lang.String value)
      Adds a header in a HTTP request represented by the request parameter.
      static boolean getBoolean​(JcmsResource resource, java.lang.String xpathExpression, boolean def)
      If the result of the XPath query is a boolean String, it is converted in Boolean.
      static java.util.Date getDate​(JcmsResource resource, java.lang.String xpathExpression, java.util.Date def)
      If the result of the XPath query is a Date as defined by the W3C Date Time format (eg 1999-01-01T14:07:00+01:00).
      For more details, see http://www.w3.org/TR/NOTE-datetime
      , it is converted this Date.
      static org.jdom.Element getFirstElement​(JcmsResource resource, java.lang.String xpathExpression)
      Utility functions to use XPath expression to introspect xml contained in entity boby of HTTP response
      static long getLong​(JcmsResource resource, java.lang.String xpathExpression, long def)
      If the result of the XPath query is a long representation String, it is converted as an long.
      static java.util.List<?> getNodeList​(JcmsResource resource, java.lang.String xpathExpression)
      Provide XPath feature : returns the list of Nodes in the xml in the entity body of the resource in first argument, with the xPathExpression in second argument.
      static org.restlet.data.Preference<org.restlet.data.MediaType> getPreferenceMediaType​(org.restlet.data.MediaType mediaType)  
      static org.restlet.data.Request getRequest​(org.restlet.data.Reference resourceRef, org.restlet.data.Method method)
      Instanciate the request with the uri and the HTTP method.
      static java.lang.Object getSingleNode​(JcmsResource resource, java.lang.String xpathExpression)
      Provide XPath feature : returns the first of the Nodes in the xml in the entity body of the resource in first argument, with the xPathExpression in second argument.
      static java.lang.String getString​(JcmsResource resource, java.lang.String xpathExpression)
      Return the most pertinent String for the result of the XPath query in the body content of the given resource.
      static java.lang.String getString​(JcmsResource resource, java.lang.String xpathExpression, java.lang.String def)
      Return the most pertinent String for the result of the XPath query in the body content of the given resource.
      static void setPreferedMediaType​(org.restlet.data.Request request, org.restlet.data.MediaType[] mediaTypes)
      In a Restlet Request object, set the prefered MediaType
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ClientUtil

        public ClientUtil()
    • Method Detail

      • addHttpHeader

        public static void addHttpHeader​(org.restlet.data.Request request,
                                         java.lang.String key,
                                         java.lang.String value)
        Adds a header in a HTTP request represented by the request parameter.
        Parameters:
        request - The Restlet object representing an HTTP request.
        key - The name of the given HTTP header
        value - The name for the given HTTP header
      • getRequest

        public static org.restlet.data.Request getRequest​(org.restlet.data.Reference resourceRef,
                                                          org.restlet.data.Method method)
        Instanciate the request with the uri and the HTTP method.
        Parameters:
        resourceRef -
        method -
        Returns:
        the Request object, with the URI and the HTTP method.
      • setPreferedMediaType

        public static void setPreferedMediaType​(org.restlet.data.Request request,
                                                org.restlet.data.MediaType[] mediaTypes)
        In a Restlet Request object, set the prefered MediaType
        Parameters:
        request - The Restlet Request object
        mediaTypes - The prefered media type
      • getPreferenceMediaType

        public static org.restlet.data.Preference<org.restlet.data.MediaType> getPreferenceMediaType​(org.restlet.data.MediaType mediaType)
      • getFirstElement

        public static org.jdom.Element getFirstElement​(JcmsResource resource,
                                                       java.lang.String xpathExpression)
                                                throws org.jdom.JDOMException
        Utility functions to use XPath expression to introspect xml contained in entity boby of HTTP response
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        Returns:
        the first element if the node as a result of the XPath xpression is an element
        Throws:
        org.jdom.JDOMException - if the XPath expression is invalid or its evaluation on the specified context failed.
      • getSingleNode

        public static java.lang.Object getSingleNode​(JcmsResource resource,
                                                     java.lang.String xpathExpression)
                                              throws org.jdom.JDOMException
        Provide XPath feature : returns the first of the Nodes in the xml in the entity body of the resource in first argument, with the xPathExpression in second argument.
        Parameters:
        resource - the search is done into the entity body of this resource
        xpathExpression - the XPath expression
        Returns:
        the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
        Throws:
        org.jdom.JDOMException - if the XPath expression is invalid or its evaluation on the specified context failed.
      • getNodeList

        public static java.util.List<?> getNodeList​(JcmsResource resource,
                                                    java.lang.String xpathExpression)
                                             throws org.jdom.JDOMException
        Provide XPath feature : returns the list of Nodes in the xml in the entity body of the resource in first argument, with the xPathExpression in second argument.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - the XPath expression
        Returns:
        the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
        Throws:
        org.jdom.JDOMException - if the XPath expression is invalid or its evaluation on the specified context failed.
      • getString

        public static java.lang.String getString​(JcmsResource resource,
                                                 java.lang.String xpathExpression)
                                          throws org.jdom.JDOMException
        Return the most pertinent String for the result of the XPath query in the body content of the given resource.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        Returns:
        the String found or null
        Throws:
        org.jdom.JDOMException - if the XPath expression is invalid or its evaluation on the specified context failed.
      • getString

        public static java.lang.String getString​(JcmsResource resource,
                                                 java.lang.String xpathExpression,
                                                 java.lang.String def)
        Return the most pertinent String for the result of the XPath query in the body content of the given resource. Otherwise, the default value is returned.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        def - the default value
        Returns:
        the String found or def
        Since:
        jcms-6.1.2
      • getBoolean

        public static boolean getBoolean​(JcmsResource resource,
                                         java.lang.String xpathExpression,
                                         boolean def)
        If the result of the XPath query is a boolean String, it is converted in Boolean. Otherwise, the default value is returned.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        def - the default value
        Returns:
        the value associated to the string found or def.
      • getDate

        public static java.util.Date getDate​(JcmsResource resource,
                                             java.lang.String xpathExpression,
                                             java.util.Date def)
        If the result of the XPath query is a Date as defined by the W3C Date Time format (eg 1999-01-01T14:07:00+01:00).
        For more details, see http://www.w3.org/TR/NOTE-datetime
        , it is converted this Date. Otherwise, the default value is returned.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        def - the default value
        Returns:
        the value associated to the string found or def.
      • getLong

        public static long getLong​(JcmsResource resource,
                                   java.lang.String xpathExpression,
                                   long def)
        If the result of the XPath query is a long representation String, it is converted as an long. Otherwise, the default value is returned.
        Parameters:
        resource - the given resource get by a rest request
        xpathExpression - an xpath expression to dig into the body content of the request
        def - the default value
        Returns:
        the value associated to the string found or def.