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 org.springframework.http.ResponseEntity<?>
    createLinksHandler(com.erudika.para.core.ParaObject pobj, String id2)
    Handles requests to link an object to other objects.
    static org.springframework.http.ResponseEntity<?>
    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 org.springframework.http.ResponseEntity<?>
    getBatchCreateResponse(com.erudika.para.core.App app, InputStream is)
    Batch create response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getBatchDeleteResponse(com.erudika.para.core.App app, List<String> ids)
    Batch delete response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getBatchReadResponse(com.erudika.para.core.App app, List<String> ids)
    Batch read response as JSON.
    static org.springframework.http.ResponseEntity<?>
    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 org.springframework.http.ResponseEntity<?>
    getCreateResponse(com.erudika.para.core.App app, String type, InputStream is)
    Create response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getDeleteResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject content)
    Delete response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getEntity(InputStream is, Class<?> type)
    Convenience overload that reads an entity from the request stream without batch processing.
    static org.springframework.http.ResponseEntity<?>
    getEntity(InputStream is, Class<?> type, boolean batchMode)
    Returns a Response with the entity object inside it and 200 status code.
    static org.springframework.http.ResponseEntity<?>
    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.servlet.http.HttpServletRequest req)
    Returns a Pager instance populated from request parameters.
    static org.springframework.http.ResponseEntity<?>
    getReadResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject content)
    Read response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getStatusResponse(org.springframework.http.HttpStatus status, String... messages)
    A generic JSON response handler.
    static org.springframework.http.ResponseEntity<?>
    getUpdateResponse(com.erudika.para.core.App app, com.erudika.para.core.ParaObject object, InputStream is)
    Update response as JSON.
    static org.springframework.http.ResponseEntity<?>
    getVotingResponse(com.erudika.para.core.ParaObject object, Map<String,Object> entity)
    Process voting request and create vote object.
    static boolean
    isAnonymousRequest(jakarta.servlet.http.HttpServletRequest request)
    Check if Authorization header starts with 'Anonymous'.
    static String
    queryParam(String param, jakarta.servlet.http.HttpServletRequest req)
    Returns the query parameter value.
    static List<String>
    queryParams(String param, jakarta.servlet.http.HttpServletRequest req)
    Returns the query parameter values.
    static org.springframework.http.ResponseEntity<?>
    readLinksHandler(com.erudika.para.core.ParaObject pobj, String id2, String type2, com.erudika.para.core.utils.Pager pager, boolean childrenOnly, jakarta.servlet.http.HttpServletRequest req)
    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 org.springframework.http.ResponseEntity<?> getEntity(InputStream is, Class<?> type)
      Convenience overload that reads an entity from the request stream without batch processing.
      Parameters:
      is - the raw request stream
      type - the expected entity type
      Returns:
      response with 200 or error status
      See Also:
    • getEntity

      public static org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> readLinksHandler(com.erudika.para.core.ParaObject pobj, String id2, String type2, com.erudika.para.core.utils.Pager pager, boolean childrenOnly, jakarta.servlet.http.HttpServletRequest req)
      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
      pager - a Pager object
      childrenOnly - find only directly linked objects in 1-to-many relationship
      req - request
      Returns:
      a Response
    • deleteLinksHandler

      public static org.springframework.http.ResponseEntity<?> 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 org.springframework.http.ResponseEntity<?> 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.servlet.http.HttpServletRequest req)
      Returns a Pager instance populated from request parameters.
      Parameters:
      req - request
      Returns:
      a Pager
    • getStatusResponse

      public static org.springframework.http.ResponseEntity<?> getStatusResponse(org.springframework.http.HttpStatus 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
    • queryParam

      public static String queryParam(String param, jakarta.servlet.http.HttpServletRequest req)
      Returns the query parameter value.
      Parameters:
      param - a parameter name
      req - request
      Returns:
      parameter value
    • queryParams

      public static List<String> queryParams(String param, jakarta.servlet.http.HttpServletRequest req)
      Returns the query parameter values.
      Parameters:
      param - a parameter name
      req - request
      Returns:
      a list of values