Package com.jalios.jcms.rest
Class RestUtil
java.lang.Object
com.jalios.jcms.rest.RestUtil
Provides utility static methods for both client and server side in REST communication.
- Since:
- JCMS-6.0
- Author:
- dissert
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddisplayResponse(org.restlet.data.Response response) Dev method to print in syso the content of a responseformatMsgToJson(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 StringformatMsgToXml(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 StringformatMsgToXml(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 StringformatXmlEntityError(String code, String message) Print an message in the following format (without indentation) :static DataRetrieve Data instance (if mbr can read it)static Datestatic StringgetExtension(org.restlet.data.MediaType mediaType) Gives an possible extension from a mime-type.static org.restlet.data.FormGives a form representation of the parameters in the queryString.static org.restlet.data.FormgetForm(org.restlet.resource.Representation entity) Gives a form representation of the parameters in the entity-body.static LinkedHashMap<String, List<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 GroupRetrieveGroupfrom a string (by testing id, ldap dn and name)static LinkedHashMap<String, Object> getJSONFormParameters(String entity) static MemberRetrieveMemberfrom a string (by testing id, login and email)static org.restlet.data.ReferencegetReferenceFromUriSuffix(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" prefixstatic org.restlet.data.MediaTypegetRestContentType(javax.servlet.http.HttpServletRequest request) Simple implementation of the feature : is application/json in accept headers ? application_JSON : application_XMLstatic WorkspacegetWorkspace(String wkStr, boolean throwExceptionOnInvalidData) RetrieveWorkspacefrom a string (by testing id and title)static final booleanisRest(javax.servlet.http.HttpServletRequest request) static voidsendApplicationError(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 voidsendApplicationError(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 voidsendDetailedError(javax.servlet.http.HttpServletResponse response, int errorCode, String applicationErrorCode, String applicationErrorMessage) Deprecated.static voidsendDetailedError(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 voidsendDetailedError(javax.servlet.http.HttpServletResponse response, int errorCode, String applicationErrorCode, String applicationErrorMessage, org.restlet.data.MediaType contentType) This method send an error streamstatic voidsendDetailedError(javax.servlet.http.HttpServletResponse response, RestException exception, org.restlet.data.MediaType contentType) This method send an error streamstatic voidsendDetailedError(org.restlet.data.Response response, RestException exception, org.restlet.data.MediaType contentType) This method send an error stream from restlet scope)static voidsendDetailedError(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 voidsendError(javax.servlet.http.HttpServletResponse response, int errorCode) static voidstatic voidsendError(javax.servlet.http.HttpServletResponse response, org.restlet.data.Status error) static voidsetUserLang(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)
-
Field Details
-
XML_UTF_8_STANDARD_BOM
- See Also:
-
CSRF_HEADER_NAME
- See Also:
-
RESPONSE_CONTENT_TYPE_NAME_ATTRIBUTE
- See Also:
-
FAILED_CONTROLLER_STATUS
- See Also:
-
-
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.usesendDetailedError(HttpServletResponse, int, String, String, MediaType)which allows the correct media type to be specifiedThis method is here for ascendant compatibility, please use the method with content type parameter instead.- Parameters:
response- theResponseobjecterrorCode- the error codeapplicationErrorCode- the application error codeapplicationErrorMessage- 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- theResponseobjecterrorCode- the error codeapplicationErrorCode- the application error codeapplicationErrorMessage- the application error messagecontentType- 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- theHttpServletResponseobjecterrorCode- the error codeapplicationErrorCode- the application error codeapplicationErrorMessage- the application error messagecontentType- 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- theHttpServletResponseobjectexception- theRestExceptioncontentType- 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- theResponseobjecthttpStatus- theStatusapplicationErrorCode- the application error codeapplicationErrorMessage- the application error messagecontentType- 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- theResponseobjectexception- theRestExceptioncontentType- the content type used to create response content
-
formatXmlEntityError
Print an message in the following format (without indentation) :<error> <code>code</code> <message>message</message> </error>
- Parameters:
code- the error codemessage- the message- Returns:
- the formatted XML String
-
displayResponse
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
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 requestdisplayInfo- if false, info message is skippeddisplayWarning- if false, warning message is skippeddisplayError- if false, error message is skipped- Returns:
- the formatted message
-
formatMsgToJson
public static Map<String,Map<String, formatMsgToJsonString>> (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, formatMsgToJsonString>> (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 requestdisplayInfo- if false, info message is skippeddisplayWarning- if false, warning message is skippeddisplayError- 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 requestresponse- the current responsecontentType- 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 requestresponse- the current responsecontentType- the content type used to format response- Throws:
IOException- if the printer is not available
-
getExtension
Gives an possible extension from a mime-type. If present in "file-document.ext" properties- Parameters:
mediaType- theMediaType- 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" prefixj2eeRequest- The current J2EE request- Returns:
- the complete URL, with the base URL
- Since:
- JCMS-6.1
-
getForm
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- theHttpServletRequest- Returns:
- the
MediaType
-
getWorkspace
public static Workspace getWorkspace(String wkStr, boolean throwExceptionOnInvalidData) throws Exception RetrieveWorkspacefrom a string (by testing id and title) -
getGroup
public static Group getGroup(String grpStr, Member mbr, boolean throwExceptionOnInvalidData) throws Exception RetrieveGroupfrom a string (by testing id, ldap dn and name)- Parameters:
grpStr- the group stringmbr- theMemberused to check read rightsthrowExceptionOnInvalidData- 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
RetrieveMemberfrom a string (by testing id, login and email)- Parameters:
mbrStr- the member stringthrowExceptionOnInvalidData- 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 nullmbr- theMemberused to ckeck read rightsthrowExceptionOnInvalidData- the an Exception if the data id is not empty and not a valid data- Returns:
- the
Dataif 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
-
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 entityj2eeRequest- theHttpServletRequest(used to read multipart-formdata)- Returns:
- the LinkedHashMap of parameters (only parameters, without any FileItem).
- Throws:
RestException- aRestExceptionif any problem occurs
-
getJSONFormParameters
public static LinkedHashMap<String,Object> getJSONFormParameters(String entity) throws RestException - Throws:
RestException
-
setUserLang
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 currentJcmsContextto updateuserLang- the User lang (of not null).
-
sendDetailedError(HttpServletResponse, int, String, String, MediaType)which allows the correct media type to be specified