Class RestUtil

java.lang.Object
com.jalios.jcms.rest.RestUtil

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

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
    static final String
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    displayResponse(org.restlet.data.Response response)
    Dev method to print in syso the content of a response
    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.
    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 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 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 String
    Print an message in the following format (without indentation) :
    static Data
    getData(String dataId, Member mbr, boolean throwExceptionOnInvalidData)
    Retrieve Data instance (if mbr can read it)
    static Date
     
    static String
    getExtension(org.restlet.data.MediaType mediaType)
    Gives an possible extension from a mime-type.
    static org.restlet.data.Form
    getForm(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.
    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(String grpStr, Member mbr, boolean throwExceptionOnInvalidData)
    Retrieve Group from a string (by testing id, ldap dn and name)
     
    static Member
    getMember(String mbrStr, boolean throwExceptionOnInvalidData)
    Retrieve Member from a string (by testing id, login and email)
    static org.restlet.data.Reference
    getReferenceFromUriSuffix(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(String wkStr, boolean throwExceptionOnInvalidData)
    Retrieve Workspace from a string (by testing id and title)
    static final boolean
    isRest(javax.servlet.http.HttpServletRequest request)
     
    static void
    sendApplicationError(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, 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, String applicationErrorCode, 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, String applicationErrorCode, String applicationErrorMessage, 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, String applicationErrorCode, 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, String applicationErrorCode, 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, String message)
     
    static void
    sendError(javax.servlet.http.HttpServletResponse response, org.restlet.data.Status error)
     
    static void
    setUserLang(JcmsContext jcmsContext, 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 Details

  • Constructor Details

    • RestUtil

      public RestUtil()
  • Method Details

    • 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 IOException
      Throws:
      IOException
    • sendError

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

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

      @Deprecated public static void sendDetailedError(javax.servlet.http.HttpServletResponse response, int errorCode, String applicationErrorCode, String applicationErrorMessage) throws 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:
      IOException - Exception thrown when the content is written to the response
    • sendDetailedError

      public static void sendDetailedError(javax.servlet.http.HttpServletResponse response, int errorCode, String applicationErrorCode, String applicationErrorMessage, String contentType) throws 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:
      IOException - Exception thrown when the content is written to the response
    • sendDetailedError

      public static void sendDetailedError(javax.servlet.http.HttpServletResponse response, int errorCode, String applicationErrorCode, String applicationErrorMessage, org.restlet.data.MediaType contentType) throws 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:
      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 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:
      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, String applicationErrorCode, 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 String formatXmlEntityError(String code, 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 IOException
      Dev method to print in syso the content of a response
      Parameters:
      response - a Restlet response
      Throws:
      IOException - if the text content of the entity body can't be retrieved
    • formatMsgToXml

      public static 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 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 Map<String,Map<String,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 Map<String,Map<String,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, String contentType) throws 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:
      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 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:
      IOException - if the printer is not available
    • getExtension

      public static 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(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(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(String wkStr, boolean throwExceptionOnInvalidData) throws 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:
      Exception - thrown if the string is not empty and if workspace cannot be found
    • getGroup

      public static Group getGroup(String grpStr, Member mbr, boolean throwExceptionOnInvalidData) throws 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:
      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(String mbrStr, boolean throwExceptionOnInvalidData) throws 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:
      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(String dataId, Member mbr, boolean throwExceptionOnInvalidData) throws 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:
      Exception - throw if the dataId is not empty and if data cannot be found, or cannot be read by member
    • getDate

      public static Date getDate(String date)
    • getFormParameters

      public static LinkedHashMap<String,List<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
    • getJSONFormParameters

      public static LinkedHashMap<String,Object> getJSONFormParameters(String entity) throws RestException
      Throws:
      RestException
    • setUserLang

      public static void setUserLang(JcmsContext jcmsContext, 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).