Class RestUtil


  • public class RestUtil
    extends java.lang.Object
    Provides utility static methods for both client and server side in REST communication.
    Since:
    JCMS-6.0
    Author:
    dissert
    • Constructor Summary

      Constructors 
      Constructor Description
      RestUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void displayResponse​(org.restlet.data.Response response)
      Dev method to print in syso the content of a response
      static java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatMsgToJson​(javax.servlet.http.HttpServletRequest request)
      Get the info, warning and error messages in the request and in the session and returns a json with all this informations.
      static java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatMsgToJson​(javax.servlet.http.HttpServletRequest request, boolean displayInfo, boolean displayWarning, boolean displayError)
      Get the info, warning and error messages in the request and in the session and returns a Map with all this informations.
      static java.lang.String formatMsgToXml​(javax.servlet.http.HttpServletRequest request)
      Get the info, warning and error messages in the request and in the session and returns a xml with all this informations.
      static java.lang.String formatMsgToXml​(javax.servlet.http.HttpServletRequest request, boolean displayInfo, boolean displayWarning, boolean displayError)
      Get the info, warning and error messages in the request and in the session and returns a xml with all this informations.
      static java.lang.String formatXmlEntityError​(java.lang.String code, java.lang.String message)
      Print an message in the following format (without indentation) :
      static Data getData​(java.lang.String dataId, Member mbr, boolean throwExceptionOnInvalidData)
      Retrieve Data instance (if mbr can read it)
      static java.util.Date getDate​(java.lang.String date)  
      static java.lang.String getExtension​(org.restlet.data.MediaType mediaType)
      Gives an possible extension from a mime-type.
      static org.restlet.data.Form getForm​(java.lang.String queryString)
      Gives a form representation of the parameters in the queryString.
      static org.restlet.data.Form getForm​(org.restlet.resource.Representation entity)
      Gives a form representation of the parameters in the entity-body.
      static java.util.LinkedHashMap<java.lang.String,​java.util.List<java.lang.String>> getFormParameters​(org.restlet.resource.Representation entity, javax.servlet.http.HttpServletRequest j2eeRequest)
      Read form parameters from a rest entity (as json entity, application/x-www-form-urlencoded or multipart-formdata entity)
      static Group getGroup​(java.lang.String grpStr, Member mbr, boolean throwExceptionOnInvalidData)
      Retrieve Group from a string (by testing id, ldap dn and name)
      static Member getMember​(java.lang.String mbrStr, boolean throwExceptionOnInvalidData)
      Retrieve Member from a string (by testing id, login and email)
      static org.restlet.data.Reference getReferenceFromUriSuffix​(java.lang.String uriSuffix, javax.servlet.http.HttpServletRequest j2eeRequest)
      Gives the complete URL from an external point of vue, from an uri with no context-path and "rest" prefix
      static org.restlet.data.MediaType getRestContentType​(javax.servlet.http.HttpServletRequest request)
      Simple implementation of the feature : is application/json in accept headers ? application_JSON : application_XML
      static Workspace getWorkspace​(java.lang.String wkStr, boolean throwExceptionOnInvalidData)
      Retrieve Workspace from a string (by testing id and title)
      static boolean isRest​(javax.servlet.http.HttpServletRequest request)  
      static void sendApplicationError​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String contentType)
      Get the messages in request or in session and put it formatted in the entity body of the response and set CLIENT_ERROR_BAD_REQUEST (400) HTTP error code.
      static void sendApplicationError​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.restlet.data.MediaType contentType)
      Get the messages in request or in session and put it formatted in the entity body of the response and set CLIENT_ERROR_BAD_REQUEST (400) HTTP error code.
      static void sendDetailedError​(javax.servlet.http.HttpServletResponse response, int errorCode, java.lang.String applicationErrorCode, java.lang.String applicationErrorMessage)
      Deprecated.
      use sendDetailedError(HttpServletResponse, int, String, String, MediaType) which allows the correct media type to be specified
      static void sendDetailedError​(javax.servlet.http.HttpServletResponse response, int errorCode, java.lang.String applicationErrorCode, java.lang.String applicationErrorMessage, java.lang.String contentType)
      This method is here for ascendant compatibility, please use the method with MediaType parameter instead.
      static void sendDetailedError​(javax.servlet.http.HttpServletResponse response, int errorCode, java.lang.String applicationErrorCode, java.lang.String applicationErrorMessage, org.restlet.data.MediaType contentType)
      This method send an error stream
      static void sendDetailedError​(javax.servlet.http.HttpServletResponse response, RestException exception, org.restlet.data.MediaType contentType)
      This method send an error stream
      static void sendDetailedError​(org.restlet.data.Response response, RestException exception, org.restlet.data.MediaType contentType)
      This method send an error stream from restlet scope)
      static void sendDetailedError​(org.restlet.data.Response response, org.restlet.data.Status httpStatus, java.lang.String applicationErrorCode, java.lang.String applicationErrorMessage, org.restlet.data.MediaType contentType)
      This method send an error stream (from restlet scope)
      static void sendError​(javax.servlet.http.HttpServletResponse response, int errorCode)  
      static void sendError​(javax.servlet.http.HttpServletResponse response, int errorCode, java.lang.String message)  
      static void sendError​(javax.servlet.http.HttpServletResponse response, org.restlet.data.Status error)  
      static void setUserLang​(JcmsContext jcmsContext, java.lang.String userLang)
      Force the update of the user lang in jcmsContext (must be only used in Rest environment to avoid not wanted side effects)
      • Methods inherited from class java.lang.Object

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

      • XML_UTF_8_STANDARD_BOM

        public static final java.lang.String XML_UTF_8_STANDARD_BOM
        See Also:
        Constant Field Values
      • CSRF_HEADER_NAME

        public static final java.lang.String CSRF_HEADER_NAME
        See Also:
        Constant Field Values
      • RESPONSE_CONTENT_TYPE_NAME_ATTRIBUTE

        public static final java.lang.String RESPONSE_CONTENT_TYPE_NAME_ATTRIBUTE
        See Also:
        Constant Field Values
      • FAILED_CONTROLLER_STATUS

        public static final java.lang.String FAILED_CONTROLLER_STATUS
        See Also:
        Constant Field Values
    • Constructor Detail

      • RestUtil

        public RestUtil()
    • Method Detail

      • isRest

        public static final boolean isRest​(javax.servlet.http.HttpServletRequest request)
      • sendError

        public static void sendError​(javax.servlet.http.HttpServletResponse response,
                                     org.restlet.data.Status error)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • sendError

        public static void sendError​(javax.servlet.http.HttpServletResponse response,
                                     int errorCode,
                                     java.lang.String message)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • sendError

        public static void sendError​(javax.servlet.http.HttpServletResponse response,
                                     int errorCode)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • sendDetailedError

        @Deprecated
        public static void sendDetailedError​(javax.servlet.http.HttpServletResponse response,
                                             int errorCode,
                                             java.lang.String applicationErrorCode,
                                             java.lang.String applicationErrorMessage)
                                      throws java.io.IOException
        Deprecated.
        use sendDetailedError(HttpServletResponse, int, String, String, MediaType) which allows the correct media type to be specified
        This method is here for ascendant compatibility, please use the method with content type parameter instead.
        Parameters:
        response - the Response object
        errorCode - the error code
        applicationErrorCode - the application error code
        applicationErrorMessage - the application error message
        Throws:
        java.io.IOException - Exception thrown when the content is written to the response
      • sendDetailedError

        public static void sendDetailedError​(javax.servlet.http.HttpServletResponse response,
                                             int errorCode,
                                             java.lang.String applicationErrorCode,
                                             java.lang.String applicationErrorMessage,
                                             java.lang.String contentType)
                                      throws java.io.IOException
        This method is here for ascendant compatibility, please use the method with MediaType parameter instead.
        Parameters:
        response - the Response object
        errorCode - the error code
        applicationErrorCode - the application error code
        applicationErrorMessage - the application error message
        contentType - the content type name used to create response content
        Throws:
        java.io.IOException - Exception thrown when the content is written to the response
      • sendDetailedError

        public static void sendDetailedError​(javax.servlet.http.HttpServletResponse response,
                                             int errorCode,
                                             java.lang.String applicationErrorCode,
                                             java.lang.String applicationErrorMessage,
                                             org.restlet.data.MediaType contentType)
                                      throws java.io.IOException
        This method send an error stream
        Parameters:
        response - the HttpServletResponse object
        errorCode - the error code
        applicationErrorCode - the application error code
        applicationErrorMessage - the application error message
        contentType - the content type used to create response content
        Throws:
        java.io.IOException - Exception thrown when the content is written to the response
      • sendDetailedError

        public static void sendDetailedError​(javax.servlet.http.HttpServletResponse response,
                                             RestException exception,
                                             org.restlet.data.MediaType contentType)
                                      throws java.io.IOException
        This method send an error stream
        Parameters:
        response - the HttpServletResponse object
        exception - the RestException
        contentType - the content type used to create response content
        Throws:
        java.io.IOException - Exception thrown when the cntent is written to the response
      • sendDetailedError

        public static void sendDetailedError​(org.restlet.data.Response response,
                                             org.restlet.data.Status httpStatus,
                                             java.lang.String applicationErrorCode,
                                             java.lang.String applicationErrorMessage,
                                             org.restlet.data.MediaType contentType)
        This method send an error stream (from restlet scope)
        Parameters:
        response - the Response object
        httpStatus - the Status
        applicationErrorCode - the application error code
        applicationErrorMessage - the application error message
        contentType - the content type used to create response content
      • sendDetailedError

        public static void sendDetailedError​(org.restlet.data.Response response,
                                             RestException exception,
                                             org.restlet.data.MediaType contentType)
        This method send an error stream from restlet scope)
        Parameters:
        response - the Response object
        exception - the RestException
        contentType - the content type used to create response content
      • formatXmlEntityError

        public static java.lang.String formatXmlEntityError​(java.lang.String code,
                                                            java.lang.String message)
        Print an message in the following format (without indentation) :
         
         <error>
          <code>code</code>
          <message>message</message>
         </error>
         
         
        Parameters:
        code - the error code
        message - the message
        Returns:
        the formatted XML String
      • displayResponse

        public static void displayResponse​(org.restlet.data.Response response)
                                    throws java.io.IOException
        Dev method to print in syso the content of a response
        Parameters:
        response - a Restlet response
        Throws:
        java.io.IOException - if the text content of the entity body can't be retrieved
      • formatMsgToXml

        public static java.lang.String formatMsgToXml​(javax.servlet.http.HttpServletRequest request)
        Get the info, warning and error messages in the request and in the session and returns a xml with all this informations. The form of the XML is as follow :
         
          <msg>
            <info>
              <session>message in the lang of the current logged member</session>
              <request>message in the lang of the current logged member</request>
            </info>
            <warning>
              <session>message in the lang of the current logged member</session>
              <request>message in the lang of the current logged member</request>
            </warning>
            <error>
              <session>message in the lang of the current logged member</session>
              <request>message in the lang of the current logged member</request>
            </error>
          </msg>
          
         
        If an XML element would be empty, it is not set.
        Parameters:
        request - the current request
        Returns:
        the formatted xml message.
      • formatMsgToXml

        public static java.lang.String formatMsgToXml​(javax.servlet.http.HttpServletRequest request,
                                                      boolean displayInfo,
                                                      boolean displayWarning,
                                                      boolean displayError)
        Get the info, warning and error messages in the request and in the session and returns a xml with all this informations. But info, warning or error level messages may be skipped.
        Parameters:
        request - the current request
        displayInfo - if false, info message is skipped
        displayWarning - if false, warning message is skipped
        displayError - if false, error message is skipped
        Returns:
        the formatted message
      • formatMsgToJson

        public static java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatMsgToJson​(javax.servlet.http.HttpServletRequest request)
        Get the info, warning and error messages in the request and in the session and returns a json with all this informations. The form of the JSON representation is as follow :
         
          {
            "info": {
               "session" : "message in the lang of the current logged member",
              "request" : "message in the lang of the current logged member"
            },
            "warning: {
              "session" : "message in the lang of the current logged member",
              "request" : "message in the lang of the current logged member" 
            },
            "error: {
               "session" : "message in the lang of the current logged member",
                "request" : "message in the lang of the current logged member"
            }
          }
         
         
        If an JSon element would be empty, it is not set.
        Parameters:
        request - the current request
        Returns:
        the Map to be seralized as JSon data.
      • formatMsgToJson

        public static java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> formatMsgToJson​(javax.servlet.http.HttpServletRequest request,
                                                                                                                                   boolean displayInfo,
                                                                                                                                   boolean displayWarning,
                                                                                                                                   boolean displayError)
        Get the info, warning and error messages in the request and in the session and returns a Map with all this informations. But info, warning or error level messages may be skipped. The form of the JSON representation is as follow :
         
          {
            "info": {
               "session" : "message in the lang of the current logged member",
              "request" : "message in the lang of the current logged member"
            },
            "warning: {
              "session" : "message in the lang of the current logged member",
              "request" : "message in the lang of the current logged member" 
            },
            "error: {
               "session" : "message in the lang of the current logged member",
                "request" : "message in the lang of the current logged member"
            }
          }
         
         
        If an JSon element would be empty, it is not set.
        Parameters:
        request - the current request
        displayInfo - if false, info message is skipped
        displayWarning - if false, warning message is skipped
        displayError - if false, error message is skipped
        Returns:
        the Map to be seralized as JSon data
      • sendApplicationError

        public static void sendApplicationError​(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response,
                                                java.lang.String contentType)
                                         throws java.io.IOException
        Get the messages in request or in session and put it formatted in the entity body of the response and set CLIENT_ERROR_BAD_REQUEST (400) HTTP error code.
        Parameters:
        request - the current request
        response - the current response
        contentType - the content type name used to format response
        Throws:
        java.io.IOException - if the printer is not available
      • sendApplicationError

        public static void sendApplicationError​(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response,
                                                org.restlet.data.MediaType contentType)
                                         throws java.io.IOException
        Get the messages in request or in session and put it formatted in the entity body of the response and set CLIENT_ERROR_BAD_REQUEST (400) HTTP error code.
        Parameters:
        request - the current request
        response - the current response
        contentType - the content type used to format response
        Throws:
        java.io.IOException - if the printer is not available
      • getExtension

        public static java.lang.String getExtension​(org.restlet.data.MediaType mediaType)
        Gives an possible extension from a mime-type. If present in "file-document.ext" properties
        Parameters:
        mediaType - the MediaType
        Returns:
        possible extensions
        Since:
        JCMS-6.1
      • getReferenceFromUriSuffix

        public static org.restlet.data.Reference getReferenceFromUriSuffix​(java.lang.String uriSuffix,
                                                                           javax.servlet.http.HttpServletRequest j2eeRequest)
        Gives the complete URL from an external point of vue, from an uri with no context-path and "rest" prefix
        Parameters:
        uriSuffix - the uriSuffix with no context-path and no "rest" prefix
        j2eeRequest - The current J2EE request
        Returns:
        the complete URL, with the base URL
        Since:
        JCMS-6.1
      • getForm

        public static org.restlet.data.Form getForm​(java.lang.String queryString)
        Gives a form representation of the parameters in the queryString. If parameter have a transformation specific format, they are transformed into the good value. Example :
        • If a value of a parameter is in the form "date(long,123456789)", it is transformed into a localized format date.
        Parameters:
        queryString - the query string
        Returns:
        the form
        Since:
        jcms-6.1.3
      • getForm

        public static org.restlet.data.Form getForm​(org.restlet.resource.Representation entity)
        Gives a form representation of the parameters in the entity-body. If parameter have a transformation specific format, they are transformed into the good value. Example :
        • If a value of a parameter is in the form "date(long,123456789)", or "date(w3c,1999-01-01T14:07:00+01:00)" it is transformed into a localized format date.
        Parameters:
        entity - A representation
        Returns:
        the form
        Since:
        jcms-6.1.3
      • getRestContentType

        public static org.restlet.data.MediaType getRestContentType​(javax.servlet.http.HttpServletRequest request)
        Simple implementation of the feature : is application/json in accept headers ? application_JSON : application_XML
        Parameters:
        request - the HttpServletRequest
        Returns:
        the MediaType
      • getWorkspace

        public static Workspace getWorkspace​(java.lang.String wkStr,
                                             boolean throwExceptionOnInvalidData)
                                      throws java.lang.Exception
        Retrieve Workspace from a string (by testing id and title)
        Parameters:
        wkStr - the workspace string
        throwExceptionOnInvalidData - the an Exception if the id is not empty and not a valid data
        Returns:
        null if the workspace cannot be found, otherwise the Workspace
        Throws:
        java.lang.Exception - thrown if the string is not empty and if workspace cannot be found
      • getGroup

        public static Group getGroup​(java.lang.String grpStr,
                                     Member mbr,
                                     boolean throwExceptionOnInvalidData)
                              throws java.lang.Exception
        Retrieve Group from a string (by testing id, ldap dn and name)
        Parameters:
        grpStr - the group string
        mbr - the Member used to check read rights
        throwExceptionOnInvalidData - the an Exception if the id is not empty and not a valid data
        Returns:
        null if the group cannot be found, otherwise the Group
        Throws:
        java.lang.Exception - thrown if the string is not empty and if group cannot be found, is multiple, or cannot be read by member
      • getMember

        public static Member getMember​(java.lang.String mbrStr,
                                       boolean throwExceptionOnInvalidData)
                                throws java.lang.Exception
        Retrieve Member from a string (by testing id, login and email)
        Parameters:
        mbrStr - the member string
        throwExceptionOnInvalidData - the an Exception if the data id is not empty and not a valid data
        Returns:
        null if the member cannot be found, otherwise the Member
        Throws:
        java.lang.Exception - thrown if the string is not empty and if member cannot be found, is multiple, or cannot be read by member
      • getData

        public static Data getData​(java.lang.String dataId,
                                   Member mbr,
                                   boolean throwExceptionOnInvalidData)
                            throws java.lang.Exception
        Retrieve Data instance (if mbr can read it)
        Parameters:
        dataId - the data Id (can be null
        mbr - the Member used to ckeck read rights
        throwExceptionOnInvalidData - the an Exception if the data id is not empty and not a valid data
        Returns:
        the Data if it can be found and if user can read it.
        Throws:
        java.lang.Exception - throw if the dataId is not empty and if data cannot be found, or cannot be read by member
      • getDate

        public static java.util.Date getDate​(java.lang.String date)
      • getFormParameters

        public static java.util.LinkedHashMap<java.lang.String,​java.util.List<java.lang.String>> getFormParameters​(org.restlet.resource.Representation entity,
                                                                                                                         javax.servlet.http.HttpServletRequest j2eeRequest)
                                                                                                                  throws RestException
        Read form parameters from a rest entity (as json entity, application/x-www-form-urlencoded or multipart-formdata entity)
        Parameters:
        entity - the body entity
        j2eeRequest - the HttpServletRequest (used to read multipart-formdata)
        Returns:
        the LinkedHashMap of parameters (only parameters, without any FileItem).
        Throws:
        RestException - a RestException if any problem occurs
      • setUserLang

        public static void setUserLang​(JcmsContext jcmsContext,
                                       java.lang.String userLang)
        Force the update of the user lang in jcmsContext (must be only used in Rest environment to avoid not wanted side effects)
        Parameters:
        jcmsContext - the current JcmsContext to update
        userLang - the User lang (of not null).