public class LinkUtils extends GenericService
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
AMPERSAND
The AMPERSAND constant.
|
static java.lang.String |
ARRAY_KEY
The ARRAY_KEY constant.
|
static java.lang.String |
AUTO_WRAP_KEY
The AUTO_WRAP_KEY constant.
|
static java.lang.String |
CANONICAL_KEY
The CANONICAL constant.
|
static java.lang.String |
CLASS_VERSION
Class version string
|
static java.lang.String |
COLLECTION_REL
IANA relation name for a link to an owner collection
|
static java.lang.String |
COLON
The COLON constant.
|
static java.lang.String |
COMMA
The COMMA constant.
|
static java.lang.String |
COUNT
The COUNT property constant.
|
static java.lang.String |
CURIES
The CURIES constant.
|
static java.lang.String |
DEFAULT_LINK_UTILS_PATH
A default configuration of an instance of this service
|
static java.lang.String |
DOT
The DOT constant.
|
static java.lang.String |
DOT_ESC
The DOT_ESC constant.
|
static java.lang.String |
EMPTY_STRING
The EMPTY_STRING constant.
|
static java.lang.String |
EQUALS
The EQUALS constant.
|
static java.lang.String |
FIELDS_PARAM_KEY
The FIELDS_PARAM_KEY constant.
|
static java.lang.String |
FIRST_KEY
The FIRST_KEY constant.
|
static java.lang.String |
HAS_MORE
The HAS_MORE property constant.
|
static java.lang.String |
HREF
The HREF constant.
|
static java.lang.String |
HREF_KEY
The HREF_KEY constant.
|
static java.lang.String |
LAST_KEY
The LAST_KEY constant.
|
static java.lang.String |
LEFT_CURLY
The LEFT_CURLY constant.
|
static java.lang.String |
LEFT_PAREN
The LEFT_PAREN constant.
|
static java.lang.String |
LEFT_PAREN_ESC
The LEFT_PAREN_ESC constant.
|
static java.lang.String |
LIMIT
The LIMIT property constant.
|
static java.lang.String |
LINKS
The LINKS constant.
|
static java.lang.String |
MEDIA_TYPE
The MEDIA_TYPE constant.
|
static java.lang.String |
METHOD
The METHOD constant.
|
static java.lang.String |
NAME
The NAME constant.
|
static java.lang.String |
NEXT_KEY
The NEXT_KEY constant.
|
static java.lang.String |
NEXT_LIMIT
The NEXT_LIMIT constant.
|
static java.lang.String |
NEXT_OFFSET
The NEXT_OFFSET constant.
|
static java.lang.String |
NEXT_TOTAL
The NEXT_TOTAL constant.
|
static java.lang.String |
OFFSET
The OFFSET property constant.
|
static java.lang.String |
PREV_KEY
The PREV_KEY constant.
|
static java.lang.String |
PROFILE
The PROFILE constant.
|
static java.lang.String |
QUESTION_MARK
The QUESTION_MARK constant.
|
static java.lang.String |
REL
The REL constant.
|
static java.lang.String |
REL_SELF
The REL_SELF constant.
|
static java.lang.String |
RIGHT_CURLY
The RIGHT_CURLY constant.
|
static java.lang.String |
RIGHT_PAREN
The RIGHT_PAREN constant.
|
protected static LinkUtils |
sDefaultLinkUtils |
static java.lang.String |
SELF_KEY
The SELF_KEY constant.
|
static java.lang.String |
SLASH
The SLASH constant.
|
static java.lang.String |
TEMPLATE_ID
The TEMPLATE_ID constant.
|
static java.lang.String |
TEMPLATED
The TEMPLATED constant.
|
static java.lang.String |
TOTAL_RESULTS
The TOTAL_RESULTS property constant.
|
SERVICE_INFO_KEY
DEFAULT_LOG_TRACE_STATUS
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
Constructor and Description |
---|
LinkUtils() |
Modifier and Type | Method and Description |
---|---|
protected void |
addLimit(java.lang.StringBuilder pBuilder,
int pLimit)
Add limit to a string builder
|
protected void |
addLinkIfNotNull(RepresentationModel pModel,
java.lang.String pHref,
java.lang.String pRel,
java.lang.String pMethod)
Add the link to the model if the href is not null
|
protected void |
addOffset(java.lang.StringBuilder pBuilder,
int pOffset)
Add offset to a string builder.
|
void |
addPaginationLinks(RepresentationModel pModel)
Add pagination links and properties to response model object
|
protected java.lang.String |
addQueryParamsToPath(java.lang.String pPath,
<any> pQueryParams,
boolean pIncludeCurrentQueryParams,
boolean pAllowDuplicateQueryParams)
Adds the given map of query params to the given uri path, optionally merging with the current request query parameters.
|
void |
addResourceLink(java.lang.String pRelation,
java.lang.String pLinkPath,
java.lang.String[] pTemplateVariables,
java.lang.String pHttpMethod,
java.util.Set<Link> pLinks)
Adds a link for the given path and relation name to the given set of links.
|
java.lang.String |
buildAbsolutePath(java.lang.String pPath,
java.lang.Object... pVars)
Ensures a given path is returned as an absolute path and with template variables populated
|
java.lang.String |
buildLink(java.lang.String pPathTemplateString,
java.lang.String pPath,
java.lang.String pId)
Build the embedded link from the supplied path and id.
|
java.lang.String[] |
buildMemberUris(<any> pQueryParams,
java.lang.String... pMemberIds)
Returns string uri(s) for a member(s) of a collection resource.
|
java.lang.String[] |
buildMemberUris(java.lang.String... pMemberIds)
Returns string uri(s) for a member(s) of a collection resource.
|
java.lang.String[] |
buildMemberUris(java.lang.String pResourceId,
<any> pQueryParams,
java.lang.String... pMemberIds)
Returns string uri(s) for a member(s) of a collection resource.
|
java.lang.String[] |
buildMemberUris(java.lang.String pResourceId,
<any> pQueryParams,
java.lang.String pIdPropertyName,
java.util.Collection pMembers)
Returns string uri(s) for member(s) of the given collection.
|
java.lang.String[] |
buildMemberUris(java.lang.String pResourceId,
<any> pQueryParams,
java.lang.String pIdPropertyName,
java.util.Collection pMembers,
java.lang.Integer pStartIndex,
java.lang.Integer pEndIndex)
Returns string uri(s) for a page of member(s) of a collection resource.
|
java.lang.String |
buildResourceUri(java.lang.String pResourceId,
boolean pSingular,
boolean pSubResource,
<any> pQueryParams,
boolean pIncludeCurrentQueryParams,
boolean pAllowDuplicateQueryParams,
java.lang.String... pPathParams)
Returns a string uri of a resource for the given resource id.
|
java.lang.String |
buildResourceUri(java.lang.String pResourceId,
boolean pSingular,
boolean pSubResource,
java.util.Map<java.lang.String,java.lang.String> pQueryParams,
java.lang.String... pPathParams)
Returns a string uri of a resource for the given resource id.
|
protected java.lang.String |
createListingLink(int pOffset,
int pLimit)
Create a link with listing/sorting params.
|
java.lang.String |
expandRelationDefinition(java.lang.String pRel)
Expand curie definition in the relation name to support self documentation
|
java.lang.String |
firstUriFromContext(ListingResponse pListingResponse)
Get the first uri for the current context.
|
protected java.lang.String |
fixSlashes(java.lang.String pPath)
Returns the given path string uri ensuring it starts with a slash and doesn't end with one.
|
java.lang.String |
formatUriPath(java.lang.String pUriPath)
Utility method to get the string path to render for a given uri path.
|
java.lang.String |
formatUriPath(java.net.URI pUri)
Utility method to get the string path to render for a given uri.
|
RestContext |
getCurrentContext()
Utility method to get the current rest context.
|
<any> |
getCurrentQueryParameters()
Utility method to get the current request query parameters.
|
UriInfo |
getCurrentUriInfo()
Utility method to get the current uri info.
|
java.lang.String |
getEndpointTemplateUriPath(RestResourceRegistry.EndpointSettings pEndpoint,
java.lang.String pUri)
Utility method to go through the possible template uris for an endpoint and find one that is associated with the
given uri.
|
static LinkUtils |
getLinkUtils()
Static utility method to get a nucleus instance of this component.
|
RestResourceRegistry |
getRegistry()
Utility method to get the current rest context resource registry.
|
java.net.URI |
getRelativeUri(java.net.URI pUri)
Gets the uri that is relative to the rest resource registry context.
|
Link[] |
getResourceLinks(RestResourceRegistry.EndpointSettings pEndpoint,
boolean pSingularLinks,
java.lang.String[] pTemplateVariables)
Retrieves the links associated with the resource
|
protected java.lang.String[] |
getTemplateVariables(java.lang.String pTemplatedUri,
java.lang.String pUri)
Method to get the variable parts of a uri.
|
protected UriInfo |
getUriInfo()
Gets the UriInfo for the current request
|
java.lang.String |
getUriPath(java.lang.String pResourceId)
Get the URI path.
|
java.lang.String |
getUriPrefix(UriInfo pContext)
Get the uri prefix portion from a context.
|
boolean |
isTemplatedPath(java.lang.String pPath)
Utility method to determine if the given path string has any templated parts, e.g.
|
java.lang.String |
joinPaths(java.lang.String... pPaths)
Joins multiple uri path strings together into a single uri path string.
|
java.lang.String |
lastUriFromContext(ListingResponse pListingResponse)
Get the last uri for the current context.
|
protected java.lang.String |
matchTemplatePath(java.lang.String pTemplatePath,
java.lang.String pPath,
boolean pPartialMatch)
Matches a given path against a given path template.
|
java.lang.String |
nextUriFromContext(ListingResponse pListingResponse)
Get the 'next' uri for the current context.
|
java.lang.String |
prevUriFromContext(ListingResponse pListingResponse)
Get the prev uri for the current context.
|
java.lang.String |
removeContextRoot(java.lang.String pUri)
Utility method to return a uri path after the current context root.
|
Link |
selfLinkFromContext()
Creates a self link to be used in the response resource representation.
|
Link |
selfLinkFromContext(java.lang.Boolean pSingular,
java.lang.String... pPathParams)
Creates a self link for the current resource response in context.
|
addLogListener, createAdminServlet, doStartService, doStopService, getAbsoluteName, getAdminServlet, getAdminServletOutputStreamEncoding, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isAdminServletUseServletOutputStream, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, reResolveThis, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setAdminServletOutputStreamEncoding, setAdminServletUseServletOutputStream, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService, toString
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogDebugTrace, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
public static java.lang.String CLASS_VERSION
public static final java.lang.String DEFAULT_LINK_UTILS_PATH
public static final java.lang.String TOTAL_RESULTS
public static final java.lang.String LIMIT
public static final java.lang.String COUNT
public static final java.lang.String HAS_MORE
public static final java.lang.String OFFSET
public static final java.lang.String NEXT_TOTAL
public static final java.lang.String NEXT_OFFSET
public static final java.lang.String NEXT_LIMIT
public static final java.lang.String REL_SELF
public static final java.lang.String HREF
public static final java.lang.String TEMPLATED
public static final java.lang.String REL
public static final java.lang.String PROFILE
public static final java.lang.String MEDIA_TYPE
public static final java.lang.String METHOD
public static final java.lang.String LINKS
public static final java.lang.String NAME
public static final java.lang.String CURIES
public static final java.lang.String ARRAY_KEY
public static final java.lang.String AUTO_WRAP_KEY
public static final java.lang.String CANONICAL_KEY
public static final java.lang.String SELF_KEY
public static final java.lang.String NEXT_KEY
public static final java.lang.String PREV_KEY
public static final java.lang.String HREF_KEY
public static final java.lang.String FIRST_KEY
public static final java.lang.String LAST_KEY
public static final java.lang.String FIELDS_PARAM_KEY
public static final java.lang.String COLON
public static final java.lang.String SLASH
public static final java.lang.String EQUALS
public static final java.lang.String AMPERSAND
public static final java.lang.String QUESTION_MARK
public static final java.lang.String COMMA
public static final java.lang.String DOT
public static final java.lang.String DOT_ESC
public static final java.lang.String LEFT_PAREN
public static final java.lang.String RIGHT_PAREN
public static final java.lang.String LEFT_PAREN_ESC
public static final java.lang.String EMPTY_STRING
public static final java.lang.String LEFT_CURLY
public static final java.lang.String RIGHT_CURLY
public static final java.lang.String TEMPLATE_ID
public static final java.lang.String COLLECTION_REL
protected static LinkUtils sDefaultLinkUtils
public static LinkUtils getLinkUtils()
public Link selfLinkFromContext()
public Link selfLinkFromContext(java.lang.Boolean pSingular, java.lang.String... pPathParams)
pSingular
- true if the response is singular, false for a collection and null to use the endpoint type.pPathParams
- additional path params to include the singular resource id if needed.protected UriInfo getUriInfo()
public void addPaginationLinks(RepresentationModel pModel) throws java.io.UnsupportedEncodingException, RestException
pModel
- the response objectjava.io.UnsupportedEncodingException
- if there was an error encoding a query param or RestException for other errorsRestException
public java.lang.String nextUriFromContext(ListingResponse pListingResponse) throws RestException
pListingResponse
- the listing response to useRestException
public java.lang.String prevUriFromContext(ListingResponse pListingResponse) throws RestException
pListingResponse
- listing response to useRestException
public java.lang.String firstUriFromContext(ListingResponse pListingResponse) throws RestException
pListingResponse
- listing response to useRestException
public java.lang.String lastUriFromContext(ListingResponse pListingResponse) throws RestException
pListingResponse
- listing response to useRestException
protected void addLinkIfNotNull(RepresentationModel pModel, java.lang.String pHref, java.lang.String pRel, java.lang.String pMethod)
pModel
- the response representation modelpHref
- the href for the uripRel
- the link relation namepublic java.lang.String getUriPrefix(UriInfo pContext)
pContext
- contains the request UriInfoprotected java.lang.String createListingLink(int pOffset, int pLimit) throws RestException
pOffset
- offsetpLimit
- limitRestException
protected void addOffset(java.lang.StringBuilder pBuilder, int pOffset)
pBuilder
- the string builder.pOffset
- the offset value.protected void addLimit(java.lang.StringBuilder pBuilder, int pLimit)
pBuilder
- the string builder.pLimit
- the limit value.public java.net.URI getRelativeUri(java.net.URI pUri) throws RestException
pUri
- uri to relativize.RestException
- if uri cannot be relativizedpublic Link[] getResourceLinks(RestResourceRegistry.EndpointSettings pEndpoint, boolean pSingularLinks, java.lang.String[] pTemplateVariables)
pEndpoint
- endpoint settings generating links for a responsepSingularLinks
- true to return links for the singular endpoints or false for the collection linkspTemplateVariables
- values for any path parameters in the links to be generated.public java.lang.String getEndpointTemplateUriPath(RestResourceRegistry.EndpointSettings pEndpoint, java.lang.String pUri)
pEndpoint
- Endpoint to match againstpUri
- Uri to match againstpublic java.lang.String removeContextRoot(java.lang.String pUri)
pUri
- uri path to trimpublic void addResourceLink(java.lang.String pRelation, java.lang.String pLinkPath, java.lang.String[] pTemplateVariables, java.lang.String pHttpMethod, java.util.Set<Link> pLinks)
pRelation
- relation name to use for the linkpLinkPath
- path for the link (may be templated)pTemplateVariables
- if the path is templated then these are the value to usepHttpMethod
- http method for the link referencepLinks
- set of links to populatepublic boolean isTemplatedPath(java.lang.String pPath)
pPath
- String path that may or may not have template variable parts.protected java.lang.String[] getTemplateVariables(java.lang.String pTemplatedUri, java.lang.String pUri)
pTemplatedUri
- The templated uri String with curly braced variable namespUri
- The actual uri to use to get the variable valuespublic java.lang.String expandRelationDefinition(java.lang.String pRel)
pRel
- the relation namepublic java.lang.String buildLink(java.lang.String pPathTemplateString, java.lang.String pPath, java.lang.String pId)
pPathTemplateString
- the path template representing the path with its placeholderspPath
- the path on which to build the resourcepId
- the id of the resourcepublic java.lang.String buildResourceUri(java.lang.String pResourceId, boolean pSingular, boolean pSubResource, java.util.Map<java.lang.String,java.lang.String> pQueryParams, java.lang.String... pPathParams) throws RestException
pResourceId
- resource id for the collection resourcepSingular
- true if the singular of the given resource should be usedpSubResource
- true if uri being built is sub resource of current contextpQueryParams
- Optional map of query param key/value pairs to be added to any built uris, null if nonepPathParams
- array of path param values needed to populate the uri or null if none.RestException
- should an error occur in processing.public java.lang.String buildResourceUri(java.lang.String pResourceId, boolean pSingular, boolean pSubResource, <any> pQueryParams, boolean pIncludeCurrentQueryParams, boolean pAllowDuplicateQueryParams, java.lang.String... pPathParams) throws RestException
pResourceId
- resource id for the collection resourcepSingular
- true if the singular of the given resource should be usedpSubResource
- true if uri being built is sub resource of current contextpQueryParams
- Optional map of query param key/value pairs to be added to any built uris, null if nonepIncludeCurrentQueryParams
- boolean, true to also include query parameters in the current request on the build uri.pAllowDuplicateQueryParams
- boolean, false to prevent a merge of query params values with the same key.pPathParams
- array of path param values needed to populate the uri or null if none.RestException
- should an error occur in processing.protected java.lang.String addQueryParamsToPath(java.lang.String pPath, <any> pQueryParams, boolean pIncludeCurrentQueryParams, boolean pAllowDuplicateQueryParams)
pPath
- String uri pathpQueryParams
- query parameters to addpIncludeCurrentQueryParams
- boolean, true to also add current request query parameters to the pathpAllowDuplicateQueryParams
- boolean, false to prevent a merge of entries with the same keyprotected java.lang.String fixSlashes(java.lang.String pPath)
pPath
- the path to fixprotected java.lang.String matchTemplatePath(java.lang.String pTemplatePath, java.lang.String pPath, boolean pPartialMatch)
pTemplatePath
- template path pattern to matchpPath
- path to matchpPartialMatch
- if true then the minimum number of segments are matched.public java.lang.String[] buildMemberUris(java.lang.String... pMemberIds) throws RestException
pMemberIds
- One or more string member instance idsRestException
- should an error occur in processing.public java.lang.String[] buildMemberUris(<any> pQueryParams, java.lang.String... pMemberIds) throws RestException
pQueryParams
- Optional map of query param key/value pairs to be added to any built uris, null if nonepMemberIds
- One or more string member instance idsRestException
- should an error occur in processing.public java.lang.String[] buildMemberUris(java.lang.String pResourceId, <any> pQueryParams, java.lang.String... pMemberIds) throws RestException
pResourceId
- resource id for the singular resource to build members frompQueryParams
- Optional map of query param key/value pairs to be added
to any built uris, null if nonepMemberIds
- One or more string member instance idsRestException
public java.lang.String[] buildMemberUris(java.lang.String pResourceId, <any> pQueryParams, java.lang.String pIdPropertyName, java.util.Collection pMembers, java.lang.Integer pStartIndex, java.lang.Integer pEndIndex) throws RestException
The collection passed will support a set/list of Repository items; Beans or Strings. A dynamic bean retrieval will be performed for pIdPropertyName on the collection item. If the property is not found or is not a string then toString will be used instead to identify the member id for the collection's singular resource. This will work for a collection of strings; a collection of beans as lomg as the bean contains the pIdPropertyName property passed in; or a collection of Repository items when idPropertyName is set to 'id'.
If passed resource id is non-null the member uri(s) are resolved based on the uri of the endpoint with given resource id, otherwise the member uri(s) are resolved based on the current context uri.
Optionally a map of query params can also be passed in which will then be appended to any built uris.
pResourceId
- resource id for the singular resource to build members uri frompQueryParams
- Optional map of query param key/value pairs to be added
to any built uris, null if nonepIdPropertyName
- identifies the collection item's property to use as
the member's idpMembers
- a collection (list/set) of resource memberspStartIndex
- start index for page of memberspEndIndex
- end index for page of membersRestException
public java.lang.String[] buildMemberUris(java.lang.String pResourceId, <any> pQueryParams, java.lang.String pIdPropertyName, java.util.Collection pMembers) throws RestException
The collection passed will support a set/list of Repository items; Beans or Strings. A dynamic bean retrieval will be performed for pIdPropertyName on the collection item. If the property is not found or is not a string then toString will be used instead to identify the member id for the collection's singular resource. This will work for a collection of strings; a collection of beans as lomg as the bean contains the pIdPropertyName property passed in; or a collection of Repository items when idPropertyName is set to 'id'.
If passed resource id is non-null the member uri(s) are resolved based on the uri of the endpoint with given resource id, otherwise the member uri(s) are resolved based on the current context uri.
Optionally a map of query params can also be passed in which will then be appended to any built uris.
pResourceId
- resource id for the singular resource to build members frompQueryParams
- Optional map of query param key/value pairs to be added
to any built uris, null if nonepIdPropertyName
- identifies the collection item's property to use as
the member's idpMembers
- a collection (list/set) of resource membersRestException
public java.lang.String getUriPath(java.lang.String pResourceId)
pResourceId
- resource to generate the path forpublic java.lang.String buildAbsolutePath(java.lang.String pPath, java.lang.Object... pVars)
pPath
- path to resolve as absolutepVars
- template values to usepublic java.lang.String formatUriPath(java.lang.String pUriPath)
pUriPath
- URI path to formatpublic java.lang.String formatUriPath(java.net.URI pUri)
pUri
- URI to formatpublic RestResourceRegistry getRegistry()
public RestContext getCurrentContext()
public UriInfo getCurrentUriInfo() throws RestException
RestException
- if the current rest context, request context or uri info is nullpublic <any> getCurrentQueryParameters() throws RestException
RestException
- if the current rest context, request context or uri info is nullpublic java.lang.String joinPaths(java.lang.String... pPaths)
pPaths
- array of string paths to join