Klasse DUUIRequestHelper
java.lang.Object
org.texttechnologylab.duui.api.routes.DUUIRequestHelper
A utility class to handle common operations on
Request
and Response
objects.- Autor:
- Cedric Borkowski
-
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic org.bson.Document
authenticate
(String authorization) Check if the provided authorization is valid and corresponds to a user.static String
badRequest
(spark.Response response, String message) Return a 400 - Bad Request Response.static int
getLimit
(spark.Request request) static int
getOrder
(spark.Request request, int defaultValue) static int
getSkip
(spark.Request request) static String
static String
getUserId
(spark.Request request) At this stage user should never be null.static org.bson.Document
getUserProps
(spark.Request request, Set<String> included) At this stage user should never be null.static boolean
isAuthorized
(spark.Request request) Check if the Authorization header contains a valid API key or session.static boolean
isNullOrEmpty
(String text) Checks if the given text is the empty string or null.static boolean
isNullOrEmpty
(List<?> list) Checks if the given list is of length 0 or null.static boolean
isNullOrEmpty
(org.bson.Document document) Checks if the given document has no keys or is null.static String
missingField
(spark.Response response, String field) Return a bad request response (400) to indicate a missing field.static String
notFound
(spark.Response response) Return a default 404 - Not Found Response.static String
toTitleCase
(String input) Convert each word in a string to title case.static String
unauthorized
(spark.Response response) Return a 401 - Unauthorized Response.static boolean
validateApiKey
(String key) Check if a user with the specified API key exists.static boolean
validateSession
(String session) Check if a user with the specified session exists.
-
Konstruktordetails
-
DUUIRequestHelper
public DUUIRequestHelper()
-
-
Methodendetails
-
getUserId
At this stage user should never be null. Get the user's ID by authentication.- Parameter:
request
- The Spark Request object.- Gibt zurück:
- The userID as a String.
-
getUserProps
At this stage user should never be null. Get the specified properties of the user.- Parameter:
request
- The Spark Request object.- Gibt zurück:
- A Document containing the requested user data.
-
notFound
Return a default 404 - Not Found Response.- Parameter:
response
- The Spark Response object.- Gibt zurück:
- A 404 - Not Found message.
-
badRequest
Return a 400 - Bad Request Response.- Parameter:
response
- The Spark Response object.- Gibt zurück:
- A 400 - Bad Request with a hopefully helpful message.
-
isAuthorized
public static boolean isAuthorized(spark.Request request) Check if the Authorization header contains a valid API key or session.- Parameter:
request
- The request object.- Gibt zurück:
- if the user is authorized.
-
unauthorized
Return a 401 - Unauthorized Response.- Parameter:
response
- The response object.- Gibt zurück:
- a message telling the request origin it's unauthorized to perform the action.
-
validateSession
Check if a user with the specified session exists.- Parameter:
session
- The sessionId a user receives after logging in.- Gibt zurück:
- if the sessionId is valid.
-
validateApiKey
Check if a user with the specified API key exists.- Parameter:
key
- The API key a user that can be generated on the account page.- Gibt zurück:
- if the API key is valid.
-
toTitleCase
Convert each word in a string to title case.- Parameter:
input
- The string to be transformed- Gibt zurück:
- A new string in title case
-
getLimit
public static int getLimit(spark.Request request) - Parameter:
request
- A spark requests object.- Gibt zurück:
- the
Integer
value of the `limit` query parameter or 0
-
getSkip
public static int getSkip(spark.Request request) - Parameter:
request
- A spark requests object.- Gibt zurück:
- the
Integer
value of the `skip` query parameter or 0
-
getSort
- Parameter:
request
- A spark requests object.defaultValue
- The default field name to sort by.- Gibt zurück:
- the value of the `sort` query parameter or the default value.
-
getOrder
public static int getOrder(spark.Request request, int defaultValue) - Parameter:
request
- A spark requests object.defaultValue
- The default sort order (1 = ascending, -1 = descending).- Gibt zurück:
- the value of the `order` query parameter or the default value.
-
isNullOrEmpty
Checks if the given text is the empty string or null.- Parameter:
text
- The text to check.- Gibt zurück:
- if the text is empty or null.
-
isNullOrEmpty
public static boolean isNullOrEmpty(org.bson.Document document) Checks if the given document has no keys or is null.- Parameter:
document
- TheDocument
to check.- Gibt zurück:
- if the document is empty or null.
-
isNullOrEmpty
Checks if the given list is of length 0 or null.- Parameter:
list
- TheList
to check.- Gibt zurück:
- if the list is empty or null.
-
missingField
Return a bad request response (400) to indicate a missing field.- Parameter:
response
- The response object.field
- The missing field.- Gibt zurück:
- a standard 400 bad request response.
-
authenticate
Check if the provided authorization is valid and corresponds to a user.- Parameter:
authorization
- a session id or API key.- Gibt zurück:
- The potential user corresponding to the authorization String.
-