Class RestUtils

java.lang.Object
com.erudika.para.server.rest.RestUtils

public final class RestUtils extends Object
A few helper methods for handling REST requests and responses.
Author:
Alex Bogdanovski [[email protected]]
  • Method Details

    • extractAccessKey

      public static String extractAccessKey(javax.servlet.http.HttpServletRequest request)
      Extracts the access key from a request. It can be a header or a parameter.
      Parameters:
      request - a request
      Returns:
      the access key
    • isAnonymousRequest

      public static boolean isAnonymousRequest(javax.servlet.http.HttpServletRequest request)
      Check if Authorization header starts with 'Anonymous'. Used for guest access.
      Parameters:
      request - a request
      Returns:
      true if user is unauthenticated
    • extractDate

      public static String extractDate(javax.servlet.http.HttpServletRequest request)
      Extracts the date field from a request. It can be a header or a parameter.
      Parameters:
      request - a request
      Returns:
      the date
    • extractResourcePath

      public static String extractResourcePath(javax.servlet.http.HttpServletRequest request)
      Extracts the resource name, for example '/v1/_resource/path' returns '_resource/path'.
      Parameters:
      request - a request
      Returns:
      the resource path
    • readResourcePath

      public static ParaObject readResourcePath(String appid, String path)
      Reads an object from a given resource path. Assumes that the "id" is located after the first slash "/" like this: {type}/{id}/...
      Parameters:
      appid - app id
      path - resource path
      Returns:
      the object found at this path or null
    • getEntity

      public static javax.ws.rs.core.Response getEntity(InputStream is, Class<?> type)
      Returns a Response with the entity object inside it and 200 status code. If there was and error the status code is different than 200.
      Parameters:
      is - the entity input stream
      type - the type to convert the entity into, for example a Map. If null, this returns the InputStream.
      Returns:
      response with 200 or error status
    • getVotingResponse

      public static javax.ws.rs.core.Response getVotingResponse(ParaObject object, Map<String,Object> entity)
      Process voting request and create vote object.
      Parameters:
      object - the object to cast vote on
      entity - request entity data
      Returns:
      status codetrue if vote was successful
    • getReadResponse

      public static javax.ws.rs.core.Response getReadResponse(App app, ParaObject content)
      Read response as JSON.
      Parameters:
      app - the app object
      content - the object that was read
      Returns:
      status code 200 or 404
    • getCreateResponse

      public static javax.ws.rs.core.Response getCreateResponse(App app, String type, InputStream is)
      Create response as JSON.
      Parameters:
      app - the app object
      type - type of the object to create
      is - entity input stream
      Returns:
      a status code 201 or 400
    • getOverwriteResponse

      public static javax.ws.rs.core.Response getOverwriteResponse(App app, String id, String type, InputStream is)
      Overwrite response as JSON.
      Parameters:
      app - the app object
      id - the object id
      type - type of the object to create
      is - entity input stream
      Returns:
      a status code 200 or 400
    • getUpdateResponse

      public static javax.ws.rs.core.Response getUpdateResponse(App app, ParaObject object, InputStream is)
      Update response as JSON.
      Parameters:
      app - the app object
      object - object to validate and update
      is - entity input stream
      Returns:
      a status code 200 or 400 or 404
    • getDeleteResponse

      public static javax.ws.rs.core.Response getDeleteResponse(App app, ParaObject content)
      Delete response as JSON.
      Parameters:
      app - the current App object
      content - the object to delete
      Returns:
      a status code 200 or 400
    • getBatchReadResponse

      public static javax.ws.rs.core.Response getBatchReadResponse(App app, List<String> ids)
      Batch read response as JSON.
      Parameters:
      app - the current App object
      ids - list of ids
      Returns:
      status code 200 or 400
    • getBatchCreateResponse

      public static javax.ws.rs.core.Response getBatchCreateResponse(App app, InputStream is)
      Batch create response as JSON.
      Parameters:
      app - the current App object
      is - entity input stream
      Returns:
      a status code 200 or 400
    • getBatchUpdateResponse

      public static javax.ws.rs.core.Response getBatchUpdateResponse(App app, Map<String,ParaObject> oldObjects, List<Map<String,Object>> newProperties)
      Batch update response as JSON.
      Parameters:
      app - the current App object
      oldObjects - a list of old objects read from DB
      newProperties - a list of new object properties to be updated
      Returns:
      a status code 200 or 400
    • getBatchDeleteResponse

      public static javax.ws.rs.core.Response getBatchDeleteResponse(App app, List<String> ids)
      Batch delete response as JSON.
      Parameters:
      app - the current App object
      ids - list of ids to delete
      Returns:
      a status code 200 or 400
    • readLinksHandler

      public static javax.ws.rs.core.Response readLinksHandler(ParaObject pobj, String id2, String type2, javax.ws.rs.core.MultivaluedMap<String,String> params, Pager pager, boolean childrenOnly)
      Handles requests to search for linked objects.
      Parameters:
      pobj - the object to operate on
      id2 - the id of the second object (optional)
      type2 - the type of the second object
      params - query parameters
      pager - a Pager object
      childrenOnly - find only directly linked objects in 1-to-many relationship
      Returns:
      a Response
    • deleteLinksHandler

      public static javax.ws.rs.core.Response deleteLinksHandler(ParaObject pobj, String id2, String type2, boolean childrenOnly)
      Handles requests to delete linked objects.
      Parameters:
      pobj - the object to operate on
      id2 - the id of the second object (optional)
      type2 - the type of the second object
      childrenOnly - find only directly linked objects in 1-to-many relationship
      Returns:
      a Response
    • createLinksHandler

      public static javax.ws.rs.core.Response createLinksHandler(ParaObject pobj, String id2)
      Handles requests to link an object to other objects.
      Parameters:
      pobj - the object to operate on
      id2 - the id of the second object (optional)
      Returns:
      a Response
    • getPagerFromParams

      public static Pager getPagerFromParams(javax.ws.rs.core.MultivaluedMap<String,String> params)
      Returns a Pager instance populated from request parameters.
      Parameters:
      params - query params map
      Returns:
      a Pager
    • getStatusResponse

      public static javax.ws.rs.core.Response getStatusResponse(javax.ws.rs.core.Response.Status status, String... messages)
      A generic JSON response handler.
      Parameters:
      status - status code
      messages - zero or more errors
      Returns:
      a response as JSON
    • returnStatusResponse

      public static void returnStatusResponse(javax.servlet.http.HttpServletResponse response, int status, String message)
      A generic JSON response handler. Returns a message and response code.
      Parameters:
      response - the response to write to
      status - status code
      message - error message
    • returnObjectResponse

      public static void returnObjectResponse(javax.servlet.http.HttpServletResponse response, Object obj)
      A generic JSON response returning an object. Status code is always 200.
      Parameters:
      response - the response to write to
      obj - an object
    • pathParam

      public static String pathParam(String param, javax.ws.rs.container.ContainerRequestContext ctx)
      Returns the path parameter value.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      a value
    • pathParams

      public static List<String> pathParams(String param, javax.ws.rs.container.ContainerRequestContext ctx)
      Returns the path parameters values.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      a list of parameter values
    • queryParam

      public static String queryParam(String param, javax.ws.rs.container.ContainerRequestContext ctx)
      Returns the query parameter value.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      parameter value
    • queryParams

      public static List<String> queryParams(String param, javax.ws.rs.container.ContainerRequestContext ctx)
      Returns the query parameter values.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      a list of values
    • hasQueryParam

      public static boolean hasQueryParam(String param, javax.ws.rs.container.ContainerRequestContext ctx)
      Returns true if parameter exists.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      true if parameter is set