Search in sources :

Example 1 with UriTemplate

use of org.glassfish.jersey.uri.UriTemplate in project jersey by jersey.

the class UriRoutingContext method getPathParameterBounds.

private int[] getPathParameterBounds(final String name) {
    final Iterator<UriTemplate> templatesIterator = templates.iterator();
    final Iterator<MatchResult> matchResultsIterator = matchResults.iterator();
    while (templatesIterator.hasNext()) {
        MatchResult mr = matchResultsIterator.next();
        // Find the index of path parameter
        final int pIndex = getLastPathParameterIndex(name, templatesIterator.next());
        if (pIndex != -1) {
            int pathLength = mr.group().length();
            int segmentIndex = mr.end(pIndex + 1);
            final int groupLength = segmentIndex - mr.start(pIndex + 1);
            // capturing group in the request path
            while (matchResultsIterator.hasNext()) {
                mr = matchResultsIterator.next();
                segmentIndex += mr.group().length() - pathLength;
                pathLength = mr.group().length();
            }
            return new int[] { segmentIndex - groupLength, segmentIndex };
        }
    }
    return null;
}
Also used : UriTemplate(org.glassfish.jersey.uri.UriTemplate) MatchResult(java.util.regex.MatchResult) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint)

Example 2 with UriTemplate

use of org.glassfish.jersey.uri.UriTemplate in project jersey by jersey.

the class RuntimeModelBuilder method createResourceMethodRouters.

private List<MethodRouting> createResourceMethodRouters(final RuntimeResource runtimeResource, final boolean subResourceMode) {
    final List<MethodRouting> methodRoutings = new ArrayList<>();
    int i = 0;
    for (final Resource resource : runtimeResource.getResources()) {
        final Resource parentResource = runtimeResource.getParent() == null ? null : runtimeResource.getParentResources().get(i++);
        final UriTemplate template = resource.getPathPattern().getTemplate();
        final PushMatchedTemplateRouter templateRouter = parentResource == null ? getTemplateRouter(subResourceMode, template, null) : getTemplateRouter(subResourceMode, parentResource.getPathPattern().getTemplate(), template);
        for (final ResourceMethod resourceMethod : resource.getResourceMethods()) {
            methodRoutings.add(new MethodRouting(resourceMethod, templateRouter, new PushMatchedMethodRouter(resourceMethod), createMethodRouter(resourceMethod)));
        }
    }
    return methodRoutings.isEmpty() ? Collections.<MethodRouting>emptyList() : methodRoutings;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) UriTemplate(org.glassfish.jersey.uri.UriTemplate) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 3 with UriTemplate

use of org.glassfish.jersey.uri.UriTemplate in project jaeger-client-java by jaegertracing.

the class JerseyServerFilter method getPathOperationName.

/**
 * Retrieves an operation name that contains the http method and value of the {@link Path} used by
 * a Jersey server implementation.
 *
 * @return the operation name
 */
private String getPathOperationName(ContainerRequestContext containerRequestContext) {
    String operationName = "";
    try {
        if (normalizedTemplate != null && containerRequestContext instanceof ContainerRequest) {
            ContainerRequest containerRequest = (ContainerRequest) containerRequestContext;
            ExtendedUriInfo uriInfo = containerRequest.getUriInfo();
            if (uriInfo instanceof UriRoutingContext) {
                UriRoutingContext uriRoutingContext = (UriRoutingContext) uriInfo;
                List<UriTemplate> templates = uriRoutingContext.getMatchedTemplates();
                String path = "";
                for (UriTemplate uriTemplate : templates) {
                    String template = (String) normalizedTemplate.get(uriTemplate);
                    path = template + path;
                }
                operationName = String.format("%s:%s", containerRequest.getMethod(), path);
            }
        }
    } catch (Exception e) {
        log.error("Unable to get operation name", e);
    }
    return operationName;
}
Also used : UriRoutingContext(org.glassfish.jersey.server.internal.routing.UriRoutingContext) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) UriTemplate(org.glassfish.jersey.uri.UriTemplate) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Example 4 with UriTemplate

use of org.glassfish.jersey.uri.UriTemplate in project micrometer by micrometer-metrics.

the class DefaultJerseyTagsProviderTest method event.

private static RequestEvent event(Integer status, Exception exception, String baseUri, String... uriTemplateStrings) {
    Builder builder = new RequestEventImpl.Builder();
    ContainerRequest containerRequest = mock(ContainerRequest.class);
    when(containerRequest.getMethod()).thenReturn("GET");
    builder.setContainerRequest(containerRequest);
    ContainerResponse containerResponse = mock(ContainerResponse.class);
    when(containerResponse.getStatus()).thenReturn(status);
    builder.setContainerResponse(containerResponse);
    builder.setException(exception, null);
    ExtendedUriInfo extendedUriInfo = mock(ExtendedUriInfo.class);
    when(extendedUriInfo.getBaseUri()).thenReturn(URI.create("http://localhost:8080" + (baseUri == null ? "/" : baseUri)));
    List<UriTemplate> uriTemplates = uriTemplateStrings == null ? Collections.emptyList() : Arrays.stream(uriTemplateStrings).map(uri -> new UriTemplate(uri)).collect(Collectors.toList());
    // UriTemplate are returned in reverse order
    Collections.reverse(uriTemplates);
    when(extendedUriInfo.getMatchedTemplates()).thenReturn(uriTemplates);
    builder.setExtendedUriInfo(extendedUriInfo);
    return builder.build(Type.FINISHED);
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Builder(org.glassfish.jersey.server.internal.monitoring.RequestEventImpl.Builder) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) UriTemplate(org.glassfish.jersey.uri.UriTemplate) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Example 5 with UriTemplate

use of org.glassfish.jersey.uri.UriTemplate in project micrometer by micrometer-metrics.

the class DefaultJerseyTagsProvider method templatedUri.

private static String templatedUri(RequestEvent event) {
    final ExtendedUriInfo uriInfo = event.getUriInfo();
    final List<UriTemplate> matchedTemplates = new ArrayList<>(uriInfo.getMatchedTemplates());
    if (matchedTemplates.size() > 1) {
        Collections.reverse(matchedTemplates);
    }
    final StringBuilder sb = new StringBuilder();
    sb.append(uriInfo.getBaseUri().getPath());
    for (UriTemplate uriTemplate : matchedTemplates) {
        sb.append(uriTemplate.getTemplate());
    }
    return sb.toString().replaceAll("//+", "/");
}
Also used : ArrayList(java.util.ArrayList) UriTemplate(org.glassfish.jersey.uri.UriTemplate) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Aggregations

UriTemplate (org.glassfish.jersey.uri.UriTemplate)6 ExtendedUriInfo (org.glassfish.jersey.server.ExtendedUriInfo)4 ArrayList (java.util.ArrayList)2 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)2 Endpoint (org.glassfish.jersey.server.internal.process.Endpoint)2 Nullable (brave.internal.Nullable)1 MatchResult (java.util.regex.MatchResult)1 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)1 Builder (org.glassfish.jersey.server.internal.monitoring.RequestEventImpl.Builder)1 UriRoutingContext (org.glassfish.jersey.server.internal.routing.UriRoutingContext)1 Resource (org.glassfish.jersey.server.model.Resource)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)1