java.lang.Object
com.erudika.para.server.rest.RestUtils
A few helper methods for handling REST requests and responses.
- Author:
- Alex Bogdanovski [[email protected]]
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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) 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 aPager
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
Returns the path parameter value.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.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
readResourcePath
(String appid, String path) 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.
-
Method Details
-
extractAccessKey
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
Extracts the date field from a request. It can be a header or a parameter.- Parameters:
request
- a request- Returns:
- the date
-
extractResourcePath
Extracts the resource name, for example '/v1/_resource/path' returns '_resource/path'.- Parameters:
request
- a request- Returns:
- the resource path
-
readResourcePath
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 idpath
- resource path- Returns:
- the object found at this path or null
-
getEntity
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 streamtype
- 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
-
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 objectcontent
- 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 objecttype
- type of the object to createis
- 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 objectid
- the object idtype
- type of the object to createis
- 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 objectobject
- object to validate and updateis
- 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 objectcontent
- the object to delete- Returns:
- a status code 200 or 400
-
getBatchReadResponse
-
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 objectis
- 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 objectoldObjects
- a list of old objects read from DBnewProperties
- a list of new object properties to be updated- Returns:
- a status code 200 or 400
-
getBatchDeleteResponse
-
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 onid2
- the id of the second object (optional)type2
- the type of the second objectparams
- query parameterspager
- aPager
objectchildrenOnly
- 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 onid2
- the id of the second object (optional)type2
- the type of the second objectchildrenOnly
- 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 onid2
- the id of the second object (optional)- Returns:
- a Response
-
getPagerFromParams
-
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 codemessages
- 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 tostatus
- status codemessage
- error message
-
returnObjectResponse
public static void returnObjectResponse(jakarta.servlet.http.HttpServletResponse response, Object obj) A generic JSON response returning an object. Status code is always200
.- Parameters:
response
- the response to write toobj
- an object
-
pathParam
-
pathParams
-
queryParam
-
queryParams
-
hasQueryParam
public static boolean hasQueryParam(String param, jakarta.ws.rs.container.ContainerRequestContext ctx) Returns true if parameter exists.- Parameters:
param
- a parameter namectx
- ctx- Returns:
- true if parameter is set
-