public abstract class BaseResource extends Object
Modifier and Type | Field and Description |
---|---|
protected static String |
DATE_FORMAT |
protected static String |
DATETIME_FORMAT |
protected static String |
DEFAULT_USER |
protected org.slf4j.Logger |
log |
static String |
NAME |
protected static javax.ws.rs.core.CacheControl |
NO_CACHE
Cache control descriptor to force no-caching.
|
static String |
REF |
protected javax.ws.rs.core.SecurityContext |
security |
static String |
UID |
protected javax.ws.rs.core.UriInfo |
uriInfo |
Modifier | Constructor and Description |
---|---|
protected |
BaseResource(org.slf4j.Logger log)
Create a resource capable of logging to the specified logger.
|
Modifier and Type | Method and Description |
---|---|
javax.ws.rs.core.Response.ResponseBuilder |
accepted()
Create an accepted response (202) with no caching.
|
javax.ws.rs.core.Response.ResponseBuilder |
accepted(URI location)
Create an accepted response (202) with no caching and a location header.
|
javax.ws.rs.core.Response.ResponseBuilder |
created(URI location)
Create a created response with no-cache headers.
|
javax.ws.rs.core.Response.ResponseBuilder |
created(URI location,
Object data)
Create a created response with no-cache headers and with the given
response data.
|
static Date |
date(String stamp)
Parse the formated date-time stamp.
|
protected String |
datetime(long millis)
Auxiliary to format milliseconds timestamps into a standard format.
|
javax.ws.rs.core.Response.ResponseBuilder |
deleted()
Create a deleted response with no-cache headers.
|
static String |
format(Date date)
Get the formated date-time stamp for the given date.
|
static String |
format(long number)
Get the formated number.
|
org.apache.commons.configuration.XMLConfiguration |
getData(String root,
String request)
Get the XML configuration created from the specified request data and
rooted at the specified element.
|
static String |
getUserName(Principal principal)
Get the user name for the specified principal.
|
Integer |
id(String sid)
Get the integer variant of the specified string ID.
|
int |
integer(String string)
Get the integer variant of the specified string number.
|
javax.ws.rs.WebApplicationException |
notFound()
Get an exception for signaling that a resource was not found.
|
javax.ws.rs.core.Response.ResponseBuilder |
ok()
Create a no-content (204) response with no caching.
|
javax.ws.rs.core.Response.ResponseBuilder |
ok(Object data)
Create a pre-populated response with no-cache headers and with the
given response data.
|
protected javax.ws.rs.core.Response.ResponseBuilder |
redirect(String path)
Returns redirect response to the specified path.
|
static String |
ref(javax.ws.rs.core.UriInfo ui,
Class<?> resourceClass,
String uid)
Construct a reference to the resource of the specified type and with
the given unique id.
|
static String |
safeCamelCase(String prefix,
Enum<?> e)
Protect against values that are null when creating camel case
representations of enumerations.
|
static String |
safeToString(Object o)
Protects against values that are null.
|
javax.ws.rs.WebApplicationException |
serverError()
Get an exception for signaling that a server error has occurred.
|
javax.ws.rs.WebApplicationException |
serviceUnavailableError()
Get an exception for signaling that a requested service is unavailable.
|
static void |
setDefaultUserName(String userName)
Sets the default user name, which will be used when no current
principal context exists.
|
String |
sid(Integer id)
Get the string variant of the specified integer ID.
|
static WebUtils.Tag |
tag(String name,
Object content)
Create a new XML tag with the specified name and content.
|
static WebUtils.Tag |
tag(javax.ws.rs.core.UriInfo ui,
Class<?> resourceClass,
String name,
Identified entity)
Produces an XML tag with embedded uid and ref elements.
|
static WebUtils.Tag |
tag(javax.ws.rs.core.UriInfo ui,
Class<?> resourceClass,
String name,
String uid)
Produces an XML tag with embedded uid and ref elements.
|
String |
uid(String urlPath)
Extracts entity uid from the specified URL path.
|
String |
uid(URL url)
Extracts entity uid from the specified URL.
|
URL |
url(String path)
Generate a URL using the supplied path.
|
protected String |
validate(String uid)
Validate the specified uid to make sure it's not null or empty.
|
protected void |
validate(String uid,
String uidr)
Validate the specified uids against each other.
|
protected <T> T |
validate(T entity)
Validates that the specified entity is not null.
|
javax.ws.rs.WebApplicationException |
warn(String message,
Object arg1,
Object arg2,
javax.ws.rs.core.Response.Status status)
Deprecated.
|
javax.ws.rs.WebApplicationException |
warn(String message,
Object arg,
javax.ws.rs.core.Response.Status status)
Deprecated.
|
public static final String UID
public static final String REF
public static final String NAME
protected static final String DATETIME_FORMAT
protected static final String DATE_FORMAT
protected static final javax.ws.rs.core.CacheControl NO_CACHE
protected final org.slf4j.Logger log
@Context protected javax.ws.rs.core.SecurityContext security
@Context protected javax.ws.rs.core.UriInfo uriInfo
protected static final String DEFAULT_USER
protected BaseResource(org.slf4j.Logger log)
log
- loggerpublic static void setDefaultUserName(String userName)
userName
- new default user namepublic static String getUserName(Principal principal)
principal
- user or group principal contextprotected <T> T validate(T entity)
T
- type of entity being validatedentity
- entity to be validatedjavax.ws.rs.WebApplicationException
- with 404 code if entity is nullpublic static WebUtils.Tag tag(String name, Object content)
name
- tag namecontent
- tag contentpublic static WebUtils.Tag tag(javax.ws.rs.core.UriInfo ui, Class<?> resourceClass, String name, Identified entity)
ui
- request contextresourceClass
- resource classname
- name of the tagentity
- entity from which to extract uidpublic static WebUtils.Tag tag(javax.ws.rs.core.UriInfo ui, Class<?> resourceClass, String name, String uid)
ui
- request contextresourceClass
- resource classname
- name of the taguid
- unique id of the resourceprotected void validate(String uid, String uidr)
uid
- uid from the resource refuidr
- uid from the request bodyjavax.ws.rs.WebApplicationException
- with bad request code if the given uids
do not matchprotected String validate(String uid)
uid
- uid from the resource refjavax.ws.rs.WebApplicationException
- with bad request code if the id is null
or emptypublic URL url(String path)
path
- url string pathjavax.ws.rs.WebApplicationException
- with internal error the given path does
not form a valid URLpublic String uid(URL url)
url
- url from which the uid is to be extractedpublic String uid(String urlPath)
urlPath
- url path from which the uid is to be extractedjavax.ws.rs.WebApplicationException
- with internal error if UID could not be
extracted from the given URL pathpublic static String ref(javax.ws.rs.core.UriInfo ui, Class<?> resourceClass, String uid)
ui
- base uri inforesourceClass
- resource typeuid
- unique id of the entitypublic org.apache.commons.configuration.XMLConfiguration getData(String root, String request)
root
- root element namerequest
- XML-encoded request datajavax.ws.rs.WebApplicationException
- with bad request if data could not be
extractedpublic javax.ws.rs.core.Response.ResponseBuilder ok()
public javax.ws.rs.core.Response.ResponseBuilder accepted()
public javax.ws.rs.core.Response.ResponseBuilder accepted(URI location)
location
- URI locationpublic javax.ws.rs.core.Response.ResponseBuilder ok(Object data)
data
- response datapublic javax.ws.rs.core.Response.ResponseBuilder created(URI location)
location
- URI locationpublic javax.ws.rs.core.Response.ResponseBuilder created(URI location, Object data)
location
- URI locationdata
- created datapublic javax.ws.rs.core.Response.ResponseBuilder deleted()
protected javax.ws.rs.core.Response.ResponseBuilder redirect(String path)
The provided path string will be normalized (see
URI.normalize()
) prior to the creation of the redirect
response. It is recommended that an absolute path be provided if path
normalization is expected.
path
- path to which redirect should leadjavax.ws.rs.WebApplicationException
- with internal error if redirect failedpublic javax.ws.rs.WebApplicationException notFound()
public javax.ws.rs.WebApplicationException serverError()
public javax.ws.rs.WebApplicationException serviceUnavailableError()
@Deprecated public javax.ws.rs.WebApplicationException warn(String message, Object arg, javax.ws.rs.core.Response.Status status)
message
- message to log as a warningarg
- object to use in lieu of a place-holder in the messagestatus
- response status@Deprecated public javax.ws.rs.WebApplicationException warn(String message, Object arg1, Object arg2, javax.ws.rs.core.Response.Status status)
message
- message to log as a warningarg1
- object to use in lieu of the first placeholder in the
messagearg2
- object to use in lieu of the second placeholder in the
messagestatus
- response statuspublic String sid(Integer id)
id
- entity IDpublic Integer id(String sid)
sid
- entity IDjavax.ws.rs.WebApplicationException
- with bad request if the given id is not
a numberpublic static String format(long number)
number
- number to be formattedpublic static String format(Date date)
date
- date to be formattedpublic static Date date(String stamp)
stamp
- date-time stamp stringprotected String datetime(long millis)
millis
- time since epochpublic int integer(String string)
string
- string containing numberjavax.ws.rs.WebApplicationException
- with bad request if the given string
does not parse into a numberpublic static String safeToString(Object o)
Object.toString()
.o
- the object to be formatted (may be null)Copyright © 2015. All Rights Reserved.