use of javax.ws.rs.core.UriBuilder in project che by eclipse.
the class Service method generateLinkForMethod.
private Link generateLinkForMethod(UriInfo uriInfo, String linkRel, Method method, Object... pathParameters) {
String httpMethod = null;
final HttpMethod httpMethodAnnotation = getMetaAnnotation(method, HttpMethod.class);
if (httpMethodAnnotation != null) {
httpMethod = httpMethodAnnotation.value();
}
if (httpMethod == null) {
throw new IllegalArgumentException(format("Method '%s' has not any HTTP method annotation and may not be used to produce link.", method.getName()));
}
final Consumes consumes = getAnnotation(method, Consumes.class);
final Produces produces = getAnnotation(method, Produces.class);
final UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
final LinkedList<String> matchedURIs = new LinkedList<>(uriInfo.getMatchedURIs());
// Get path to the root resource.
if (uriInfo.getMatchedResources().size() < matchedURIs.size()) {
matchedURIs.remove();
}
while (!matchedURIs.isEmpty()) {
baseUriBuilder.path(matchedURIs.pollLast());
}
final Path path = method.getAnnotation(Path.class);
if (path != null) {
baseUriBuilder.path(path.value());
}
final Link link = DtoFactory.getInstance().createDto(Link.class).withRel(linkRel).withHref(baseUriBuilder.build(pathParameters).toString()).withMethod(httpMethod);
if (consumes != null) {
link.setConsumes(consumes.value()[0]);
}
if (produces != null) {
link.setProduces(produces.value()[0]);
}
Class<?>[] parameterClasses = method.getParameterTypes();
if (parameterClasses.length > 0) {
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < parameterClasses.length; i++) {
if (annotations[i].length > 0) {
boolean isBodyParameter = false;
QueryParam queryParam = null;
Description description = null;
Required required = null;
Valid valid = null;
DefaultValue defaultValue = null;
for (int j = 0; j < annotations[i].length; j++) {
Annotation annotation = annotations[i][j];
isBodyParameter |= !JAX_RS_ANNOTATIONS.contains(annotation.annotationType().getName());
Class<?> annotationType = annotation.annotationType();
if (annotationType == QueryParam.class) {
queryParam = (QueryParam) annotation;
} else if (annotationType == Description.class) {
description = (Description) annotation;
} else if (annotationType == Required.class) {
required = (Required) annotation;
} else if (annotationType == Valid.class) {
valid = (Valid) annotation;
} else if (annotationType == DefaultValue.class) {
defaultValue = (DefaultValue) annotation;
}
}
if (queryParam != null) {
LinkParameter parameter = DtoFactory.getInstance().createDto(LinkParameter.class).withName(queryParam.value()).withRequired(required != null).withType(getParameterType(parameterClasses[i]));
if (defaultValue != null) {
parameter.setDefaultValue(defaultValue.value());
}
if (description != null) {
parameter.setDescription(description.value());
}
if (valid != null) {
parameter.setValid(Arrays.asList(valid.value()));
}
link.getParameters().add(parameter);
} else if (isBodyParameter) {
if (description != null) {
link.setRequestBody(DtoFactory.getInstance().createDto(RequestBodyDescriptor.class).withDescription(description.value()));
}
}
}
}
}
return link;
}
use of javax.ws.rs.core.UriBuilder in project che by eclipse.
the class PagingUtil method createLinkHeader.
/**
* Generates link header value from the page object and base uri.
* <a href="https://tools.ietf.org/html/rfc5988">The Link header spec</a>
*
* @param page
* the page used to generate link
* @param uri
* the uri which is used for adding {@code skipCount} & {@code maxItems} query parameters
* @return 'Link' header value
* @throws NullPointerException
* when either {@code page} or {@code uri} is null
*/
public static String createLinkHeader(Page<?> page, URI uri) {
requireNonNull(page, "Required non-null page");
requireNonNull(uri, "Required non-null uri");
final ArrayList<Pair<String, Page.PageRef>> pageRefs = new ArrayList<>(4);
pageRefs.add(Pair.of("first", page.getFirstPageRef()));
pageRefs.add(Pair.of("last", page.getLastPageRef()));
if (page.hasPreviousPage()) {
pageRefs.add(Pair.of("prev", page.getPreviousPageRef()));
}
if (page.hasNextPage()) {
pageRefs.add(Pair.of("next", page.getNextPageRef()));
}
final UriBuilder ub = UriBuilder.fromUri(uri);
return pageRefs.stream().map(refPair -> format("<%s>; rel=\"%s\"", ub.clone().replaceQueryParam("skipCount", refPair.second.getItemsBefore()).replaceQueryParam("maxItems", refPair.second.getPageSize()).build().toString(), refPair.first)).collect(joining(LINK_HEADER_SEPARATOR));
}
use of javax.ws.rs.core.UriBuilder in project che by eclipse.
the class SshMachineInstance method getRuntime.
public MachineRuntimeInfoImpl getRuntime() {
// lazy initialization
if (machineRuntime == null) {
synchronized (this) {
if (machineRuntime == null) {
UriBuilder uriBuilder = UriBuilder.fromUri("http://" + sshClient.getHost());
final Map<String, ServerImpl> servers = new HashMap<>();
for (ServerConf serverConf : machinesServers) {
servers.put(serverConf.getPort(), serverConfToServer(serverConf, uriBuilder.clone()));
}
machineRuntime = new MachineRuntimeInfoImpl(emptyMap(), emptyMap(), servers);
}
}
// todo get env from client
}
return machineRuntime;
}
use of javax.ws.rs.core.UriBuilder in project killbill by killbill.
the class JaxrsUriBuilder method fromResource.
private UriBuilder fromResource(final Class theClass) {
UriBuilder uriBuilder = classToUriBuilder.get(theClass);
if (uriBuilder == null) {
synchronized (classToUriBuilder) {
uriBuilder = classToUriBuilder.get(theClass);
if (uriBuilder == null) {
uriBuilder = UriBuilder.fromResource(theClass);
classToUriBuilder.put(theClass, uriBuilder);
}
}
}
return uriBuilder.clone();
}
use of javax.ws.rs.core.UriBuilder in project killbill by killbill.
the class JaxrsUriBuilder method fromPath.
private UriBuilder fromPath(final String path, final Class theClass, final String getMethodName) {
final String key = path + theClass.getName() + getMethodName;
UriBuilder uriBuilder = pathClassAndMethodToUriBuilder.get(key);
if (uriBuilder == null) {
synchronized (pathClassAndMethodToUriBuilder) {
uriBuilder = pathClassAndMethodToUriBuilder.get(key);
if (uriBuilder == null) {
uriBuilder = fromPath(path, theClass).path(theClass, getMethodName);
pathClassAndMethodToUriBuilder.put(key, uriBuilder);
}
}
}
return uriBuilder.clone();
}
Aggregations