コード例 #1
0
  protected ServerResponse 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 RuntimeException(e);
      }
      ServerResponse serverResponse = (ServerResponse) requestContext.getResponseAbortedWith();
      if (serverResponse != null) {
        return prepareResponse(serverResponse);
      }
    }

    Object rtn = null;
    try {
      rtn = methodInjector.invoke(request, response, target);
    } catch (WebApplicationException wae) {
      prepareResponse(ServerResponse.convertToServerResponse(wae.getResponse()));
      throw wae;
    }

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

    if (request.isSuspended()) {
      AbstractAsynchronousResponse asyncResponse =
          (AbstractAsynchronousResponse) request.getAsynchronousResponse();
      if (asyncResponse == null) return null;
      asyncResponse.setAnnotations(method.getAnnotations());
      asyncResponse.setWriterInterceptors(writerInterceptors);
      asyncResponse.setResponseFilters(responseFilters);
      return null;
    }
    if (rtn == null || method.getReturnType().equals(void.class)) {
      return prepareResponse((ServerResponse) Response.noContent().build());
    }
    if (Response.class.isAssignableFrom(method.getReturnType()) || rtn instanceof Response) {
      return prepareResponse(ServerResponse.convertToServerResponse((Response) rtn));
    }

    Response.ResponseBuilder builder = Response.ok(rtn);
    builder.type(resolveContentType(request, rtn));
    ServerResponse jaxrsResponse = (ServerResponse) builder.build();
    jaxrsResponse.setGenericType(genericReturnType);
    return prepareResponse(jaxrsResponse);
  }
コード例 #2
0
ファイル: ResourceMethod.java プロジェクト: EOC/Resteasy
  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;
  }
コード例 #3
0
 /**
  * Call pre-process ContainerRequestFilters
  *
  * @param in
  * @return
  */
 public Response preprocess(HttpRequest in) {
   for (HttpRequestPreprocessor preprocessor : this.requestPreprocessors) {
     preprocessor.preProcess(in);
   }
   ContainerRequestFilter[] requestFilters =
       providerFactory.getContainerRequestFilterRegistry().preMatch();
   PreMatchContainerRequestContext requestContext = new PreMatchContainerRequestContext(in);
   for (ContainerRequestFilter filter : requestFilters) {
     try {
       filter.filter(requestContext);
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
     Response response = requestContext.getResponseAbortedWith();
     if (response != null) return response;
   }
   return null;
 }
コード例 #4
0
  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;
  }