org.wicketstuff.rest.resource
Class AbstractRestResource<T extends IWebSerialDeserial>

java.lang.Object
  extended by org.wicketstuff.rest.resource.AbstractRestResource<T>
All Implemented Interfaces:
Serializable, IResource, IClusterable, IWebSerialDeserial

public abstract class AbstractRestResource<T extends IWebSerialDeserial>
extends Object
implements IResource, IWebSerialDeserial

Base class to build a resource that serves REST requests.

Author:
andrea del bene
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.wicket.request.resource.IResource
IResource.Attributes
 
Constructor Summary
AbstractRestResource(T serialDeserial)
          Constructor with no role-checker (i.e we don't use annotation AuthorizeInvocation).
AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleCheckingStrategy)
          Main constructor that takes in input the object serializer/deserializer and the role-checking strategy to use.
 
Method Summary
protected  void configureObjSerialDeserial(T objSerialDeserial)
          Deprecated. use onConfigure(T objSerialDeserial) instead.
static Url extractUrlFromRequest()
          Utility method to extract the client URL from the current request.
static WebRequest getCurrentWebRequest()
          Utility method to retrieve the current web request.
static WebResponse getCurrentWebResponse()
          Utility method to retrieve the current web response.
protected  Map<String,List<MethodMappingInfo>> getMappedMethods()
          Return mapped methods grouped by number of segments and HTTP method.
protected  IValidator getValidator(String key)
          Retrieve a registered validato.
 boolean isMimeTypeSupported(String mimeType)
          Check if a given MIME type is handled.
static void noSuitableMethodFound(WebResponse response, HttpMethod httpMethod)
          This method can be used to write a standard error message to the current response object when no mapped method has been found for the current request.
 void objectToResponse(Object result, WebResponse response, String mimeType)
          Method invoked to serialize the result of the invoked method and write this value to the response.
protected  void onAfterMethodInvoked(MethodMappingInfo mappedMethod, IResource.Attributes attributes, Object result)
          Invoked just after a mapped method has been invoked to serve the current request.
protected  void onBeforeMethodInvoked(MethodMappingInfo mappedMethod, IResource.Attributes attributes)
          Invoked just before a mapped method is invoked to serve the current request.
protected  void onInitialize(T objSerialDeserial)
          Method called to initialize and configure the resource.
protected  void registerValidator(String key, IValidator validator)
          Register a Wicket validator for the current resource.
<T> T
requestToObject(WebRequest request, Class<T> argClass, String mimeType)
          Internal method that tries to extract an instance of the given class from the request body.
 void respond(IResource.Attributes attributes)
          Handles a REST request invoking one of the methods annotated with MethodMapping.
protected  void setResponseStatusCode(int statusCode)
          Set the status code for the current response.
static Object toObject(Class clazz, String value)
          Utility method to convert string values to the corresponding objects.
protected  void unregisterValidator(String key)
          Unregister a Wicket validator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractRestResource

public AbstractRestResource(T serialDeserial)
Constructor with no role-checker (i.e we don't use annotation AuthorizeInvocation).

Parameters:
serialDeserial - General class that is used to serialize/desiarilze objects to string.

AbstractRestResource

public AbstractRestResource(T serialDeserial,
                            IRoleCheckingStrategy roleCheckingStrategy)
Main constructor that takes in input the object serializer/deserializer and the role-checking strategy to use.

Parameters:
serialDeserial - General class that is used to serialize/desiarilze objects to string
roleCheckingStrategy - the role-checking strategy.
Method Detail

respond

public final void respond(IResource.Attributes attributes)
Handles a REST request invoking one of the methods annotated with MethodMapping. If the annotated method returns a value, this latter is automatically serialized to a given string format (like JSON, XML, etc...) and written to the web response.
If no method is found to serve the current request, a 400 HTTP code is returned to the client. Similarly, a 401 HTTP code is return if the user doesn't own one of the roles required to execute an annotated method (See AuthorizeInvocation).

Specified by:
respond in interface IResource
Parameters:
attributes - the Attribute object of the current request

noSuitableMethodFound

public static void noSuitableMethodFound(WebResponse response,
                                         HttpMethod httpMethod)
This method can be used to write a standard error message to the current response object when no mapped method has been found for the current request.

Parameters:
response - the current response object
httpMethod - the HTTP method of the current request

onBeforeMethodInvoked

protected void onBeforeMethodInvoked(MethodMappingInfo mappedMethod,
                                     IResource.Attributes attributes)
Invoked just before a mapped method is invoked to serve the current request.

Parameters:
mappedMethod - the mapped method.
attributes - the current Attributes object.

onAfterMethodInvoked

protected void onAfterMethodInvoked(MethodMappingInfo mappedMethod,
                                    IResource.Attributes attributes,
                                    Object result)
Invoked just after a mapped method has been invoked to serve the current request.

Parameters:
mappedMethod - the mapped method.
attributes - the current Attributes object.
result - the value returned by the invoked method.

objectToResponse

public void objectToResponse(Object result,
                             WebResponse response,
                             String mimeType)
Method invoked to serialize the result of the invoked method and write this value to the response.

Specified by:
objectToResponse in interface IWebSerialDeserial
Parameters:
response - The current response object.
result - The object to write to response.
restMimeFormats - The MIME type to use to serialize data

configureObjSerialDeserial

@Deprecated
protected void configureObjSerialDeserial(T objSerialDeserial)
Deprecated. use onConfigure(T objSerialDeserial) instead.

Method called to initialize and configure the object serializer/deserializer.

Parameters:
objSerialDeserial - the object serializer/deserializer

onInitialize

protected void onInitialize(T objSerialDeserial)
Method called to initialize and configure the resource.

Parameters:
objSerialDeserial - the object serializer/deserializer

extractUrlFromRequest

public static Url extractUrlFromRequest()
Utility method to extract the client URL from the current request.

Returns:
the URL for the current request.

requestToObject

public <T> T requestToObject(WebRequest request,
                             Class<T> argClass,
                             String mimeType)
Internal method that tries to extract an instance of the given class from the request body.

Specified by:
requestToObject in interface IWebSerialDeserial
Parameters:
argClass - the type we want to extract from request body.
request - the request object.
mimeType - the MIME type of the request.
Returns:
the extracted object.

getCurrentWebRequest

public static final WebRequest getCurrentWebRequest()
Utility method to retrieve the current web request.

Returns:
the current web request

toObject

public static Object toObject(Class clazz,
                              String value)
                       throws IllegalArgumentException
Utility method to convert string values to the corresponding objects.

Parameters:
clazz - the type of the object we want to obtain.
value - the string value we want to convert.
Returns:
the object corresponding to the converted string value, or null if value parameter is null
Throws:
IllegalArgumentException

getCurrentWebResponse

public static final WebResponse getCurrentWebResponse()
Utility method to retrieve the current web response.

Returns:
the current web response

isMimeTypeSupported

public boolean isMimeTypeSupported(String mimeType)
Description copied from interface: IWebSerialDeserial
Check if a given MIME type is handled.

Specified by:
isMimeTypeSupported in interface IWebSerialDeserial
Parameters:
mimeType - the MIME type we want to check.
Returns:
true if the MIME type is supported, false otherwise.

setResponseStatusCode

protected final void setResponseStatusCode(int statusCode)
Set the status code for the current response.

Parameters:
statusCode - the status code we want to set on the current response.

getMappedMethods

protected Map<String,List<MethodMappingInfo>> getMappedMethods()
Return mapped methods grouped by number of segments and HTTP method. So for example, to get all methods mapped on a path with three segments and with GET method, the key to use will be "3_GET" (underscore-separated)

Returns:
the immutable map containing mapped methods.

registerValidator

protected void registerValidator(String key,
                                 IValidator validator)
Register a Wicket validator for the current resource.

Parameters:
key - the key to use to store the validator.
validator - the validator to register

unregisterValidator

protected void unregisterValidator(String key)
Unregister a Wicket validator.

Parameters:
key - the key to use to remove the validator.

getValidator

protected IValidator getValidator(String key)
Retrieve a registered validato.

Parameters:
key - the key to use to retrieve the validator.
Returns:
the registered validator corresponding to the given key. Null if no validator has been registered with the given key.


Copyright © 2013. All Rights Reserved.