Search in sources :

Example 1 with Dispatcher

use of org.jboss.resteasy.spi.Dispatcher in project resteasy by resteasy.

the class ContainerResponseContextImpl method writeException.

private void writeException(Throwable t) {
    /*
       * Here we cannot call AsyncResponse.resume(t) because that would invoke the response filters
       * and we should not invoke them because we're already in them.
       */
    HttpResponse httpResponse = (HttpResponse) contextDataMap.get(HttpResponse.class);
    SynchronousDispatcher dispatcher = (SynchronousDispatcher) contextDataMap.get(Dispatcher.class);
    ResteasyAsynchronousResponse asyncResponse = request.getAsyncContext().getAsyncResponse();
    RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
    tracingLogger.flush(httpResponse.getOutputHeaders());
    dispatcher.unhandledAsynchronousException(httpResponse, t);
    onComplete.accept(t);
    asyncResponse.complete();
    asyncResponse.completionCallbacks(t);
}
Also used : ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) HttpResponse(org.jboss.resteasy.spi.HttpResponse) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) Dispatcher(org.jboss.resteasy.spi.Dispatcher)

Example 2 with Dispatcher

use of org.jboss.resteasy.spi.Dispatcher in project scm-manager by scm-manager.

the class InvalidFormatExceptionMapperTest method shouldMapInvalidFormatExceptionDueToInvalidEnum.

@Test
void shouldMapInvalidFormatExceptionDueToInvalidEnum() throws URISyntaxException, UnsupportedEncodingException {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
    dispatcher.getRegistry().addSingletonResource(new SimpleResource());
    dispatcher.getProviderFactory().registerProvider(InvalidFormatExceptionMapper.class);
    MockHttpRequest request = MockHttpRequest.post("/").contentType("application/json").content("{\"e\": \"NONE\"}".getBytes());
    MockHttpResponse response = new MockHttpResponse();
    dispatcher.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(400);
    assertThat(response.getContentAsString()).contains("2qRyyaVcJ1");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Dispatcher(org.jboss.resteasy.spi.Dispatcher) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with Dispatcher

use of org.jboss.resteasy.spi.Dispatcher in project resteasy by resteasy.

the class SseEventSinkInterceptor method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    ResourceMethodInvoker rmi = ((PostMatchContainerRequestContext) requestContext).getResourceMethod();
    if (rmi.isAsyncStreamProvider() || rmi.isSse()) {
        Dispatcher dispatcher = ResteasyContext.getContextData(Dispatcher.class);
        ResteasyProviderFactory providerFactory = dispatcher != null ? dispatcher.getProviderFactory() : ResteasyProviderFactory.getInstance();
        SseEventOutputImpl sink = new SseEventOutputImpl(new SseEventProvider(providerFactory), providerFactory);
        ResteasyContext.getContextDataMap().put(SseEventSink.class, sink);
        ResteasyContext.getContextData(PostResourceMethodInvokers.class).addInvokers(new PostResourceMethodInvoker() {

            @Override
            public void invoke() {
                sink.flushResponseToClient();
            }
        });
    }
}
Also used : PostResourceMethodInvoker(org.jboss.resteasy.core.PostResourceMethodInvoker) ResourceMethodInvoker(org.jboss.resteasy.core.ResourceMethodInvoker) PostMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PostMatchContainerRequestContext) PostResourceMethodInvoker(org.jboss.resteasy.core.PostResourceMethodInvoker) Dispatcher(org.jboss.resteasy.spi.Dispatcher) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) PostResourceMethodInvokers(org.jboss.resteasy.core.PostResourceMethodInvokers)

Example 4 with Dispatcher

use of org.jboss.resteasy.spi.Dispatcher in project resteasy by resteasy.

the class ServletContainerDispatcher method init.

@SuppressWarnings(value = "unchecked")
public void init(ServletContext servletContext, ConfigurationBootstrap bootstrap, HttpRequestFactory requestFactory, HttpResponseFactory responseFactory) throws ServletException {
    this.requestFactory = requestFactory;
    this.responseFactory = responseFactory;
    ResteasyDeployment ctxDeployment = (ResteasyDeployment) servletContext.getAttribute(ResteasyDeployment.class.getName());
    ResteasyProviderFactory globalFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName());
    if (globalFactory == null && ctxDeployment != null) {
        globalFactory = ctxDeployment.getProviderFactory();
    }
    Dispatcher globalDispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName());
    if (globalDispatcher == null && ctxDeployment != null) {
        globalDispatcher = ctxDeployment.getDispatcher();
    }
    String application = bootstrap.getInitParameter("jakarta.ws.rs.Application");
    String useGlobalStr = bootstrap.getInitParameter("resteasy.servlet.context.deployment");
    boolean useGlobal = globalFactory != null;
    if (useGlobalStr != null)
        useGlobal = Boolean.parseBoolean(useGlobalStr);
    // in the servlet context
    if (useGlobal) {
        providerFactory = globalFactory;
        dispatcher = globalDispatcher;
        if ((providerFactory != null && dispatcher == null) || (providerFactory == null && dispatcher != null)) {
            throw new ServletException(Messages.MESSAGES.unknownStateListener());
        }
        // We haven't been initialized by an external entity so bootstrap ourselves
        if (providerFactory == null) {
            deployment = bootstrap.createDeployment();
            deployment.start();
            servletContext.setAttribute(ResteasyProviderFactory.class.getName(), deployment.getProviderFactory());
            servletContext.setAttribute(Dispatcher.class.getName(), deployment.getDispatcher());
            servletContext.setAttribute(Registry.class.getName(), deployment.getRegistry());
            dispatcher = deployment.getDispatcher();
            providerFactory = deployment.getProviderFactory();
        } else {
            // ResteasyBootstrap inited us.  Check to see if the servlet defines an Application class
            if (application != null) {
                try {
                    Map contextDataMap = ResteasyContext.getContextDataMap();
                    contextDataMap.putAll(dispatcher.getDefaultContextObjects());
                    Application app = ResteasyDeploymentImpl.createApplication(application.trim(), dispatcher, providerFactory);
                    // push context data so we can inject it
                    processApplication(app);
                    servletMappingPrefix = bootstrap.getParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX);
                    if (servletMappingPrefix == null)
                        servletMappingPrefix = "";
                    servletMappingPrefix = servletMappingPrefix.trim();
                } finally {
                    ResteasyContext.removeContextDataLevel();
                }
            } else {
                servletMappingPrefix = bootstrap.getParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX);
                if (servletMappingPrefix == null)
                    servletMappingPrefix = "";
                servletMappingPrefix = servletMappingPrefix.trim();
            }
        }
    } else {
        servletMappingPrefix = bootstrap.getParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX);
        if (servletMappingPrefix == null)
            servletMappingPrefix = "";
        servletMappingPrefix = servletMappingPrefix.trim();
        deployment = bootstrap.createDeployment();
        deployment.start();
        dispatcher = deployment.getDispatcher();
        providerFactory = deployment.getProviderFactory();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) Registry(org.jboss.resteasy.spi.Registry) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ThreadLocalResteasyProviderFactory(org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) Dispatcher(org.jboss.resteasy.spi.Dispatcher) Map(java.util.Map) Application(jakarta.ws.rs.core.Application)

Example 5 with Dispatcher

use of org.jboss.resteasy.spi.Dispatcher in project resteasy by resteasy.

the class InternalDispatcher method getResponse.

public Response getResponse(MockHttpRequest request, Object entity) {
    try {
        Dispatcher dispatcher = ResteasyContext.getContextData(Dispatcher.class);
        if (dispatcher == null) {
            return null;
        }
        enhanceRequest(request);
        return dispatcher.internalInvocation(request, new MockHttpResponse(), entity);
    } finally {
    }
}
Also used : Dispatcher(org.jboss.resteasy.spi.Dispatcher) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse)

Aggregations

Dispatcher (org.jboss.resteasy.spi.Dispatcher)6 SynchronousDispatcher (org.jboss.resteasy.core.SynchronousDispatcher)3 MockHttpResponse (org.jboss.resteasy.mock.MockHttpResponse)2 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)2 ServletException (jakarta.servlet.ServletException)1 Application (jakarta.ws.rs.core.Application)1 Map (java.util.Map)1 PostResourceMethodInvoker (org.jboss.resteasy.core.PostResourceMethodInvoker)1 PostResourceMethodInvokers (org.jboss.resteasy.core.PostResourceMethodInvokers)1 ResourceMethodInvoker (org.jboss.resteasy.core.ResourceMethodInvoker)1 ThreadLocalResteasyProviderFactory (org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory)1 PostMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PostMatchContainerRequestContext)1 ResteasyProviderFactoryImpl (org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl)1 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)1 HttpResponse (org.jboss.resteasy.spi.HttpResponse)1 Registry (org.jboss.resteasy.spi.Registry)1 ResteasyAsynchronousResponse (org.jboss.resteasy.spi.ResteasyAsynchronousResponse)1 ResteasyDeployment (org.jboss.resteasy.spi.ResteasyDeployment)1 RESTEasyTracingLogger (org.jboss.resteasy.tracing.RESTEasyTracingLogger)1 Test (org.junit.jupiter.api.Test)1