ServiceRequest Class

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

Constructors

ServiceRequest( HttpServletRequest request )
ServiceRequest( String service, HttpServletRequest request )

Public Methods

getService( ) returns String
Returns the service name from the http request. Service requests follow the convention: "http://localhost/servlet/service/path". For example "http://localhost/myapp/admin/user". In this example, the servlet path is "myapp" and the service name is "admin". Note that the servlet path is optional and may include multiple "directories". This method makes it easier to find the service name from the url.
getMethod( ) returns String
Returns a method name for the HTTP request. Examples:
  • GET "http://localhost/user" returns "getUser"
  • DELETE "http://localhost/user" returns "deleteUser"
  • POST or PUT "http://localhost/user" returns "saveUser"
If the request is read-only, "POST", "PUT", and "DELETE" requests will be re-mapped to "GET".

If the URL contains a "servlet" path or a "service" path, will return the first object in the path after the servlet and/or service. Consider this example: "http://localhost/myapp/admin/user" In this example, the servlet path is "myapp" and the service path is "admin" and and so the method name is derived from "user".

Note that this method is used by the WebService class to map service requests to REST service endpoints and execute CRUD operations.
setReadOnly( boolean readOnly ) returns void
Used to disable insert, update, and delete operations. "POST", "PUT", and "DELETE" requests are remapped to "GET". See getMethod() for more information on how requests are mapped.
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 part of the url path at a given index AFTER the service name. For example, index 0 for "http://localhost/servlet/service/a/b/c" would yield "a".
getPath( ) returns String
Returns a part of the url path AFTER the service name. For example, "http://localhost/servlet/service/a/b/c" would yield "/a/b/c".
setPath( String path ) returns void
Used to update the path of the current URL. This method can be used to coerce a request to route to different web methods (see getMethod).
pathURL path, excluding servlet and service path. For example, if a URL follows the follows a pattern like "http://localhost/servlet/service/a/b/c" the path is "/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.
keyQuery string parameter name. Performs a case insensitive search for the keyword.
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 in the query string.
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