public void registryUpdated(JaxrsInterceptorRegistry registry) { this.resourceMethodProviderFactory = new ResteasyProviderFactory(parentProviderFactory); for (DynamicFeature feature : parentProviderFactory.getServerDynamicFeatures()) { feature.configure(resourceInfo, new FeatureContextDelegate(resourceMethodProviderFactory)); } if (registry.getIntf().equals(WriterInterceptor.class)) { writerInterceptors = resourceMethodProviderFactory .getServerWriterInterceptorRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); } else if (registry.getIntf().equals(ContainerRequestFilter.class)) { requestFilters = resourceMethodProviderFactory .getContainerRequestFilterRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); } else if (registry.getIntf().equals(ContainerResponseFilter.class)) { responseFilters = resourceMethodProviderFactory .getContainerResponseFilterRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); } }
protected MediaType resolveContentTypeByAccept(List<MediaType> accepts, Object entity) { if (accepts == null || accepts.size() == 0 || entity == null) { return MediaType.WILDCARD_TYPE; } Class clazz = entity.getClass(); Type type = this.method.getGenericReturnType(); if (entity instanceof GenericEntity) { GenericEntity gen = (GenericEntity) entity; clazz = gen.getRawType(); type = gen.getType(); } for (MediaType accept : accepts) { if (resourceMethodProviderFactory.getMessageBodyWriter( clazz, type, method.getAnnotatedMethod().getAnnotations(), accept) != null) { return accept; } } return MediaType.WILDCARD_TYPE; }
public ResourceMethodInvoker( ResourceMethod method, InjectorFactory injector, ResourceFactory resource, ResteasyProviderFactory providerFactory) { this.injector = injector; this.resource = resource; this.parentProviderFactory = providerFactory; this.method = method; resourceInfo = new ResourceInfo() { @Override public Method getResourceMethod() { return ResourceMethodInvoker.this.method.getAnnotatedMethod(); } @Override public Class<?> getResourceClass() { return ResourceMethodInvoker.this.method.getResourceClass().getClazz(); } }; this.resourceMethodProviderFactory = new ResteasyProviderFactory(providerFactory); for (DynamicFeature feature : providerFactory.getServerDynamicFeatures()) { feature.configure(resourceInfo, new FeatureContextDelegate(resourceMethodProviderFactory)); } this.methodInjector = injector.createMethodInjector(method, resourceMethodProviderFactory); // hack for when message contentType == null // and @Consumes is on the class expectsBody = this.methodInjector.expectsBody(); requestFilters = resourceMethodProviderFactory .getContainerRequestFilterRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); responseFilters = resourceMethodProviderFactory .getContainerResponseFilterRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); writerInterceptors = resourceMethodProviderFactory .getServerWriterInterceptorRegistry() .postMatch(method.getResourceClass().getClazz(), method.getAnnotatedMethod()); // we register with parent to lisen for redeploy evens providerFactory.getContainerRequestFilterRegistry().getListeners().add(this); providerFactory.getContainerResponseFilterRegistry().getListeners().add(this); providerFactory.getServerWriterInterceptorRegistry().getListeners().add(this); ContextResolver<GeneralValidator> resolver = providerFactory.getContextResolver(GeneralValidator.class, MediaType.WILDCARD_TYPE); if (resolver != null) { validator = providerFactory .getContextResolver(GeneralValidator.class, MediaType.WILDCARD_TYPE) .getContext(null); } if (validator != null) { isValidatable = validator.isValidatable(getMethod().getDeclaringClass()); methodIsValidatable = validator.isMethodValidatable(getMethod()); } }
public void initializeAsync(ResteasyAsynchronousResponse asyncResponse) { asyncResponse.setAnnotations(method.getAnnotatedMethod().getAnnotations()); asyncResponse.setWriterInterceptors(writerInterceptors); asyncResponse.setResponseFilters(responseFilters); asyncResponse.setMethod(this); }
protected BuiltResponse invokeOnTarget( HttpRequest request, HttpResponse response, Object target) { ResteasyProviderFactory.pushContext( ResourceInfo.class, resourceInfo); // we don't pop so writer interceptors can get at this PostMatchContainerRequestContext requestContext = new PostMatchContainerRequestContext(request, this); for (ContainerRequestFilter filter : requestFilters) { try { filter.filter(requestContext); } catch (IOException e) { throw new ApplicationException(e); } BuiltResponse serverResponse = (BuiltResponse) requestContext.getResponseAbortedWith(); if (serverResponse != null) { return serverResponse; } } if (validator != null) { if (isValidatable) { validator.validate(request, target); } if (methodIsValidatable) { request.setAttribute(GeneralValidator.class.getName(), validator); } else if (isValidatable) { validator.checkViolations(request); } } Object rtn = null; try { rtn = methodInjector.invoke(request, response, target); } catch (RuntimeException ex) { if (request.getAsyncContext().isSuspended()) { try { request.getAsyncContext().getAsyncResponse().resume(ex); } catch (Exception e) { logger.error("Error resuming failed async operation", e); } return null; } else { throw ex; } } if (request.getAsyncContext().isSuspended() || request.wasForwarded()) { return null; } if (rtn == null || method.getReturnType().equals(void.class)) { BuiltResponse build = (BuiltResponse) Response.noContent().build(); build.addMethodAnnotations(method.getAnnotatedMethod()); return build; } if (Response.class.isAssignableFrom(method.getReturnType()) || rtn instanceof Response) { if (!(rtn instanceof BuiltResponse)) { Response r = (Response) rtn; Headers<Object> metadata = new Headers<Object>(); metadata.putAll(r.getMetadata()); rtn = new BuiltResponse(r.getStatus(), metadata, r.getEntity(), null); } BuiltResponse rtn1 = (BuiltResponse) rtn; rtn1.addMethodAnnotations(method.getAnnotatedMethod()); if (rtn1.getGenericType() == null) { if (getMethod().getReturnType().equals(Response.class)) { rtn1.setGenericType(rtn1.getEntityClass()); } else { rtn1.setGenericType(method.getGenericReturnType()); } } return rtn1; } Response.ResponseBuilder builder = Response.ok(rtn); BuiltResponse jaxrsResponse = (BuiltResponse) builder.build(); if (jaxrsResponse.getGenericType() == null) { if (getMethod().getReturnType().equals(Response.class)) { jaxrsResponse.setGenericType(jaxrsResponse.getEntityClass()); } else { jaxrsResponse.setGenericType(method.getGenericReturnType()); } } jaxrsResponse.addMethodAnnotations(method.getAnnotatedMethod()); return jaxrsResponse; }
public Annotation[] getMethodAnnotations() { return method.getAnnotatedMethod().getAnnotations(); }