public boolean doesConsume(MediaType contentType) {
    boolean matches = false;
    if (method.getConsumes().length == 0 || (contentType == null && !expectsBody)) return true;

    if (contentType == null) {
      contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
    }
    for (MediaType type : method.getConsumes()) {
      if (type.isCompatible(contentType)) {
        matches = true;
        break;
      }
    }
    return matches;
  }
  public boolean doesProduce(List<? extends MediaType> accepts) {
    if (accepts == null || accepts.size() == 0) {
      // System.out.println("**** no accepts " +" method: " + method);
      return true;
    }
    if (method.getProduces().length == 0) {
      // System.out.println("**** no produces " +" method: " + method);
      return true;
    }

    for (MediaType accept : accepts) {
      for (MediaType type : method.getProduces()) {
        if (type.isCompatible(accept)) {
          return true;
        }
      }
    }
    return false;
  }
 public BuiltResponse invoke(HttpRequest request, HttpResponse response, Object target) {
   request.setAttribute(ResourceMethodInvoker.class.getName(), this);
   incrementMethodCount(request.getHttpMethod());
   ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
   if (method.getPath() != null) {
     uriInfo.pushMatchedURI(uriInfo.getMatchingPath());
   }
   uriInfo.pushCurrentResource(target);
   BuiltResponse rtn = invokeOnTarget(request, response, target);
   return rtn;
 }
 private JaxrsResourceLocatorDescription resLocatorDescription(
     ResourceClass resClass,
     String contextPath,
     String mapping,
     Collection<String> servletMappings,
     List<Class<?>> resolvedCls) {
   JaxrsResourceLocatorDescription locatorRes = new JaxrsResourceLocatorDescription();
   locatorRes.resourceClass = resClass.getClazz();
   resolvedCls.add(resClass.getClazz());
   for (ResourceMethod resMethod : resClass.getResourceMethods()) {
     JaxrsResourceMethodDescription jaxrsRes = new JaxrsResourceMethodDescription();
     jaxrsRes.consumeTypes = resMethod.getConsumes();
     jaxrsRes.contextPath = contextPath;
     jaxrsRes.httpMethods = resMethod.getHttpMethods();
     jaxrsRes.method = resMethod.getMethod();
     jaxrsRes.produceTypes = resMethod.getProduces();
     jaxrsRes.resourceClass = resClass.getClazz();
     String resPath =
         new StringBuilder(mapping)
             .append("/")
             .append(resMethod.getFullpath())
             .toString()
             .replace("//", "/");
     jaxrsRes.resourcePath = resPath;
     jaxrsRes.servletMappings = servletMappings;
     addMethodParameters(jaxrsRes, resMethod.getMethod());
     locatorRes.methodsDescriptions.add(jaxrsRes);
   }
   for (ResourceLocator resLocator : resClass.getResourceLocators()) {
     Class<?> clz = resLocator.getReturnType();
     if (clz.equals(resClass.getClazz())) {
       break;
     }
     if (resolvedCls.contains(clz)) {
       break;
     } else {
       resolvedCls.add(clz);
     }
     ResourceClass subResClass = ResourceBuilder.locatorFromAnnotations(clz);
     String subMapping =
         new StringBuilder(mapping)
             .append("/")
             .append(resLocator.getFullpath())
             .toString()
             .replace("//", "/");
     JaxrsResourceLocatorDescription inner =
         resLocatorDescription(subResClass, contextPath, subMapping, servletMappings, resolvedCls);
     if (inner.containsMethodResources()) {
       locatorRes.subLocatorDescriptions.add(inner);
     }
   }
   return locatorRes;
 }
  public MediaType resolveContentType(HttpRequest in, Object entity) {
    MediaType chosen = (MediaType) in.getAttribute(SegmentNode.RESTEASY_CHOSEN_ACCEPT);
    if (chosen != null && !chosen.equals(MediaType.WILDCARD_TYPE)) {
      return chosen;
    }

    List<MediaType> accepts = in.getHttpHeaders().getAcceptableMediaTypes();

    if (accepts == null || accepts.size() == 0) {
      if (method.getProduces().length == 0) return MediaType.WILDCARD_TYPE;
      else return method.getProduces()[0];
    }

    if (method.getProduces().length == 0) {
      return resolveContentTypeByAccept(accepts, entity);
    }

    for (MediaType accept : accepts) {
      for (MediaType type : method.getProduces()) {
        if (type.isCompatible(accept)) return type;
      }
    }
    return MediaType.WILDCARD_TYPE;
  }
 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 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());
   }
 }
 public MediaType[] getProduces() {
   return method.getProduces();
 }
 public Set<String> getHttpMethods() {
   return method.getHttpMethods();
 }
  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;
  }
 @Override
 public Method getMethod() {
   return method.getMethod();
 }
 public Annotation[] getMethodAnnotations() {
   return method.getAnnotatedMethod().getAnnotations();
 }
 public Class<?> getResourceClass() {
   return method.getResourceClass().getClazz();
 }
 public Type getGenericReturnType() {
   return method.getGenericReturnType();
 }
 public MediaType[] getConsumes() {
   return method.getConsumes();
 }
  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);
 }