org.jboss.resteasy.spi.Failure Java Examples

The following examples show how to use org.jboss.resteasy.spi.Failure. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuarkusConstructorInjector.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Object construct(HttpRequest request, HttpResponse response, boolean unwrapAsync)
        throws Failure, WebApplicationException, ApplicationException {
    if (QuarkusInjectorFactory.CONTAINER == null) {
        return delegate.construct(request, response, unwrapAsync);
    }
    if (factory == null) {
        factory = QuarkusInjectorFactory.CONTAINER.instanceFactory(this.ctor.getDeclaringClass());
    }
    if (factory == null) {
        return delegate.construct(request, response, unwrapAsync);
    }
    return factory.create().get();
}
 
Example #2
Source File: LoggingInterceptor.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method)
        throws Failure, WebApplicationException {
    if (logger.isDebugEnabled()) {

        String httpMethod = request.getHttpMethod();

        URI uri = ui.getRequestUri();

        String uriPath = uri.getPath();
        if (uri.getQuery() != null) {
            uriPath += "?" + uri.getQuery();
        }
        if (uri.getFragment() != null) {
            uriPath += "#" + uri.getFragment();
        }

        String sessionid = null;
        List<String> headerSessionId = request.getHttpHeaders().getRequestHeader("sessionid");
        if (headerSessionId != null) {
            sessionid = headerSessionId.get(0);
        }
        if (logger.isDebugEnabled()) {
            // log only in debug mode
            logger.debug(sessionid + "|" + httpMethod + "|" + uriPath);
        }
    }
    return null;
}
 
Example #3
Source File: KeycloakErrorHandler.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private int getStatusCode(Throwable throwable) {
    int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
    if (throwable instanceof WebApplicationException) {
        WebApplicationException ex = (WebApplicationException) throwable;
        status = ex.getResponse().getStatus();
    }
    if (throwable instanceof Failure) {
        Failure f = (Failure) throwable;
        status = f.getErrorCode();
    }
    if (throwable instanceof JsonParseException) {
        status = Response.Status.BAD_REQUEST.getStatusCode();
    }
    return status;
}
 
Example #4
Source File: VertxRequestHandler.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void dispatch(RoutingContext routingContext, InputStream is, VertxOutput output) {
    ManagedContext requestContext = beanContainer.requestContext();
    requestContext.activate();
    routingContext.remove(QuarkusHttpUser.AUTH_FAILURE_HANDLER);
    QuarkusHttpUser user = (QuarkusHttpUser) routingContext.user();
    if (association != null) {
        association.setIdentity(QuarkusHttpUser.getSecurityIdentity(routingContext, null));
    }
    currentVertxRequest.setCurrent(routingContext);
    try {
        Context ctx = vertx.getOrCreateContext();
        HttpServerRequest request = routingContext.request();
        ResteasyUriInfo uriInfo = VertxUtil.extractUriInfo(request, rootPath);
        ResteasyHttpHeaders headers = VertxUtil.extractHttpHeaders(request);
        HttpServerResponse response = request.response();
        VertxHttpResponse vertxResponse = new VertxHttpResponse(request, dispatcher.getProviderFactory(),
                request.method(), allocator, output);

        // using a supplier to make the remote Address resolution lazy: often it's not needed and it's not very cheap to create.
        LazyHostSupplier hostSupplier = new LazyHostSupplier(request);

        VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, routingContext, headers, uriInfo, request.rawMethod(),
                hostSupplier,
                dispatcher.getDispatcher(), vertxResponse, requestContext);
        vertxRequest.setInputStream(is);
        try {
            ResteasyContext.pushContext(SecurityContext.class, new QuarkusResteasySecurityContext(request, routingContext));
            ResteasyContext.pushContext(RoutingContext.class, routingContext);
            dispatcher.service(ctx, request, response, vertxRequest, vertxResponse, true);
        } catch (Failure e1) {
            vertxResponse.setStatus(e1.getErrorCode());
            if (e1.isLoggable()) {
                log.error(e1);
            }
        } catch (Throwable ex) {
            routingContext.fail(ex);
        }

        boolean suspended = vertxRequest.getAsyncContext().isSuspended();
        boolean requestContextActive = requestContext.isActive();
        if (!suspended) {
            try {
                if (requestContextActive) {
                    requestContext.terminate();
                }
            } finally {
                try {
                    vertxResponse.finish();
                } catch (IOException e) {
                    log.debug("IOException writing JAX-RS response", e);
                }
            }
        } else {
            //we need the request context to stick around
            requestContext.deactivate();
        }
    } catch (Throwable t) {
        try {
            routingContext.fail(t);
        } finally {
            if (requestContext.isActive()) {
                requestContext.terminate();
            }
        }
    }
}
 
Example #5
Source File: QuarkusInjectorFactory.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public CompletionStage<Void> inject(HttpRequest request, HttpResponse response, Object target, boolean unwrapAsync)
        throws Failure, WebApplicationException, ApplicationException {
    return delegate.inject(request, response, PROXY_UNWRAPPER.apply(target), unwrapAsync);
}
 
Example #6
Source File: QuarkusConstructorInjector.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Object injectableArguments(HttpRequest request, HttpResponse response, boolean unwrapAsync)
        throws Failure {
    return this.delegate.injectableArguments(request, response, unwrapAsync);
}
 
Example #7
Source File: VertxPluginRequestHandler.java    From redpipe with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(HttpServerRequest request)
{
   request.bodyHandler(buff -> {
      Context ctx = vertx.getOrCreateContext();
      ResteasyUriInfo uriInfo = VertxUtil.extractUriInfo(request.getDelegate(), servletMappingPrefix);
      ResteasyHttpHeaders headers = VertxUtil.extractHttpHeaders(request.getDelegate());
      HttpServerResponse response = request.response();
      VertxHttpResponse vertxResponse = new VertxHttpResponseWithWorkaround(response.getDelegate(), dispatcher.getProviderFactory(), request.method());
      VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx.getDelegate(), headers, uriInfo, request.rawMethod(), dispatcher.getDispatcher(), vertxResponse, false);
      if (buff.length() > 0)
      {
         ByteBufInputStream in = new ByteBufInputStream(buff.getDelegate().getByteBuf());
         vertxRequest.setInputStream(in);
      }

      try
      {
     	AppGlobals.set(appGlobals);
     	appGlobals.injectGlobals();
         dispatcher.service(ctx.getDelegate(), request.getDelegate(), response.getDelegate(), vertxRequest, vertxResponse, true);
      } catch (Failure e1)
      {
         vertxResponse.setStatus(e1.getErrorCode());
      } catch (Exception ex)
      {
         vertxResponse.setStatus(500);
         LogMessages.LOGGER.error(Messages.MESSAGES.unexpected(), ex);
      }
      finally 
      {
     	 AppGlobals.set(null);
      }
      if (!vertxRequest.getAsyncContext().isSuspended())
      {
         try
         {
            vertxResponse.finish();
         } catch (IOException e)
         {
            e.printStackTrace();
         }
      }
   });
}
 
Example #8
Source File: AllOtherExceptionsMapper.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(Exception e) {
    int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
    Response response = null;

    if (e instanceof WebApplicationException) {
        response = ((WebApplicationException) e).getResponse();
        logger.debug("An exception occurred when processing REST response", e);
    } else if (e instanceof Failure) { // Resteasy support
        Failure failure = ((Failure) e);
        if (failure.getErrorCode() > 0) {
            status = failure.getErrorCode();
        }
        response = failure.getResponse();
        logger.debug("An exception occurred when processing REST response", e);
    } else {
        logger.error("An exception occurred when processing REST response", e);
    }

    Response.ResponseBuilder builder;

    if (response != null) {
        builder = Response.status(response.getStatus());
        // b.entity(response.getEntity());

        // copy headers
        for (String headerName : response.getMetadata().keySet()) {
            List<Object> headerValues = response.getMetadata().get(headerName);
            for (Object headerValue : headerValues) {
                builder.header(headerName, headerValue);
            }
        }
    } else {
        builder = Response.status(status);
    }

    builder.entity(new ErrorResponseRest(e)).type(MediaType.APPLICATION_JSON);

    return builder.build();

}
 
Example #9
Source File: AllOtherExceptionsMapper.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(Exception e) {
    int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
    Response response = null;

    if (e instanceof WebApplicationException) {
        response = ((WebApplicationException) e).getResponse();
        if (e instanceof NotFoundException) {
            return response; // In case of 404 we want to return the empty body.
        }
        logger.debug("An exception occurred when processing REST response", e);
    } else if (e instanceof Failure) { // Resteasy support
        Failure failure = ((Failure) e);
        if (failure.getErrorCode() > 0) {
            status = failure.getErrorCode();
        }
        response = failure.getResponse();
        logger.debug("An exception occurred when processing REST response", e);
    } else {
        logger.error("An exception occurred when processing REST response", e);
    }

    Response.ResponseBuilder builder;

    if (response != null) {
        builder = Response.status(response.getStatus());

        // copy headers
        for (Map.Entry<String, List<Object>> en : response.getMetadata().entrySet()) {
            String headerName = en.getKey();
            List<Object> headerValues = en.getValue();
            for (Object headerValue : headerValues) {
                builder.header(headerName, headerValue);
            }
        }
    } else {
        builder = Response.status(status);
    }

    builder.entity(new ErrorResponse(e)).type(MediaType.APPLICATION_JSON);

    return builder.build();
}
 
Example #10
Source File: ClientRegistrationAuth.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private Failure unauthorized(String errorDescription) {
    event.detail(Details.REASON, errorDescription).error(Errors.INVALID_TOKEN);
    throw new ErrorResponseException(OAuthErrorException.INVALID_TOKEN, errorDescription, Response.Status.UNAUTHORIZED);
}
 
Example #11
Source File: ClientRegistrationAuth.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private Failure forbidden() {
    return forbidden("Forbidden");
}
 
Example #12
Source File: ClientRegistrationAuth.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private Failure forbidden(String errorDescription) {
    event.error(Errors.NOT_ALLOWED);
    throw new ErrorResponseException(OAuthErrorException.INSUFFICIENT_SCOPE, errorDescription, Response.Status.FORBIDDEN);
}
 
Example #13
Source File: ClientRegistrationAuth.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private Failure notFound() {
    event.error(Errors.CLIENT_NOT_FOUND);
    throw new ErrorResponseException(OAuthErrorException.INVALID_REQUEST, "Client not found", Response.Status.NOT_FOUND);
}