SpanCustomizingApplicationEventListener implements ApplicationEventListener, RequestEventListener { @Nullable static String route(ContainerRequest request) { ExtendedUriInfo uriInfo = request.getUriInfo(); List<UriTemplate> templates = uriInfo.getMatchedTemplates(); int templateCount = templates.size(); if (templateCount == 0) return ""; StringBuilder builder = null; String basePath = uriInfo.getBaseUri().getPath(); String result = null; if (!"/" .equals(basePath)) { result = basePath; } for (int i = templateCount - 1; i >= 0; i--) { String template = templates.get(i).getTemplate(); 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() : ""; } @Inject  SpanCustomizingApplicationEventListener(EventParser parser); static SpanCustomizingApplicationEventListener create(); static SpanCustomizingApplicationEventListener create(EventParser parser); @Override void onEvent(ApplicationEvent event); @Override RequestEventListener onRequest(RequestEvent requestEvent); @Override void onEvent(RequestEvent event);  }
@Test public void ignoresEventsExceptFinish() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/"), new PathTemplate("/items/{itemId}") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/items/{itemId}"); }
@Test public void route() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/"), new PathTemplate("/items/{itemId}") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/items/{itemId}"); }
@Test public void route_noPath() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/eggs") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/eggs"); }
@Test public void route_invalid() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/"), new PathTemplate("/") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEmpty(); }
@Test public void route_basePath() { setBaseUri("/base"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/"), new PathTemplate("/items/{itemId}") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/base/items/{itemId}"); }
@Test public void route_nested() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/"), new PathTemplate("/items/{itemId}"), new PathTemplate("/"), new PathTemplate("/nested") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/nested/items/{itemId}"); }
@Test public void route_nested_reverse() { setBaseUri("/"); when(uriInfo.getMatchedTemplates()).thenReturn(Arrays.asList( new PathTemplate("/items/{itemId}"), new PathTemplate("/"), new PathTemplate("/nested"), new PathTemplate("/") )); assertThat(SpanCustomizingApplicationEventListener.route(request)) .isEqualTo("/nested/items/{itemId}"); }