Package com.jalios.jcms.rest
Class RestUtil
- java.lang.Object
-
- com.jalios.jcms.rest.RestUtil
-
public class RestUtil extends java.lang.ObjectProvides 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 java.lang.StringCSRF_HEADER_NAMEstatic java.lang.StringFAILED_CONTROLLER_STATUSstatic java.lang.StringRESPONSE_CONTENT_TYPE_NAME_ATTRIBUTEstatic java.lang.StringXML_UTF_8_STANDARD_BOM
-
Constructor Summary
Constructors Constructor Description RestUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voiddisplayResponse(org.restlet.data.Response response)Dev method to print in syso the content of a responsestatic 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.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 java.lang.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 java.lang.StringformatXmlEntityError(java.lang.String code, java.lang.String message)Print an message in the following format (without indentation) :static DatagetData(java.lang.String dataId, Member mbr, boolean throwExceptionOnInvalidData)Retrieve Data instance (if mbr can read it)static java.util.DategetDate(java.lang.String date)static java.lang.StringgetExtension(org.restlet.data.MediaType mediaType)Gives an possible extension from a mime-type.static org.restlet.data.FormgetForm(java.lang.String queryString)Gives 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 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 GroupgetGroup(java.lang.String grpStr, Member mbr, boolean throwExceptionOnInvalidData)RetrieveGroupfrom a string (by testing id, ldap dn and name)static MembergetMember(java.lang.String mbrStr, boolean throwExceptionOnInvalidData)RetrieveMemberfrom a string (by testing id, login and email)static org.restlet.data.ReferencegetReferenceFromUriSuffix(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" 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(java.lang.String wkStr, boolean throwExceptionOnInvalidData)RetrieveWorkspacefrom a string (by testing id and title)static booleanisRest(javax.servlet.http.HttpServletRequest request)static voidsendApplicationError(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 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, java.lang.String applicationErrorCode, java.lang.String applicationErrorMessage)Deprecated.usesendDetailedError(HttpServletResponse, int, String, String, MediaType)which allows the correct media type to be specifiedstatic voidsendDetailedError(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 voidsendDetailedError(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 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, java.lang.String applicationErrorCode, java.lang.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 voidsendError(javax.servlet.http.HttpServletResponse response, int errorCode, java.lang.String message)static voidsendError(javax.servlet.http.HttpServletResponse response, org.restlet.data.Status error)static voidsetUserLang(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)
-
-
-
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
-
-
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.IOExceptionDeprecated.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:
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.IOExceptionThis 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:
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.IOExceptionThis 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:
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.IOExceptionThis method send an error stream- Parameters:
response- theHttpServletResponseobjectexception- theRestExceptioncontentType- 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- 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
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 codemessage- the message- Returns:
- the formatted XML String
-
displayResponse
public static void displayResponse(org.restlet.data.Response response) throws java.io.IOExceptionDev 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 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 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 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, java.lang.String contentType) throws java.io.IOExceptionGet 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:
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.IOExceptionGet 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:
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- theMediaType- 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" prefixj2eeRequest- 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- theHttpServletRequest- Returns:
- the
MediaType
-
getWorkspace
public static Workspace getWorkspace(java.lang.String wkStr, boolean throwExceptionOnInvalidData) throws java.lang.Exception
RetrieveWorkspacefrom a string (by testing id and title)- Parameters:
wkStr- the workspace stringthrowExceptionOnInvalidData- 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
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:
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
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:
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 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:
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 RestExceptionRead 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
-
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 currentJcmsContextto updateuserLang- the User lang (of not null).
-
-