Package org.wicketstuff.rest.resource
Class AbstractRestResource<T extends IWebSerialDeserial>
- java.lang.Object
-
- org.wicketstuff.rest.resource.AbstractRestResource<T>
-
- All Implemented Interfaces:
Serializable
,IResource
,IClusterable
public abstract class AbstractRestResource<T extends IWebSerialDeserial> extends Object implements IResource
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
-
-
Field Summary
Fields Modifier and Type Field Description static String
NO_SUITABLE_METHOD_FOUND
static String
USER_IS_NOT_ALLOWED
-
Constructor Summary
Constructors Constructor Description AbstractRestResource(T serialDeserial)
Constructor with no role-checker (i.e we don't use annotationAuthorizeInvocation
).AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleCheckingStrategy)
Constructor that takes in input the object serializer/deserializer and the role-checking strategy to use.AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleCheckingStrategy, Supplier<Locale> localeSupplier)
Main constructor that takes in input the object serializer/deserializer and the role-checking strategy to use.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.Supplier<Locale>
getLocaleSupplier()
Gets the Supplier used to retrieve the Localeprotected Map<String,List<MethodMappingInfo>>
getMappedMethods()
Return mapped methods grouped by number of segments and HTTP method.Map<Method,MethodMappingInfo>
getMappedMethodsInfo()
MethodMappingInfo
getMethodInfo(Method method)
protected <E> IValidator<E>
getValidator(String key, E validatorType)
Retrieve a registered validator.protected void
handleException(WebResponse response, Exception exception)
Handle Exception.protected void
handleValidationError(WebResponse response, String outputFormat, IValidationError error)
Handle Validation exception.void
noSuitableMethodFound(WebResponse response, org.wicketstuff.restutils.http.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.<E> E
requestToObject(WebRequest request, Class<E> argClass, String mimeType)
Internal method that tries to extract an instance of the given class from the request body.protected int
resolveExceptionStatusCode(Exception e, int defaultStatusCode)
ResolveWebResponse
status code for Exception.protected int
resolveValidationErrorStatusCode(IValidationError error, int defaultStatusCode)
ResolveWebResponse
status code forIValidationError
.void
respond(IResource.Attributes attributes)
Handles a REST request invoking one of the methods annotated withMethodMapping
.protected void
setResponseStatusCode(int statusCode)
Set the status code for the current response.static Object
toObject(Class<?> clazz, String value, Supplier<Locale> localeSupplier)
Utility method to convert string values to the corresponding objects.protected void
unauthorizedMethodAccess(WebResponse response, MethodMappingInfo methodInfo)
Handle unauthorized user access.protected void
unregisterValidator(String key)
Unregister a Wicket validator.
-
-
-
Field Detail
-
NO_SUITABLE_METHOD_FOUND
public static final String NO_SUITABLE_METHOD_FOUND
- See Also:
- Constant Field Values
-
USER_IS_NOT_ALLOWED
public static final String USER_IS_NOT_ALLOWED
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractRestResource
public AbstractRestResource(T serialDeserial)
Constructor with no role-checker (i.e we don't use annotationAuthorizeInvocation
).- Parameters:
serialDeserial
- General class that is used to serialize/desiarilze objects to string.
-
AbstractRestResource
public AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleCheckingStrategy)
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 stringroleCheckingStrategy
- the role-checking strategy.
-
AbstractRestResource
public AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleCheckingStrategy, Supplier<Locale> localeSupplier)
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 stringroleCheckingStrategy
- the role-checking strategy.localeSupplier
- the Supplier used to retrieve the Locale.
-
-
Method Detail
-
respond
public final void respond(IResource.Attributes attributes)
Handles a REST request invoking one of the methods annotated withMethodMapping
. 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 (SeeAuthorizeInvocation
).
-
unauthorizedMethodAccess
protected void unauthorizedMethodAccess(WebResponse response, MethodMappingInfo methodInfo)
Handle unauthorized user access. Default: set response Status to 401. Override this method to implement customized handling- Parameters:
response
- Response-ObjectmethodInfo
- The mapped method
-
noSuitableMethodFound
public void noSuitableMethodFound(WebResponse response, org.wicketstuff.restutils.http.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 objecthttpMethod
- 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.- Parameters:
response
- The current response object.result
- The object to write to response.mimeType
- The MIME type to use to serialize data
-
onInitialize
protected void onInitialize(T objSerialDeserial)
Method called to initialize and configure the resource.- Parameters:
objSerialDeserial
- the object serializer/deserializer
-
handleException
protected void handleException(WebResponse response, Exception exception)
Handle Exception. Default: responds with a generic error message "General server error." and logging the exception. Override this method to implement customized error handling- Parameters:
exception
- The Exceptionresponse
- Response-Object
-
resolveExceptionStatusCode
protected int resolveExceptionStatusCode(Exception e, int defaultStatusCode)
ResolveWebResponse
status code for Exception. Override this method to implement customized status code resolution for exceptions- Parameters:
e
- The ExceptiondefaultStatusCode
- default status code returned in this situation byAbstractRestResource
-
resolveValidationErrorStatusCode
protected int resolveValidationErrorStatusCode(IValidationError error, int defaultStatusCode)
ResolveWebResponse
status code forIValidationError
. Override this method to implement customized status code resolution for validation errors- Parameters:
error
- The Validation ErrordefaultStatusCode
- default status code returned in this situation byAbstractRestResource
-
handleValidationError
protected void handleValidationError(WebResponse response, String outputFormat, IValidationError error)
Handle Validation exception. Override this method to implement customized validation error handling- Parameters:
response
-outputFormat
-error
-
-
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 <E> E requestToObject(WebRequest request, Class<E> argClass, String mimeType)
Internal method that tries to extract an instance of the given class from the request body.- Parameters:
argClass
- the type we want to extract from request body.- 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, Supplier<Locale> localeSupplier) throws RuntimeException
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:
RuntimeException
-
getCurrentWebResponse
public static final WebResponse getCurrentWebResponse()
Utility method to retrieve the current web response.- Returns:
- the current web response
-
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 final 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 final void unregisterValidator(String key)
Unregister a Wicket validator.- Parameters:
key
- the key to use to remove the validator.
-
getValidator
protected final <E> IValidator<E> getValidator(String key, E validatorType)
Retrieve a registered validator.- 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.
-
getMappedMethodsInfo
public Map<Method,MethodMappingInfo> getMappedMethodsInfo()
-
getMethodInfo
public MethodMappingInfo getMethodInfo(Method method)
-
-