ServiceRequest Class

Used to encapsulate an HttpServletRequest and simplify the parsing/extracting of parameters from the raw HTTP request.

Constructors

ServiceRequest( javaxt.http.servlet.HttpServletRequest request )
ServiceRequest( String service, javaxt.http.servlet.HttpServletRequest request )
ServiceRequest( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServlet servlet )
ServiceRequest( jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServlet servlet )

Public Methods

setService( String service ) returns void
Used to update the service and path variables associated with the request.
serviceA segment in the request URL used to designate the start of the path variable. The service segment should appear after the servlet. For example, the service segment in "http://localhost/servlet/service/a/b/c" is simply "service" and the resultant path is "/a/b/c". If the servlet path is undefined/missing (e.g. "http://localhost/service/a/b/c"), you can still specify "service" as the service and the resultant path would be "/a/b/c". You can even use multiple segments for the service parameter (e.g. "/path/to/service/" for "http://localhost/servlet/path/to/service/a/b/c"). If the service parameter is empty or null or simply doesn't exist in the URL, then the path variable will be set to whatever is to the right of the servlet path.
getService( ) returns String
Returns the service path in the request URL. Returns null if the service is not set.
getMethod( ) returns String
Returns a method name for the HTTP request using the first path segment returned by getPath(0) and the HTTP request method (GET, POST, etc). Examples:
  • GET "http://localhost/user" returns "getUser"
  • DELETE "http://localhost/user" returns "deleteUser"
  • POST or PUT "http://localhost/user" returns "saveUser"
Returns an empty string if the path is empty.

Note that the path starts after the servlet and service. Consider the following URL: "http://localhost/myapp/admin/user". In this example, the servlet is "myapp" and the service is "admin". The method name is derived from the first path segment after the servlet and service (i.e. "user"). An HTTP GET request to this URL would yield "getUser". See setService() and setPath() for more information.

If the request is read-only, "POST", "PUT", and "DELETE" requests will be re-mapped to "GET". See set setReadOnly().

Note that the WebService class relies on this method to map service requests to REST service endpoints and execute CRUD operations.
setReadOnly( boolean readOnly ) returns void
Used to enable/disable insert, update, and delete operations by updating the string returned by getMethod().
readOnlyIf true, "POST", "PUT", and "DELETE" requests are remapped to "GET" for getMethod().
isReadOnly( ) returns boolean
Returns true if insert, update, and delete operations have been disabled. See setReadOnly() for more information. Default is false.
getPath( int i ) returns javaxt.utils.Value
Returns a segment from the requested URL path at a given index. Note that the path starts after the servlet and service. See setService() and setPath() for more information on how the path variable is set.
iIndex number, starting from 0. For example, index 0 for "http://localhost/servlet/service/a/b/c" would yield "a".
getPath( ) returns String
Returns a part of the requested URL path after the servlet and service. For example, "http://localhost/servlet/service/a/b/c" would yield "/a/b/c". See setService() and setPath() for more information on how the path variable is set.
setPath( String path ) returns void
Used to update the path variable associated with the request. The path variable is essential for several methods in this class (e.g. getId and getMethod) and is critical for routing requests to different web methods in the WebService class. This method is called whenever the service is updated (see setService).
pathURL path after the servlet path. For example, if a URL follows a pattern like "http://localhost/servlet/service/a/b/c", you should provide everything to the right of the servlet (i.e. "/service/a/b/c"). If a service is defined, the resultant path would be "/a/b/c". If a service is not defined, the resultant path would be "/service/a/b/c".
getID( ) returns Long
Returns the ID associated with the request. Assuming the service request follows the convention "http://localhost/servlet/service/object", the ID for the "http://localhost/photos/config/user/54" is 54. If an ID is not found in the path or is invalid, then the id parameter in the query string is returned. Example: "http://localhost/photos/config/user?id=54"
getURL( ) returns javaxt.utils.URL
Returns the original url used to make the request.
getParameter( String key ) returns javaxt.utils.Value
Returns the value associated with a parameter in the request. Performs a case insensitive search for the keyword in the query string. In addition, will search the JSON payload of the request if parseJson is set to true. If the value is an empty string or "null" then a null value is returned.
hasParameter( String key ) returns boolean
Returns true if the request contains a given parameter in the request. Performs a case insensitive search for the keyword in the query string. In addition, will search the JSON payload of the request if parseJson is set to true.
setParameter( String key, String val ) returns void
Used to update a parameter extracted from the original request. Performs a case insensitive search for the keyword.
removeParameter( String key ) returns void
Used to remove a parameter from the request. Performs a case insensitive search for the keyword.
getParameterNames( ) returns String[]
Returns a list of all the parameter keywords found in this request.
getOffset( ) returns Long
Returns the value of the "offset" parameter in the request as a number. This parameter is used by the WebService class to paginate through list requests.
getLimit( ) returns Long
Returns the value of the "limit" parameter in the request as a number. This parameter is used by the WebService class to paginate through list requests.
getRequest( ) returns HttpServletRequest
Returns the original, unmodified HTTP request used to instantiate this class.
setPayload( byte[] payload ) returns void
Used to update the raw bytes representing the payload of the request
getPayload( ) returns byte[]
Returns the raw bytes from the payload of the request
getJson( ) returns JSONObject
Returns the payload of the request as a JSON object
parseJson( ) returns void
Calling this method will expand parameter searches into the payload of the request. See getParameter() for more info.
getUser( ) returns java.security.Principal
Returns the user associated with the request
getCredentials( ) returns String[]
Returns the credentials associated with an HTTP request. The credentials will vary based on the security authentication scheme used to authenticate clients (e.g. "BASIC", "DIGEST", "NTLM", etc). In the case of "BASIC" authentication, the credentials typically include a username and password. In the case of "NTLM" authentication, the credentials may only contain a username. What is actually returned is up to the javaxt.http.servlet.Authenticator used to authenticate the request.
authenticate( ) returns void
Used to authenticate a client request. Authentication is performed by a javaxt.http.servlet.Authenticator. If no Authenticator is defined or if the Authenticator fails to authenticate the client, this method throws a ServletException.
isCacheable( String eTag, String date ) returns boolean
Returns true if a given eTag matches the "if-none-match" request header. Additional checks are performed against other cache related headers (e.g. "cache-control" and "if-modified-since"). If true is returned, the corresponding HTTP response can be set to a 304 "Not Modified".
eTagA custom string that acts as a unique identifier (including version) for an HTTP response. ETags are frequently used when sending static data such as files. In the context of dynamic web services, the same concept can be applied for static, or semi-static responses (e.g. static keywords, images in a database, etc).
dateA date associated with the HTTP response. The date should be in GMT and in "EEE, dd MMM yyyy HH:mm:ss zzz" format (e.g. "Sat, 23 Oct 2010 13:04:28 GMT").
getFields( ) returns Field[]
Returns an array of ServiceRequest.Fields by parsing the "fields" parameter in the HTTP request (e.g. "?fields=id,firstName,lastName").
getFields( String fields ) returns Field[]
Used to parse a given String into an array of Fields.
fieldsA comma delimited list of fields (e.g. "id,firstName,lastName")
getFilter( ) returns Filter
Returns a ServiceRequest.Filter by parsing the query string associated this request. Examples:
  • http://localhost?id=1 (id = 1)
  • http://localhost?id>=1 (id > 1)
  • http://localhost?id!=1 (id <> 1)
  • http://localhost?id=1,2,3 (id in (1,2,3))
  • http://localhost?id!=1,2,3 (id not in (1,2,3))
  • http://localhost?active=true (active = true)
  • http://localhost?name='Bob' (name = 'Bob')
  • http://localhost?name='Bo%' (name like 'Bo%')
  • http://localhost?name!='Bo%' (name not like 'Bo%')
Note that the operators can be transposed without altering the filter (e.g. ">=" is the same as "=>").

Alternatively, a Filter can be generated by parsing a "filter" parameter containing JSON object. In fact, if a "filter" parameter is provided, it will be used instead of the query string.

getWhere( ) returns String
Returns the value for the "where" parameter in the HTTP request.
getSort( ) returns Sort
Returns an order by statement found in the request (e.g. "orderby" query string). The order by statement may be given as a comma delimited list of fields with optional sort direction for each field. Alternatively, the order by statement can be specified as a JSON array with a "property" and "direction" for each entry in the array. The order by statement is encapsulated as an instance of the Sort class.
getKeywords( ) returns HashSet<String>
Returns a copy of all the keywords used to identify parameters (e.g. "fields", "orderby", "offset", "limit", etc)
setKeyword( String keyword, String type ) returns void
Used to add or update an entry in the keyword map. For example, the default keyword used to identify fields in the getFields() method is "fields" and the default keyword used to identify sorting in getSort() if "orderby". This method provides a mechanism for overridding these defaults. For example, suppose in your application you wish to use "columns" instead of "fields" for the getFields() method. You would set the keyword to "columns" and type to "fields".
keywordA parameter keyword.
typeWhat the given parameter should map to (e.g. "fields")
getKeyword( String type ) returns String
Returns a keyword for a given type (see setKeyword for more information)
getSelectStatement( String tableName ) returns String
Returns a SQL "select" statement for the current request. Compiles the select statement using an array of Fields returned by the getFields() method. Prepends a table name to each field, if given. If no fields are found in the request, a "select *" statement is returned. Otherwise, a select statement is returned, starting with "select ".
tableNameIf given, fields that are not functions are prepended with a table name. This parameter is optional.
getSelectStatement( Class... model ) returns String
Returns a SQL "select" statement for the current request. Compiles the "select" statement using using an array of Fields returned by the getFields() method. Prepends a table name to each field by mapping fields to models.
modelOne or more Model classes used to identify fields. If a field is matched to a model the field name (column name) is prepended with a table name. Order is important.
getWhereStatement( Class... c ) returns String
Returns a SQL "where" statement for the current request. Compiles the "where" statement using Filter class returned by the getFilter() method. If the Filter is empty, will use raw value for the "where" parameter returned by the getWhere() method instead. Returns an empty string if the Filter is empty and the "where" parameter is not defined. Otherwise, a where statement is returned, starting with a white space " " for convenience.
cModel classes used to validate fields in the filter. This parameter is optional.
getOrderByStatement( ) returns String
Returns a SQL order by statement for the current request. Compiles the order by statement using Sort class returned by the getSort() method. Returns an empty string if a sort was not defined. Otherwise, the order by statement returned, starting with a white space " " for convenience.
getOffsetLimitStatement( javaxt.sql.Driver driver ) returns String
Returns a SQL offset and limit statement for the current request. These statements are used for pagination. Note that different database vendors use different keywords to specify offset and limit. The given Driver is used to determine which keywords to use. Returns an empty string if limit and is offset are not defined. Otherwise, the limit and/or offset statement is returned, starting with a white space " " for convenience.
driverAn instance of a javaxt.sql.Driver class. This parameter is optional.

Public Classes