Package com.erudika.para.server.rest
Class RestUtils
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 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 StringextractAccessKey(jakarta.servlet.http.HttpServletRequest request) Extracts the access key from a request.static StringextractDate(jakarta.servlet.http.HttpServletRequest request) Extracts the date field from a request.static StringextractResourcePath(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.PagergetPagerFromParams(jakarta.servlet.http.HttpServletRequest req) Returns aPagerinstance 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 booleanisAnonymousRequest(jakarta.servlet.http.HttpServletRequest request) Check if Authorization header starts with 'Anonymous'.static StringqueryParam(String param, jakarta.servlet.http.HttpServletRequest req) Returns the query parameter value.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.ParaObjectreadResourcePath(String appid, String path) Reads an object from a given resource path.static voidreturnObjectResponse(jakarta.servlet.http.HttpServletResponse response, Object obj) A generic JSON response returning an object.static voidreturnStatusResponse(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
Convenience overload that reads an entity from the request stream without batch processing.- Parameters:
is- the raw request streamtype- 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 streamtype- 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 onentity- 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 objectcontent- 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 objecttype- type of the object to createis- 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 objectid- the object idtype- type of the object to createis- 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 objectobject- object to validate and updateis- 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 objectcontent- 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 objectids- 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 objectis- 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 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
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 objectids- 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 onid2- the id of the second object (optional)type2- the type of the second objectpager- aPagerobjectchildrenOnly- find only directly linked objects in 1-to-many relationshipreq- 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 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 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 onid2- 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 aPagerinstance 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 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
-
queryParam
Returns the query parameter value.- Parameters:
param- a parameter namereq- request- Returns:
- parameter value
-
queryParams
Returns the query parameter values.- Parameters:
param- a parameter namereq- request- Returns:
- a list of values
-