Beispiel #1
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 Response getResponse(
     HttpRequest request, HttpResponse response, ResourceInvoker invoker) {
   Response jaxrsResponse = null;
   try {
     jaxrsResponse = invoker.invoke(request, response);
     if (request.getAsyncContext().isSuspended()) {
       /**
        * Callback by the initial calling thread. This callback will probably do nothing in an
        * asynchronous environment but will be used to simulate AsynchronousResponse in vanilla
        * Servlet containers that do not support asychronous HTTP.
        */
       request.getAsyncContext().getAsyncResponse().initialRequestThreadFinished();
       jaxrsResponse = null; // we're handing response asynchronously
     }
   } catch (Exception e) {
     handleInvokerException(request, response, e);
   }
   return jaxrsResponse;
 }
  public void pushContextObjects(HttpRequest request, HttpResponse response) {
    Map contextDataMap = ResteasyProviderFactory.getContextDataMap();
    contextDataMap.put(HttpRequest.class, request);
    contextDataMap.put(HttpResponse.class, response);
    contextDataMap.put(HttpHeaders.class, request.getHttpHeaders());
    contextDataMap.put(UriInfo.class, request.getUri());
    contextDataMap.put(Request.class, new RequestImpl(request));
    contextDataMap.put(ResteasyAsynchronousContext.class, request.getAsyncContext());

    contextDataMap.putAll(defaultContextObjects);
  }
  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;
  }