下面列出了javax.ws.rs.core.UriBuilder#path() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static URI buildBaseUri(final HttpServletRequest request, final javax.ws.rs.core.UriInfo uriInfo, final List<PathSegment> precedingPathSegments) throws ODataException {
try {
UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
for (final PathSegment ps : precedingPathSegments) {
uriBuilder = uriBuilder.path(ps.getPath());
for (final String key : ps.getMatrixParameters().keySet()) {
final Object[] v = ps.getMatrixParameters().get(key).toArray();
uriBuilder = uriBuilder.matrixParam(key, v);
}
}
/*
* workaround because of host name is cached by uriInfo
*/
uriBuilder.host(request.getServerName());
String uriString = uriBuilder.build().toString();
if (!uriString.endsWith("/")) {
uriString = uriString + "/";
}
return new URI(uriString);
} catch (final URISyntaxException e) {
throw new ODataException(e);
}
}
@POST
public Response createNewOrder(JsonObject order, @Context UriInfo uriInfo) {
Integer newOrderId = this.orderService.createNewOrder(new Order(order));
UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
uriBuilder.path(Integer.toString(newOrderId));
return Response.created(uriBuilder.build()).build();
}
/**
* <p>Parses given method and constructs a {@link UriTemplate} containing
* all the information found in the annotations {@link javax.ws.rs.Path},
* {@link javax.ws.rs.QueryParam} and {@link javax.ws.rs.MatrixParam}.</p>
*/
UriTemplate parseMethod(Method method, String basePath) {
UriBuilder uriBuilder = UriBuilder.fromPath(basePath);
Path controllerPath = AnnotationUtils.getAnnotation(method.getDeclaringClass(), Path.class);
if (controllerPath != null) {
uriBuilder.path(controllerPath.value());
}
Path methodPath = AnnotationUtils.getAnnotation(method, Path.class);
if (methodPath != null) {
uriBuilder.path(methodPath.value());
}
UriTemplate.Builder uriTemplateBuilder = UriTemplate.fromTemplate(uriBuilder.toTemplate());
// Populate a List with all properties of given target and all parameters of given method
// except for BeanParams where we need all properties of annotated type.
List<AnnotatedElement> annotatedElements = BeanUtils.getFieldsAndAccessors(method.getDeclaringClass());
Arrays.asList(method.getParameters()).forEach(param -> {
if (param.isAnnotationPresent(BeanParam.class)) {
annotatedElements.addAll(BeanUtils.getFieldsAndAccessors(param.getType()));
} else {
annotatedElements.add(param);
}
});
annotatedElements.forEach(accessibleObject -> {
if (accessibleObject.isAnnotationPresent(QueryParam.class)) {
uriTemplateBuilder.queryParam(accessibleObject.getAnnotation(QueryParam.class).value());
}
if (accessibleObject.isAnnotationPresent(MatrixParam.class)) {
uriTemplateBuilder.matrixParam(accessibleObject.getAnnotation(MatrixParam.class).value());
}
});
return uriTemplateBuilder.build();
}
@POST
public Response createSubscription(@FormParam("autoAck") @DefaultValue("true") boolean autoAck,
@FormParam("selector") String selector,
@Context UriInfo uriInfo) {
ActiveMQRestLogger.LOGGER.debug("Handling POST request for \"" + uriInfo.getPath() + "\"");
try {
QueueConsumer consumer = null;
int attributes = 0;
if (selector != null) {
attributes = attributes | SELECTOR_SET;
}
if (autoAck) {
consumer = createConsumer(selector);
} else {
attributes |= ACKNOWLEDGED;
consumer = createAcknowledgedConsumer(selector);
}
String attributesSegment = "attributes-" + attributes;
UriBuilder location = uriInfo.getAbsolutePathBuilder();
location.path(attributesSegment);
location.path(consumer.getId());
Response.ResponseBuilder builder = Response.created(location.build());
if (autoAck) {
QueueConsumer.setConsumeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1");
} else {
AcknowledgedQueueConsumer.setAcknowledgeNextLink(serviceManager.getLinkStrategy(), builder, uriInfo, uriInfo.getMatchedURIs().get(0) + "/" + attributesSegment + "/" + consumer.getId(), "-1");
}
return builder.build();
} catch (ActiveMQException e) {
throw new RuntimeException(e);
} finally {
}
}
/**
* Returns a URI based on the supplied URI with the paths appended to the base URI.
*
* @param uriInfo
* URI of current actions
* @param paths
* Paths that need to be appended
* @return
*/
public static URI getURI(UriInfo uriInfo, String... paths) {
UriBuilder builder = uriInfo.getBaseUriBuilder();
for (String path : paths) {
if (path != null) {
builder.path(path);
}
}
return builder.build();
}
private URI getCollectionIDFromURIInfo(UriInfo uriInfo, int contextId) {
// remove parameters from url
String urlPath = uriInfo.getPath();
UriBuilder builder = UriBuilder.fromPath(urlPath.substring(0,urlPath.indexOf("/")));
builder.path(Integer.toString(contextId));
return builder.build();
}
private static URI findURI(MethodFinder method, Object... params) {
Method m = method.method();
UriInfo uriInfo = ResteasyProviderFactory.getContextData(UriInfo.class);
UriBuilder builder = uriInfo.getBaseUriBuilder().path(m.getDeclaringClass());
if(m.isAnnotationPresent(Path.class))
builder.path(m);
return builder.build(params);
}
/**
* Create a new http-200 response
*
* @return the http-200 (ok) response
*/
public Response ok() {
final UriBuilder builder = this.location.getAbsolutePathBuilder();
if (this.entity != null) {
builder.path(this.entity);
}
return Response.ok(builder.build()).build();
}
protected String createSenderWithIdLink(UriInfo info) {
UriBuilder builder = info.getRequestUriBuilder();
builder.path("create");
String uri = builder.build().toString();
uri += "/{id}";
return uri;
}
@Override
public Response addTrustedIDP(UriInfo ui, TrustedIdp trustedIDP) {
LOG.info("add Trusted IDP config");
TrustedIdp createdTrustedIdp = trustedIdpDAO.addTrustedIDP(trustedIDP);
UriBuilder uriBuilder = UriBuilder.fromUri(ui.getRequestUri());
uriBuilder.path("{index}");
URI location = uriBuilder.build(createdTrustedIdp.getRealm());
return Response.created(location).entity(trustedIDP).build();
}
public static URI encode(final URI uri) {
final List<PathSegment> pathSegments = UriComponent.decodePath(uri, false);
final UriBuilder uriBuilder = RuntimeDelegate.getInstance().createUriBuilder();
// Build base URL
uriBuilder.scheme(uri.getScheme()).host(uri.getHost()).port(uri.getPort());
// Iterate over path segments and encode it if necessary
for (final PathSegment ps : pathSegments) {
uriBuilder.path(UriComponent.encode(ps.toString(), UriComponent.Type.PATH_SEGMENT));
}
logger.debug("URL before encoding: {}", uri.toString());
URI result = uriBuilder.build();
logger.debug("URL after encoding: {}", result.toString());
return result;
}
/**
* Return the health check URL for the service
*
* @param applicationScheme Scheme the server is listening on
* @return health check URL
*/
protected String getHealthCheckUrl(String applicationScheme) {
final UriBuilder builder = UriBuilder.fromPath(environment.getAdminContext().getContextPath());
builder.path("healthcheck");
builder.scheme(applicationScheme);
if (serviceAddress.get() == null) {
builder.host("127.0.0.1");
} else {
builder.host(serviceAddress.get());
}
builder.port(serviceAdminPort.get());
return builder.build().toString();
}
@Override
public Response addClaim(UriInfo ui, Claim claim) {
LOG.info("add Claim config");
Claim createdClaim = claimDAO.addClaim(claim);
UriBuilder uriBuilder = UriBuilder.fromUri(ui.getRequestUri());
uriBuilder.path("{index}");
URI location = uriBuilder.build(createdClaim.getClaimType().toString());
return Response.created(location).entity(claim).build();
}
@POST
@Consumes("application/xml")
public Response createCustomer(Customer customer, @Context UriInfo uriInfo)
{
customer.setId(idCounter.incrementAndGet());
customerDB.put(customer.getId(), customer);
System.out.println("Created customer " + customer.getId());
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
builder.path(Integer.toString(customer.getId()));
return Response.created(builder.build()).build();
}
protected String createSenderWithIdLink(UriInfo info) {
UriBuilder builder = info.getRequestUriBuilder();
builder.path("create");
String uri = builder.build().toString();
uri += "/{id}";
return uri;
}
public URI build(Resource resource, OntClass typeClass)
{
if (resource == null) throw new IllegalArgumentException("Resource cannot be null");
if (typeClass == null) throw new IllegalArgumentException("OntClass cannot be null");
// skolemization template builds with absolute path builder (e.g. "{slug}")
String path = getStringValue(typeClass, LDT.path);
if (path == null)
throw new IllegalStateException("Cannot skolemize resource of class " + typeClass + " which does not have ldt:path annotation");
final UriBuilder builder;
// treat paths starting with / as absolute, others as relative (to the current absolute path)
// JAX-RS URI templates do not have this distinction (leading slash is irrelevant)
if (path.startsWith("/"))
builder = getBaseUriBuilder().clone();
else
{
Resource parent = getParent(typeClass);
if (parent != null) builder = UriBuilder.fromUri(parent.getURI());
else builder = getAbsolutePathBuilder().clone();
}
Map<String, String> nameValueMap = getNameValueMap(resource, new UriTemplateParser(path));
builder.path(path);
// add fragment identifier
String fragment = getStringValue(typeClass, LDT.fragment);
return builder.fragment(fragment).buildFromMap(nameValueMap); // TO-DO: wrap into SkolemizationException
}
protected String createSenderLink(UriInfo info) {
UriBuilder builder = info.getRequestUriBuilder();
builder.path("create");
String uri = builder.build().toString();
return uri;
}
public static UriBuilder authUrl(UriBuilder baseUriBuilder) {
UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
return uriBuilder.path(OIDCLoginProtocolService.class, "auth");
}
public static UriBuilder authUrl(final UriBuilder baseUriBuilder) {
final UriBuilder uriBuilder = authProtocolBaseUrl(baseUriBuilder);
return uriBuilder.path(DockerV2LoginProtocolService.class, "auth");
}
/**
* Create a new http-201 response
*
* @return the http-201 (created) response
*/
public Response created() {
final UriBuilder builder = this.location.getAbsolutePathBuilder();
if (this.entity == null) {
throw new WebserviceException("The response entity can't be null");
}
builder.path(this.entity);
return Response.created(builder.build()).entity(this.entity).build();
}