Search in sources :

Example 6 with UriTemplate

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

the class SpanCustomizingApplicationEventListener method route.

/**
 * This returns the matched template as defined by a base URL and path expressions.
 *
 * <p>Matched templates are pairs of (resource path, method path) added with
 * {@link org.glassfish.jersey.server.internal.routing.RoutingContext#pushTemplates(UriTemplate,
 * UriTemplate)}. This code skips redundant slashes from either source caused by Path("/") or
 * Path("").
 */
@Nullable
static String route(ContainerRequest request) {
    ExtendedUriInfo uriInfo = request.getUriInfo();
    List<UriTemplate> templates = uriInfo.getMatchedTemplates();
    int templateCount = templates.size();
    if (templateCount == 0)
        return "";
    // don't allocate unless you need it!
    StringBuilder builder = null;
    String basePath = uriInfo.getBaseUri().getPath();
    String result = null;
    if (!"/".equals(basePath)) {
        // skip empty base paths
        result = basePath;
    }
    for (int i = templateCount - 1; i >= 0; i--) {
        String template = templates.get(i).getTemplate();
        // skip allocation
        if ("/".equals(template))
            continue;
        if (builder != null) {
            builder.append(template);
        } else if (result != null) {
            builder = new StringBuilder(result).append(template);
            result = null;
        } else {
            result = template;
        }
    }
    return result != null ? result : builder != null ? builder.toString() : "";
}
Also used : UriTemplate(org.glassfish.jersey.uri.UriTemplate) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo) Nullable(brave.internal.Nullable)

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