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 Summary

    Modifier and Type
    Method
    Description
    static jakarta.ws.rs.core.Response
    createLinksHandler(com.erudika.para.core.ParaObject pobj, String id2)
    Handles requests to link an object to other objects.
    static jakarta.ws.rs.core.Response
    deleteLinksHandler(com.erudika.para.core.ParaObject pobj, String id2, String type2, boolean childrenOnly)
    Handles requests to delete linked objects.
    static String
    extractAccessKey(jakarta.servlet.http.HttpServletRequest request)
    Extracts the access key from a request.
    static String
    extractDate(jakarta.servlet.http.HttpServletRequest request)
    Extracts the date field from a request.
    static String
    extractResourcePath(jakarta.servlet.http.HttpServletRequest request)
    Extracts the resource name, for example '/v1/_resource/path' returns '_resource/path'.
    static jakarta.ws.rs.core.Response
    getBatchCreateResponse(com.erudika.para.core.App app, InputStream is)
    Batch create response as JSON.
    static jakarta.ws.rs.core.Response
    getBatchDeleteResponse(com.erudika.para.core.App app, List<String> ids)
    Batch delete response as JSON.
    static jakarta.ws.rs.core.Response
    getBatchReadResponse(com.erudika.para.core.App app, List<String> ids)
    Batch read response as JSON.
    static jakarta.ws.rs.core.Response
    getBatchUpdateResponse(com.erudika.para.core.App app, Map<String,com.erudika.para.core.ParaObject> oldObjects, List<Map<String,Object>> newProperties)
    Batch update response as JSON.
    static jakarta.ws.rs.core.Response
    getCreateResponse(com.erudika.para.core.App app, String type, InputStream is)
    Create response as JSON.
    static jakarta.ws.rs.core.Response
    getDeleteResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject content)
    Delete response as JSON.
    static jakarta.ws.rs.core.Response
    getEntity(InputStream is, Class<?> type)
     
    static jakarta.ws.rs.core.Response
    getEntity(InputStream is, Class<?> type, boolean batchMode)
    Returns a Response with the entity object inside it and 200 status code.
    static jakarta.ws.rs.core.Response
    getOverwriteResponse(com.erudika.para.core.App app, String id, String type, InputStream is)
    Overwrite response as JSON.
    static com.erudika.para.core.utils.Pager
    getPagerFromParams(jakarta.ws.rs.core.MultivaluedMap<String,String> params)
    Returns a Pager instance populated from request parameters.
    static jakarta.ws.rs.core.Response
    getReadResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject content)
    Read response as JSON.
    static jakarta.ws.rs.core.Response
    getStatusResponse(jakarta.ws.rs.core.Response.Status status, String... messages)
    A generic JSON response handler.
    static jakarta.ws.rs.core.Response
    getUpdateResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject object, InputStream is)
    Update response as JSON.
    static jakarta.ws.rs.core.Response
    getVotingResponse(com.erudika.para.core.ParaObject object, Map<String,Object> entity)
    Process voting request and create vote object.
    static boolean
    hasQueryParam(String param, jakarta.ws.rs.container.ContainerRequestContext ctx)
    Returns true if parameter exists.
    static boolean
    isAnonymousRequest(jakarta.servlet.http.HttpServletRequest request)
    Check if Authorization header starts with 'Anonymous'.
    static String
    pathParam(String param, jakarta.ws.rs.container.ContainerRequestContext ctx)
    Returns the path parameter value.
    static List<String>
    pathParams(String param, jakarta.ws.rs.container.ContainerRequestContext ctx)
    Returns the path parameters values.
    static String
    queryParam(String param, jakarta.ws.rs.container.ContainerRequestContext ctx)
    Returns the query parameter value.
    static List<String>
    queryParams(String param, jakarta.ws.rs.container.ContainerRequestContext ctx)
    Returns the query parameter values.
    static jakarta.ws.rs.core.Response
    readLinksHandler(com.erudika.para.core.ParaObject pobj, String id2, String type2, jakarta.ws.rs.core.MultivaluedMap<String,String> params, com.erudika.para.core.utils.Pager pager, boolean childrenOnly)
    Handles requests to search for linked objects.
    static com.erudika.para.core.ParaObject
    Reads an object from a given resource path.
    static void
    returnObjectResponse(jakarta.servlet.http.HttpServletResponse response, Object obj)
    A generic JSON response returning an object.
    static void
    returnStatusResponse(jakarta.servlet.http.HttpServletResponse response, int status, String message)
    A generic JSON response handler.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • extractAccessKey

      public static String extractAccessKey(jakarta.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(jakarta.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(jakarta.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(jakarta.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 com.erudika.para.core.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 jakarta.ws.rs.core.Response getEntity(InputStream is, Class<?> type)
      Returns:
      response with 200 or error status
      See Also:
    • getEntity

      public static jakarta.ws.rs.core.Response getEntity(InputStream is, Class<?> type, boolean batchMode)
      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.
      batchMode - if true, checks size of individual objects only, not size of whole batch.
      Returns:
      response with 200 or error status
    • getVotingResponse

      public static jakarta.ws.rs.core.Response getVotingResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response getReadResponse(com.erudika.para.core.App app, com.erudika.para.core.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 jakarta.ws.rs.core.Response getCreateResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response getOverwriteResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response getUpdateResponse(com.erudika.para.core.App app, com.erudika.para.core.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 jakarta.ws.rs.core.Response getDeleteResponse(com.erudika.para.core.App app, com.erudika.para.core.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 jakarta.ws.rs.core.Response getBatchReadResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response getBatchCreateResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response getBatchUpdateResponse(com.erudika.para.core.App app, Map<String,com.erudika.para.core.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 jakarta.ws.rs.core.Response getBatchDeleteResponse(com.erudika.para.core.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 jakarta.ws.rs.core.Response readLinksHandler(com.erudika.para.core.ParaObject pobj, String id2, String type2, jakarta.ws.rs.core.MultivaluedMap<String,String> params, com.erudika.para.core.utils.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 jakarta.ws.rs.core.Response deleteLinksHandler(com.erudika.para.core.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 jakarta.ws.rs.core.Response createLinksHandler(com.erudika.para.core.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 com.erudika.para.core.utils.Pager getPagerFromParams(jakarta.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 jakarta.ws.rs.core.Response getStatusResponse(jakarta.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(jakarta.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(jakarta.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, jakarta.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, jakarta.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, jakarta.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, jakarta.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, jakarta.ws.rs.container.ContainerRequestContext ctx)
      Returns true if parameter exists.
      Parameters:
      param - a parameter name
      ctx - ctx
      Returns:
      true if parameter is set