예제 #1
0
  public BuiltResponse invoke(HttpRequest request, HttpResponse response, Object target) {
    request.setAttribute(ResourceMethod.class.getName(), this);
    incrementMethodCount(request.getHttpMethod());
    ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri();
    uriInfo.pushCurrentResource(target);
    try {
      BuiltResponse jaxrsResponse = invokeOnTarget(request, response, target);

      if (jaxrsResponse != null && jaxrsResponse.getEntity() != null) {
        // if the content type isn't set, then set it to be either most desired type from the Accept
        // header
        // or the first media type in the @Produces annotation
        // See RESTEASY-144
        Object type = jaxrsResponse.getMetadata().getFirst(HttpHeaderNames.CONTENT_TYPE);
        if (type == null)
          jaxrsResponse
              .getMetadata()
              .putSingle(
                  HttpHeaderNames.CONTENT_TYPE,
                  resolveContentType(request, jaxrsResponse.getEntity()));
      }
      return jaxrsResponse;

    } finally {
      uriInfo.popCurrentResource();
    }
  }
예제 #2
0
  protected BuiltResponse invokeOnTarget(
      HttpRequest request, HttpResponse response, Object target) {
    if (validator != null) {
      violationsContainer = new ViolationsContainer<Object>(validator.validate(target));
      request.setAttribute(ViolationsContainer.class.getName(), violationsContainer);
      request.setAttribute(GeneralValidator.class.getName(), validator);
    }

    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;
      }
    }

    Object rtn = methodInjector.invoke(request, response, target);

    if (violationsContainer != null && violationsContainer.size() > 0) {
      throw new ResteasyViolationExceptionExtension(violationsContainer);
    }

    if (request.getAsyncContext().isSuspended()) {
      request.getAsyncContext().getAsyncResponse().setAnnotations(method.getAnnotations());
      request.getAsyncContext().getAsyncResponse().setWriterInterceptors(writerInterceptors);
      request.getAsyncContext().getAsyncResponse().setResponseFilters(responseFilters);
      request.getAsyncContext().getAsyncResponse().setMethod(this);
      return null;
    }
    if (rtn == null || method.getReturnType().equals(void.class)) {
      BuiltResponse build = (BuiltResponse) Response.noContent().build();
      build.setAnnotations(method.getAnnotations());
      return build;
    }
    if (Response.class.isAssignableFrom(method.getReturnType()) || rtn instanceof Response) {
      BuiltResponse rtn1 = (BuiltResponse) rtn;
      rtn1.setAnnotations(method.getAnnotations());
      return rtn1;
    }

    Response.ResponseBuilder builder = Response.ok(rtn);
    builder.type(resolveContentType(request, rtn));
    BuiltResponse jaxrsResponse = (BuiltResponse) builder.build();
    jaxrsResponse.setGenericType(genericReturnType);
    jaxrsResponse.setAnnotations(method.getAnnotations());
    return jaxrsResponse;
  }
  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;
  }