@Override public void filter(ContainerRequestContext request) throws IOException { // do not filter requests that do not use OAuth authentication String authHeader = request.getHeaderString(OAuth1Parameters.AUTHORIZATION_HEADER); if (authHeader == null || !authHeader.toUpperCase().startsWith(OAuth1Parameters.SCHEME.toUpperCase())) { return; } // do not filter requests that matches to access or token resources final Method handlingMethod = uriInfo.get().getMatchedResourceMethod().getInvocable().getHandlingMethod(); if (handlingMethod.isAnnotationPresent(TokenResource.class) || handlingMethod.getDeclaringClass().isAnnotationPresent(TokenResource.class)) { return; } // do not filter if the request path matches pattern to ignore if (match(ignorePathPattern, request.getUriInfo().getPath())) { return; } OAuth1SecurityContext sc; try { sc = getSecurityContext(request); } catch (OAuth1Exception e) { if (optional) { return; } else { throw e; } } request.setSecurityContext(sc); }
@Override public WebTarget apply(ContainerRequest containerRequest) { // no need for try-catch - unlike for @*Param annotations, any issues with @Uri would usually be caused // by incorrect server code, so the default runtime exception mapping to 500 is appropriate final ExtendedUriInfo uriInfo = containerRequest.getUriInfo(); final Map<String, Object> pathParamValues = uriInfo.getPathParameters().entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, (Function<Map.Entry<String, List<String>>, Object>) stringObjectEntry -> { List<String> input = stringObjectEntry.getValue(); return input.isEmpty() ? null : input.get(0); })); JerseyUriBuilder uriBuilder = new JerseyUriBuilder().uri(this.uri).resolveTemplates(pathParamValues); final ManagedClient managedClient = client.get(); if (!uriBuilder.isAbsolute()) { final String customBaseUri = managedClient.customBaseUri; final String rootUri = customBaseUri.isEmpty() ? uriInfo.getBaseUri().toString() : customBaseUri; uriBuilder = new JerseyUriBuilder().uri(rootUri).path(uriBuilder.toTemplate()); } return managedClient.instance.target(uriBuilder); } }
break; case RESOURCE_METHOD_FINISHED: final ResourceMethod method = event.getUriInfo().getMatchedResourceMethod(); methodStats = new MethodStats(method, methodTimeStart, now - methodTimeStart); break; event.getUriInfo().getMatchedTemplates() .stream() .collect(Collectors.collectingAndThen(Collectors.toList(), uriTemplates -> {
private static List<ResourceMethod> getMatchedMethods(final ExtendedUriInfo uriInfo) { final List<ResourceMethod> matchedResourceLocators = uriInfo.getMatchedResourceLocators(); final List<ResourceMethod> methods = new ArrayList<>(1 + matchedResourceLocators.size()); methods.add(uriInfo.getMatchedResourceMethod()); methods.addAll(matchedResourceLocators); return methods; } }
ExtendedUriInfo uriInfo = request.getUriInfo(); List<UriTemplate> templates = uriInfo.getMatchedTemplates(); int templateCount = templates.size(); if (templateCount == 0) return ""; String basePath = uriInfo.getBaseUri().getPath(); String result = null; if (!"/".equals(basePath)) { // skip empty base paths
@Override public Response apply(ContainerRequestContext containerRequestContext) { final RuntimeResource resource = extendedUriInfo.get().getMatchedRuntimeResources().get(0); final UriInfo uriInfo = containerRequestContext.getUriInfo(); return Response.status(Response.Status.NOT_FOUND).build(); return Response.ok() .type(MediaTypes.WADL_TYPE) .allow(ModelProcessorUtil.getAllowedMethods(resource)) .header("Last-modified", lastModified) .entity(bytes)
@Override public Response apply(ContainerRequestContext containerRequestContext) { final Set<String> allowedMethods = ModelProcessorUtil.getAllowedMethods( (extendedUriInfo.get().getMatchedRuntimeResources().get(0))); return Response.ok() .allow(allowedMethods) .header(HttpHeaders.CONTENT_LENGTH, "0") .type(containerRequestContext.getAcceptableMediaTypes().get(0)) .build(); } }
@Override public Response apply(ContainerRequestContext containerRequestContext) { Set<String> allowedMethods = ModelProcessorUtil.getAllowedMethods(extendedUriInfo.get() .getMatchedRuntimeResources().get(0)); final String allowedList = allowedMethods.toString(); final String optionsBody = allowedList.substring(1, allowedList.length() - 1); return Response.ok(optionsBody, MediaType.TEXT_PLAIN_TYPE) .allow(allowedMethods) .build(); } }
@Override public Response apply(ContainerRequestContext containerRequestContext) { final Set<String> allowedMethods = ModelProcessorUtil.getAllowedMethods( (extendedUriInfo.getMatchedRuntimeResources().get(0))); return Response.ok() .allow(allowedMethods) .header(HttpHeaders.CONTENT_LENGTH, "0") .type(containerRequestContext.getAcceptableMediaTypes().get(0)) .build(); } }
/** * Obtains a model object for a viewable. * * @param extendedUriInfo uri info to obtain last matched resource from. * @return a model object. */ private Object getModel(final ExtendedUriInfo extendedUriInfo) { final List<Object> matchedResources = extendedUriInfo.getMatchedResources(); if (resourceInstance != null) { return setModelClass(resourceInstance); } else if (matchedResources.size() > 1) { return setModelClass(matchedResources.get(1)); } else { return setModelClass(resourceContextProvider.get().getResource(resourceClass)); } }
private boolean useEnvelope() { try { String envelope = containerRequestProvider.get().getUriInfo().getQueryParameters().getFirst("envelope"); if (envelope!=null && envelope.equalsIgnoreCase("true")) { //i have tested the boolean interpretation from jax-rs or jackson, whoever takes care of it, and //that's exactly how it behaves: "true" ignoring case is true, everything else (including "on", "1" //and so on) is false. //i'm not judging, i just do it exactly the same way, so that exceptions are consistently returned //in line with success results. return true; } } catch (Exception e) { //never mind. } return false; }
final String pathTemplate = requestContext.getUriInfo().getPathParameters().getFirst(IMPLICIT_VIEW_PATH_PARAMETER); if (pathTemplate != null) { templateNames.add(pathTemplate); final ExtendedUriInfo uriInfo = extendedUriInfoProvider.get(); final List<RuntimeResource> matchedRuntimeResources = uriInfo.getMatchedRuntimeResources(); if (matchedRuntimeResources.size() > 1) {
@Override public Response apply(ContainerRequestContext containerRequestContext) { Set<String> allowedMethods = ModelProcessorUtil.getAllowedMethods(extendedUriInfo .getMatchedRuntimeResources().get(0)); final String allowedList = allowedMethods.toString(); final String optionsBody = allowedList.substring(1, allowedList.length() - 1); return Response.ok(optionsBody, MediaType.TEXT_PLAIN_TYPE) .allow(allowedMethods) .build(); } }
/** * Get an {@link ErrorTemplate} annotation from resource method / class the throwable was raised from. * * @return an error template annotation or {@code null} if the method is not annotated. */ private ErrorTemplate getErrorTemplate() { final ExtendedUriInfo uriInfo = uriInfoProvider.get(); final ResourceMethod matchedResourceMethod = uriInfo.getMatchedResourceMethod(); if (matchedResourceMethod != null) { final Invocable invocable = matchedResourceMethod.getInvocable(); ErrorTemplate errorTemplate = invocable.getHandlingMethod().getAnnotation(ErrorTemplate.class); if (errorTemplate == null) { Class<?> handlerClass = invocable.getHandler().getHandlerClass(); if (invocable.isInflector() && TemplateInflector.class .isAssignableFrom(invocable.getHandler().getHandlerClass())) { handlerClass = ((TemplateInflector) invocable.getHandler().getInstance(null)).getModelClass(); } errorTemplate = handlerClass.getAnnotation(ErrorTemplate.class); } return errorTemplate; } return null; }
/** * The term 'definition method' used by the Jersey model means the method that contains JAX-RS/Jersey annotations. */ private Method getDefinitionMethod(ContainerRequestContext requestContext) { if (!(requestContext.getUriInfo() instanceof ExtendedUriInfo)) { throw new IllegalStateException("Could not get Extended Uri Info. Incompatible version of Jersey?"); } ExtendedUriInfo uriInfo = (ExtendedUriInfo) requestContext.getUriInfo(); ResourceMethod matchedResourceMethod = uriInfo.getMatchedResourceMethod(); Invocable invocable = matchedResourceMethod.getInvocable(); return invocable.getDefinitionMethod(); }
@Override public List<PathSegment> apply(ContainerRequest request) { return request.getUriInfo().getPathSegments(name, decode); } }
Resource resource = event.getUriInfo().getMatchedResourceMethod().getParent(); !request.getRequestHeaders().keySet().contains(HttpHeaders.X_FORWARDED_FOR)) { throw new IllegalStateException("Header " + HttpHeaders.X_FORWARDED_FOR + " is required but was not found in the request"); for (Map.Entry<String, List<String>> entry : request.getRequestHeaders().entrySet()) { if (!REDACTED_HEADERS.contains(entry.getKey())) { builder.append(" Header : " + entry.getKey() + " = " + entry.getValue() + "\n"); builder.append(" Method : " + request.getMethod() + "\n"); builder.append(" URI : " + request.getRequestUri() + "\n"); for (Map.Entry<String, List<String>> entry : request.getUriInfo().getQueryParameters(true).entrySet()) { String name = entry.getKey(); List<String> value = entry.getValue();
@Override public Object apply(ContainerRequest containerRequest) { try { return extractor.extract(containerRequest.getUriInfo().getQueryParameters(decode)); } catch (ExtractorException e) { throw new ParamException.QueryParamException(e.getCause(), extractor.getName(), extractor.getDefaultValueString()); } } }
@Override public Credentials provide() { Cookie tokenCookie = getContainerRequest().getCookies().get(tokenKey); } else if (!isAuthorized(result.get(), getContainerRequest().getUriInfo().getMatchedTemplates(), getContainerRequest().getMethod())) { throw new WebApplicationException(Response.Status.FORBIDDEN); } else {
@Override public Object apply(ContainerRequest request) { try { return extractor.extract(request.getUriInfo().getPathParameters(decode)); } catch (ExtractorException e) { throw new PathParamException(e.getCause(), extractor.getName(), extractor.getDefaultValueString()); } } }