@Override
    public Response apply(final ContainerRequestContext req) {
      // Suspend current request
      final ProcessingContext processingContext = processingContextProvider.get();
      processingContext.suspend();

      Executors.newSingleThreadExecutor()
          .submit(
              new Runnable() {

                @Override
                public void run() {
                  try {
                    Thread.sleep(500);
                  } catch (InterruptedException ex) {
                    ex.printStackTrace(System.err);
                  }

                  // Returning will enter the suspended request
                  processingContext.resume(Response.ok().entity(responseContent).build());
                }
              });

      return null;
    }
Ejemplo n.º 2
0
  @Override
  public ContainerResponse apply(final ContainerRequest requestContext) {
    final Object resource = routingContextFactory.get().peekMatchedResource();

    final ProcessingContext processingCtx = invocationContextFactory.get();
    if (method.isSuspendDeclared()) {
      processingCtx.setSuspendTimeout(method.getSuspendTimeout(), method.getSuspendTimeoutUnit());
    }
    requestContext.setProperty(ReaderInterceptorExecutor.INTERCEPTORS, getReaderInterceptors());
    requestContext.setProperty(WriterInterceptorExecutor.INTERCEPTORS, getWriterInterceptors());
    final Response response = dispatcher.dispatch(resource, requestContext);

    if (method.isSuspendDeclared()) {
      processingCtx.setResponse(resource);
      processingCtx.trySuspend();
    }

    final ContainerResponse responseContext = new ContainerResponse(requestContext, response);
    final Invocable invocable = method.getInvocable();
    responseContext.setEntityAnnotations(invocable.getHandlingMethod().getDeclaredAnnotations());

    if (responseContext.hasEntity()
        && !(responseContext.getEntityType() instanceof ParameterizedType)) {
      Type invocableType = invocable.getResponseType();
      if (invocableType != null
          && Void.TYPE != invocableType
          && Void.class != invocableType
          && invocableType != Response.class) {
        responseContext.setEntityType(invocableType);
      }
    }

    return responseContext;
  }