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:
  • Field Details

  • Constructor Details

    • 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)
      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.
    • 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 string
      roleCheckingStrategy - the role-checking strategy.
      localeSupplier - the Supplier used to retrieve the Locale.
  • Method Details

    • 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
    • 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-Object
      methodInfo - 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 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.
      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 Exception
      response - Response-Object
    • resolveExceptionStatusCode

      protected int resolveExceptionStatusCode(Exception e, int defaultStatusCode)
      Resolve WebResponse status code for Exception. Override this method to implement customized status code resolution for exceptions
      Parameters:
      e - The Exception
      defaultStatusCode - default status code returned in this situation by AbstractRestResource
    • resolveValidationErrorStatusCode

      protected int resolveValidationErrorStatusCode(IValidationError error, int defaultStatusCode)
      Resolve WebResponse status code for IValidationError. Override this method to implement customized status code resolution for validation errors
      Parameters:
      error - The Validation Error
      defaultStatusCode - default status code returned in this situation by AbstractRestResource
    • 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)
    • getLocaleSupplier

      public Supplier<Locale> getLocaleSupplier()
      Gets the Supplier used to retrieve the Locale
      Returns:
      the Supplier used to retrieve the Locale