public abstract class AbstractEntityProvider
extends java.lang.Object
implements javax.ws.rs.ext.MessageBodyWriter<java.util.Map>
Extend this class to create a new entity provider for supporting a new response format.
This class provides two main helper methods to format a map of Service Data Objects into the requested response format:
Modifier and Type | Field and Description |
---|---|
static java.lang.String | COPYRIGHT
copyright
|
protected javax.servlet.http.HttpServletRequest | request
The javax.servlet.http.HttpServletRequest object in the context.
|
protected javax.servlet.http.HttpServletResponse | response
The response.
|
Constructor and Description |
---|
AbstractEntityProvider() |
Modifier and Type | Method and Description |
---|---|
protected java.util.Map<java.lang.String,java.lang.Object> | formatResponseUsingBODToMapConfig(java.util.Map dataMap)
This method is a helper method for formatting Service Data Objects to a Map.
|
protected byte[] | formatResponseUsingJsp(java.util.Map dataMap)
This method is a helper method for formatting Service Data Objects using a JSP.
|
protected java.lang.String | getActionFormat()
This method returns the action format which is required for resolving the struts action of the formatting JSP.
|
protected java.lang.String | getErrorActionPrefix()
This method returns the prefix for the struts action for the error handling JSP for the specific response format.
|
long | getSize(java.util.Map arg0, java.lang.Class<?> arg1, java.lang.reflect.Type arg2, java.lang.annotation.Annotation[] arg3, javax.ws.rs.core.MediaType arg4)
This method calculates the size of a Map.
|
boolean | isWriteable(java.lang.Class<?> arg0, java.lang.reflect.Type arg1, java.lang.annotation.Annotation[] arg2, javax.ws.rs.core.MediaType arg3)
This method checks if the Map is writable.
|
public static final java.lang.String COPYRIGHT
@Context protected javax.servlet.http.HttpServletRequest request
@Context protected javax.servlet.http.HttpServletResponse response
public long getSize(java.util.Map arg0, java.lang.Class<?> arg1, java.lang.reflect.Type arg2, java.lang.annotation.Annotation[] arg3, javax.ws.rs.core.MediaType arg4)
public boolean isWriteable(java.lang.Class<?> arg0, java.lang.reflect.Type arg1, java.lang.annotation.Annotation[] arg2, javax.ws.rs.core.MediaType arg3)
protected byte[] formatResponseUsingJsp(java.util.Map dataMap) throws javax.ws.rs.WebApplicationException
This method parses the map of data provided by the JAX-RS Resource handler and sets them as request properties in the request to the formatting JSP. The request properties can be accessed in the JSP using the same keys that were set in the data map. Additionally, for data objects, a list of data objects are set to the request properties directly using the request property dataObject. If there exists only one data object in the list then, the request property dataObject is set to the data object itself instead of a list. The struts action to the JSP is constructed using the action format returned by getActionFormat and the formatting group name. For example:
Suppose for ATOM Feed, the action format is /{0}Atom/{1} and the formatting group name provided is ShowPersonDataAreaType. The struts action will be resolved to /ShowPersonDataAreaTypeAtom/0.
In the above example, the last component denotes the store id. A store id 0(zero) is used to specify site level. This method first tries to get a JSP action from the current store. If it does not exist, it will search the store path to get a JSP action from an asset store, until the site-level JSP action is found.
If errors are passed by the JAX-RS Resource handler in the data map, then it invokes an error JSP struts action returned by getErrorActionPrefix.
protected java.util.Map<java.lang.String,java.lang.Object> formatResponseUsingBODToMapConfig(java.util.Map dataMap) throws java.lang.Exception
This method parses the map of data provided by the JAX-RS Resource handler and retrieves DataObject(s) and BOD to Map configuration data so that it can convert each DataObject into a Map based on the configuration. The BOD to Map configuration is from XML files located under Rest.war/WEB-INF/config directory.
If the input dataMap contains multiple DataObjects, the method will convert each DataObject into a Map based on its own BOD to Map configuration data and add them to a List. The corresponding JAX-RS Resource handler should implement a postProcess method to correlate the list of maps and create a meaningful map to return.
By default, the data map contains the following data. You can add more data into the map from your JAX-RS Resource handler class for your customization.
Key | Value |
dataList | List of map containing BOD DataAreaType Service Data Objects and BOD to Map configuration data. |
resultData | Map of result data from a REST service. This map will be put in response directly. |
errors | List of map of errors if there exists any errors/exceptions. This map will be put in response directly. |
formatGroupName | Group name to identify the main JSP struts action. |
resourceHandler | ResourceHandler that interface to invoke postProcess method. |
For dataList, each map in the list contains the following:
Key | Value |
dataObject | A BOD DataAreaType Service Data Object. |
resourceName | Resource type. It is used to locate the rest--clientobjects.xml file. |
urlParamGroupName | The URLParameterGroup name defined in XML to convert BOD to a Map. |
flattenNoun | Flatten the top level noun if it is true. |
resourceId | A URI represent the resource. |
protected java.lang.String getActionFormat()
By default, this method returns NULL. Extend this method for JAX-RS entity providers that require a JSP for formatting the response. For example, for ATOM Feed it can return "/{0}Atom/{1}", where 0 is substituted with the provided formatting group name and 1 is substituted with the resolved store id.
protected java.lang.String getErrorActionPrefix()
By default, this method returns "Error". Hence, for example, for ATOM feed, the struts action for the error handling JSP would become "/ErrorAtom/0".
It is recommended to have only one error JSP per response format. Based upon your requirements you can either have an error JSP at the site level or at the store level. In the example above "/ErrorAtom/0", 0 denotes site level.