Class AbstractResource

java.lang.Object
is.galia.resource.AbstractResource
Direct Known Subclasses:
AbstractImageResource, ConfigurationResource, FileResource, HealthResource, LandingResource, StatusResource, TaskResource, TasksResource, TrailingSlashResource

public abstract class AbstractResource extends Object

Abstract HTTP resource. Instances should subclass and override one or more of the HTTP-method-specific methods doGET() etc., and may optionally use doInit() and destroy().

  • Field Details

  • Constructor Details

    • AbstractResource

      public AbstractResource()
  • Method Details

    • decodePathComponent

      protected static String decodePathComponent(String pathComponent)
      URL-decodes and un-slashes the given path component.
      Parameters:
      pathComponent - Raw path component.
      Returns:
      Decoded path component.
    • doInit

      public void doInit() throws Exception

      Initialization method, called after all necessary setters have been called but before any request handler method (like doGET() etc.)

      If an implementation class has anything to do with image identifiers, it should add the request identifier to the request context.

      Overrides must call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • destroy

      public void destroy()

      Called at the end of the instance's lifecycle.

      Overrides must call super.

    • doDELETE

      public void doDELETE() throws Exception

      Must be overridden by implementations that support DELETE.

      Overrides must not call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • doGET

      public void doGET() throws Exception

      Must be overridden by implementations that support GET.

      Overrides must not call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • doHEAD

      public void doHEAD() throws Exception
      This implementation simply calls doGET(). When that is overridden, this may also be overridden in order to set headers only and not compute a response body.
      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • doOPTIONS

      public final void doOPTIONS()
      If the implementation supports any methods other than OPTIONS, this method sends a correct response to an OPTIONS request. Otherwise, it returns HTTP 405 (Method Not Allowed).
    • doPATCH

      public void doPATCH() throws Exception

      Must be overridden by implementations that support PATCH.

      Overrides must not call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • doPOST

      public void doPOST() throws Exception

      Must be overridden by implementations that support POST.

      Overrides must not call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • doPUT

      public void doPUT() throws Exception

      Must be overridden by implementations that support PUT.

      Overrides must not call super.

      Throws:
      Exception - upon any error. This may be a ResourceException to enable a custom status.
    • authenticateUsingBasic

      protected final void authenticateUsingBasic(String realm, CredentialStore credentialStore)
      Checks the Authorization header for credentials that exist in the given CredentialStore. If not found, sends a WWW-Authenticate header and throws an exception.
      Parameters:
      realm - Basic realm.
      credentialStore - Credential store.
      Throws:
      ResourceException - if authentication failed.
    • getCanonicalClientIPAddress

      protected String getCanonicalClientIPAddress()
      Returns the user agent's IP address, respecting the X-Forwarded-For request header, if present.
      Returns:
      User agent's IP address.
    • getRoutes

      public abstract Set<Route> getRoutes()
      Part of the Resource contract.
      Returns:
      All routes to which the instance responds.
    • getCommonTemplateVars

      protected final Map<String,Object> getCommonTemplateVars()
      Returns a map of template variables common to most or all templates.
      Returns:
      Common template variables.
    • getDelegate

      protected final Delegate getDelegate()
      Returns the Delegate instance for the current request. The result is cached and may be null.
      Returns:
      Instance for the current request.
    • getLocalBasePath

      protected String getLocalBasePath()
    • getLogger

      protected abstract org.slf4j.Logger getLogger()
    • getPreferredMediaTypes

      protected final List<String> getPreferredMediaTypes()
      Returns a list of client-preferred media types as expressed in the Accept request header.
      Returns:
      List of client-preferred media types.
      See Also:
    • getPublicReference

      protected Reference getPublicReference()

      The request URI is not necessarily the URI that the client has supplied, or sees, or should see, as it may have come from a reverse proxy server. This method generates a new instance based on the request URI and either the Key.BASE_URI configuration key or X-Forwarded-* request headers, resulting in something that is more appropriate to expose to a client. For example, when generating URIs of application resources to include in a response, it would be appropriate to generate them using this method.

      Note that the return value may not be appropriate for exposure to a client in all cases—for example, any identifier present in the URI path is not translated (slashes substituted, etc.). In those cases it will be necessary to use adjust the path manually.

      Returns:
      New instance.
      See Also:
    • getPublicRootReference

      protected Reference getPublicRootReference()

      Returns a reference to the base URI of the application.

      Key.BASE_URI is respected, if set. Otherwise, the X-Forwarded-* request headers are respected, if available.

      Returns:
      New instance.
      See Also:
    • getRepresentationDisposition

      protected String getRepresentationDisposition(String identifierStr, Format outputFormat)

      Returns a sanitized value for a Content-Disposition header based on the value of the RESPONSE_CONTENT_DISPOSITION_QUERY_ARG query argument.

      If the disposition is attachment and the filename is not set, it will be set to a reasonable value based on the given identifier and output format.

      Parameters:
      identifierStr - URI identifier.
      outputFormat - Requested image format.
      Returns:
      Value for a Content-Disposition header, which may be null.
    • getRequest

      public final Request getRequest()
      Returns the request being handled.
      Returns:
      The request being handled.
    • getRequestContext

      protected final RequestContext getRequestContext()
      Returns:
      Instance corresponding to the request. Its properties are gradually filled in as they become available.
    • getResponse

      public final Response getResponse()
      Returns the response to be sent.
      Returns:
      The response to be sent.
    • negotiateContentType

      protected final String negotiateContentType(List<String> limitToTypes)
      Negotiates a content/media type according to client preferences as expressed in an Accept header.
      Parameters:
      limitToTypes - Media types to limit the result to, in order of most to least preferred by the application.
      Returns:
      Negotiated media type, or null if negotiation failed.
    • setRequest

      public final void setRequest(Request request)
      Sets the request being handled.
      Parameters:
      request - Request being handled.
    • setResponse

      public final void setResponse(Response response)
      Sets the response to send.
      Parameters:
      response - Response that will be sent.