/** * Initialize common java resource method dispatcher structures. * * @param resourceMethod invocable resource class Java method. * @param methodHandler method invocation handler. * @param validator input/output parameter validator. */ AbstractJavaResourceMethodDispatcher(final Invocable resourceMethod, final InvocationHandler methodHandler, final ConfiguredValidator validator) { this.method = resourceMethod.getDefinitionMethod(); this.methodHandler = methodHandler; this.resourceMethod = resourceMethod; this.validator = validator; }
/** * Initialize common java resource method dispatcher structures. * * @param resourceMethod invocable resource class Java method. * @param methodHandler method invocation handler. * @param validator input/output parameter validator. */ AbstractJavaResourceMethodDispatcher(final Invocable resourceMethod, final InvocationHandler methodHandler, final ConfiguredValidator validator) { this.method = resourceMethod.getDefinitionMethod(); this.methodHandler = methodHandler; this.resourceMethod = resourceMethod; this.validator = validator; }
private static Optional<UnitOfWork> registerUnitOfWorkAnnotations(ResourceMethod method) { UnitOfWork annotation = method.getInvocable().getDefinitionMethod().getAnnotation(UnitOfWork.class); if (annotation == null) { annotation = method.getInvocable().getHandlingMethod().getAnnotation(UnitOfWork.class); } return Optional.ofNullable(annotation); } }
final Method m = method.getInvocable().getDefinitionMethod();
/** * 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(); }
/** * The term 'definition method' used by the Jersey model means the method that contains JAX-RS/Jersey annotations. */ static 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 com.sun.research.ws.wadl.Method createMethod( org.glassfish.jersey.server.model.Resource r, final ResourceMethod m) { com.sun.research.ws.wadl.Method wadlMethod = new com.sun.research.ws.wadl.Method(); wadlMethod.setName(m.getHttpMethod()); wadlMethod.setId(m.getInvocable().getDefinitionMethod().getName()); if (m.isExtended()) { wadlMethod.getAny().add(WadlApplicationContextImpl.EXTENDED_ELEMENT); } return wadlMethod; }
@Override public InvocationHandler create(Invocable method) { final Class<?> resourceClass = method.getHandler().getHandlerClass(); if (resourceClass == null || !isEjbComponent(resourceClass)) { return null; } final Method handlingMethod = method.getDefinitionMethod(); for (Class iFace : remoteAndLocalIfaces(resourceClass)) { try { final Method iFaceMethod = iFace.getDeclaredMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); if (iFaceMethod != null) { return new InvocationHandler() { @Override public Object invoke(Object target, Method ignored, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { return iFaceMethod.invoke(target, args); } }; } } catch (NoSuchMethodException | SecurityException ex) { logLookupException(handlingMethod, resourceClass, iFace, ex); } } return null; }
@Override public com.sun.research.ws.wadl.Method createMethod( org.glassfish.jersey.server.model.Resource r, final ResourceMethod m) { com.sun.research.ws.wadl.Method wadlMethod = new com.sun.research.ws.wadl.Method(); wadlMethod.setName(m.getHttpMethod()); wadlMethod.setId(m.getInvocable().getDefinitionMethod().getName()); if (m.isExtended()) { wadlMethod.getAny().add(WadlApplicationContextImpl.EXTENDED_ELEMENT); } return wadlMethod; }
@Override public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) { if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final Method handlingMethod = resourceMethod.getHandlingMethod(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); final Method definitionMethod = resourceMethod.getDefinitionMethod(); if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result)); if (result instanceof Response) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity())); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } } }
/** * @param r Jersey resource component. * @param m resource method. * @param p method parameter. * @return the enhanced {@link Param}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createParam(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod, org.glassfish.jersey.server.model.Parameter) */ public Param createParam(final org.glassfish.jersey.server.model.Resource r, final org.glassfish.jersey.server.model.ResourceMethod m, final Parameter p) { final Param result = delegate.createParam(r, m, p); if (result != null) { final ParamDocType paramDoc = resourceDoc.getParamDoc(m.getInvocable().getDefinitionMethod().getDeclaringClass(), m.getInvocable().getDefinitionMethod(), p); if (paramDoc != null && !isEmpty(paramDoc.getCommentText())) { final Doc doc = new Doc(); doc.getContent().add(paramDoc.getCommentText()); result.getDoc().add(doc); } } return result; }
/** * @param r Jersey resource component. * @param m resource method. * @param p method parameter. * @return the enhanced {@link Param}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createParam(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod, org.glassfish.jersey.server.model.Parameter) */ public Param createParam(final org.glassfish.jersey.server.model.Resource r, final org.glassfish.jersey.server.model.ResourceMethod m, final Parameter p) { final Param result = delegate.createParam(r, m, p); if (result != null) { final ParamDocType paramDoc = resourceDoc.getParamDoc(m.getInvocable().getDefinitionMethod().getDeclaringClass(), m.getInvocable().getDefinitionMethod(), p); if (paramDoc != null && !isEmpty(paramDoc.getCommentText())) { final Doc doc = new Doc(); doc.getContent().add(paramDoc.getCommentText()); result.getDoc().add(doc); } } return result; }
@Override public void visitResourceMethod(final ResourceMethod resourceMethod) { if (resourceMethod.isExtended()) { return; } if (resourceMethod.getInvocable() != null) { final Invocable i = resourceMethod.getInvocable(); Method method = i.getDefinitionMethod(); List<ProvideLinkDescriptor> linkDescriptors = new ArrayList<>(); handleMetaAnnotations(resourceMethod, method, linkDescriptors); handleAnnotations(resourceMethod, linkDescriptors, method, null); for (ProvideLinkDescriptor linkDescriptor : linkDescriptors) { for (Class<?> target : linkDescriptor.getProvideLink().value()) { target = handleInheritedTarget(linkDescriptor, target); newMappings.computeIfAbsent(target, aClass -> new ArrayList<>()).add(linkDescriptor); } } } processComponents(resourceMethod); }
/** * @param resource Jersey resource component. * @param resourceMethod resource method. * @return the enhanced {@link com.sun.research.ws.wadl.Method}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createMethod(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod) */ public Method createMethod(final org.glassfish.jersey.server.model.Resource resource, final ResourceMethod resourceMethod) { final Method result = delegate.createMethod(resource, resourceMethod); final java.lang.reflect.Method method = resourceMethod.getInvocable().getDefinitionMethod(); final MethodDocType methodDoc = resourceDoc.getMethodDoc(method.getDeclaringClass(), method); if (methodDoc != null && !isEmpty(methodDoc.getCommentText())) { final Doc doc = new Doc(); doc.getContent().add(methodDoc.getCommentText()); // doc.getOtherAttributes().put( new QName( "xmlns" ), "http://www.w3.org/1999/xhtml" ); result.getDoc().add(doc); } return result; }
/** * @param resource Jersey resource component. * @param resourceMethod resource method. * @return the enhanced {@link com.sun.research.ws.wadl.Method}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createMethod(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod) */ public Method createMethod(final org.glassfish.jersey.server.model.Resource resource, final ResourceMethod resourceMethod) { final Method result = delegate.createMethod(resource, resourceMethod); final java.lang.reflect.Method method = resourceMethod.getInvocable().getDefinitionMethod(); final MethodDocType methodDoc = resourceDoc.getMethodDoc(method.getDeclaringClass(), method); if (methodDoc != null && !isEmpty(methodDoc.getCommentText())) { final Doc doc = new Doc(); doc.getContent().add(methodDoc.getCommentText()); // doc.getOtherAttributes().put( new QName( "xmlns" ), "http://www.w3.org/1999/xhtml" ); result.getDoc().add(doc); } return result; }
/** * @param r Jersey resource component. * @param m resource method. * @param mediaType media type. * @return the enhanced {@link com.sun.research.ws.wadl.Representation}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createRequestRepresentation(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod, javax.ws.rs.core.MediaType) */ public Representation createRequestRepresentation(final org.glassfish.jersey.server.model.Resource r, final org.glassfish.jersey.server.model.ResourceMethod m, final MediaType mediaType) { final Representation result = delegate.createRequestRepresentation(r, m, mediaType); final RepresentationDocType requestRepresentation = resourceDoc.getRequestRepresentation(m.getInvocable() .getDefinitionMethod().getDeclaringClass(), m.getInvocable().getDefinitionMethod(), result.getMediaType() ); if (requestRepresentation != null) { result.setElement(requestRepresentation.getElement()); addDocForExample(result.getDoc(), requestRepresentation.getExample()); } return result; }
@Override public void onValidate(final ValidationInterceptorContext ctx) { final Object resource = ctx.getResource(); final Invocable resourceMethod = ctx.getInvocable(); final Object[] args = ctx.getArgs(); final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); // Resource validation. if (beanDescriptor.isBeanConstrained()) { constraintViolations.addAll(validate(resource)); } if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Method handlingMethod = resourceMethod.getHandlingMethod(); // Resource method validation - input parameters. final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters() && validateOnExecutionHandler.validateMethod(resource.getClass(), resourceMethod.getDefinitionMethod(), resourceMethod.getHandlingMethod())) { constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args)); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } }
/** * @param r Jersey resource component. * @param m resource method. * @param mediaType media type. * @return the enhanced {@link com.sun.research.ws.wadl.Representation}. * @see org.glassfish.jersey.server.wadl.WadlGenerator#createRequestRepresentation(org.glassfish.jersey.server.model.Resource, * org.glassfish.jersey.server.model.ResourceMethod, javax.ws.rs.core.MediaType) */ public Representation createRequestRepresentation(final org.glassfish.jersey.server.model.Resource r, final org.glassfish.jersey.server.model.ResourceMethod m, final MediaType mediaType) { final Representation result = delegate.createRequestRepresentation(r, m, mediaType); final RepresentationDocType requestRepresentation = resourceDoc.getRequestRepresentation(m.getInvocable() .getDefinitionMethod().getDeclaringClass(), m.getInvocable().getDefinitionMethod(), result.getMediaType() ); if (requestRepresentation != null) { result.setElement(requestRepresentation.getElement()); addDocForExample(result.getDoc(), requestRepresentation.getExample()); } return result; }
/** * Create a builder from an existing resource method model. * * @param parent parent resource model builder. * @param originalMethod existing resource method model to create the builder from. */ /* package */ Builder(final Resource.Builder parent, ResourceMethod originalMethod) { this.parent = parent; this.consumedTypes = new LinkedHashSet<>(originalMethod.getConsumedTypes()); this.producedTypes = new LinkedHashSet<>(originalMethod.getProducedTypes()); this.suspended = originalMethod.isSuspendDeclared(); this.suspendTimeout = originalMethod.getSuspendTimeout(); this.suspendTimeoutUnit = originalMethod.getSuspendTimeoutUnit(); this.handlerParameters = new LinkedHashSet<>(originalMethod.getInvocable().getHandler().getParameters()); this.nameBindings = originalMethod.getNameBindings(); this.httpMethod = originalMethod.getHttpMethod(); this.managedAsync = originalMethod.isManagedAsyncDeclared(); Invocable invocable = originalMethod.getInvocable(); this.handlingMethod = invocable.getHandlingMethod(); this.encodedParams = false; this.routingResponseType = invocable.getRoutingResponseType(); this.extended = originalMethod.isExtended(); Method handlerMethod = invocable.getDefinitionMethod(); MethodHandler handler = invocable.getHandler(); if (handler.isClassBased()) { handledBy(handler.getHandlerClass(), handlerMethod); } else { handledBy(handler.getHandlerInstance(), handlerMethod); } }
/** * Create a builder from an existing resource method model. * * @param parent parent resource model builder. * @param originalMethod existing resource method model to create the builder from. */ /* package */ Builder(final Resource.Builder parent, ResourceMethod originalMethod) { this.parent = parent; this.consumedTypes = new LinkedHashSet<>(originalMethod.getConsumedTypes()); this.producedTypes = new LinkedHashSet<>(originalMethod.getProducedTypes()); this.suspended = originalMethod.isSuspendDeclared(); this.suspendTimeout = originalMethod.getSuspendTimeout(); this.suspendTimeoutUnit = originalMethod.getSuspendTimeoutUnit(); this.handlerParameters = new LinkedHashSet<>(originalMethod.getInvocable().getHandler().getParameters()); this.nameBindings = originalMethod.getNameBindings(); this.httpMethod = originalMethod.getHttpMethod(); this.managedAsync = originalMethod.isManagedAsyncDeclared(); Invocable invocable = originalMethod.getInvocable(); this.handlingMethod = invocable.getHandlingMethod(); this.encodedParams = false; this.routingResponseType = invocable.getRoutingResponseType(); this.extended = originalMethod.isExtended(); Method handlerMethod = invocable.getDefinitionMethod(); MethodHandler handler = invocable.getHandler(); if (handler.isClassBased()) { handledBy(handler.getHandlerClass(), handlerMethod); } else { handledBy(handler.getHandlerInstance(), handlerMethod); } }