Search in sources :

Example 1 with ResourceContext

use of jakarta.ws.rs.container.ResourceContext in project tomee by apache.

the class Contexts method bind.

/**
 * Using a set ensures we don't set the thread local twice or more,
 * there may be super classes with injection points of identical types
 *
 * Also allows us to get context references from other sources such as interceptors
 *
 * @param exchange Exchange
 * @param types    Collection
 */
public static void bind(final Exchange exchange, final Collection<Class<?>> types) {
    // used in lazy mode by RESTResourceFinder if cdi beans uses @Context, === initThreadLocal
    EXCHANGE.set(exchange);
    CdiAppContextsService.pushRequestReleasable(CleanUpThreadLocal.INSTANCE);
    for (final Class<?> type : types) {
        if (Request.class.equals(type)) {
            final Request binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Request.class);
            ThreadLocalContextManager.REQUEST.set(binding);
        } else if (UriInfo.class.equals(type)) {
            final UriInfo binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, UriInfo.class);
            ThreadLocalContextManager.URI_INFO.set(binding);
        } else if (HttpHeaders.class.equals(type)) {
            final HttpHeaders binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpHeaders.class);
            ThreadLocalContextManager.HTTP_HEADERS.set(binding);
        } else if (SecurityContext.class.equals(type)) {
            final SecurityContext binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, SecurityContext.class);
            ThreadLocalContextManager.SECURITY_CONTEXT.set(binding);
        } else if (ContextResolver.class.equals(type)) {
            final ContextResolver<?> binding = JAXRSUtils.createContextValue(exchange.getInMessage(), type, ContextResolver.class);
            ThreadLocalContextManager.CONTEXT_RESOLVER.set(binding);
        } else if (Providers.class.equals(type)) {
            final Providers providers = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Providers.class);
            ThreadLocalContextManager.PROVIDERS.set(providers);
        } else if (ServletRequest.class.equals(type)) {
            ServletRequest servletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ServletRequest.class);
            if (servletRequest == null) {
                // probably the case with CXF
                servletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletRequest.class);
            }
            ThreadLocalContextManager.SERVLET_REQUEST.set(servletRequest);
        } else if (HttpServletRequest.class.equals(type)) {
            final HttpServletRequest httpServletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletRequest.class);
            ThreadLocalContextManager.HTTP_SERVLET_REQUEST.set(httpServletRequest);
        } else if (HttpServletResponse.class.equals(type)) {
            final HttpServletResponse httpServletResponse = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletResponse.class);
            ThreadLocalContextManager.HTTP_SERVLET_RESPONSE.set(httpServletResponse);
        } else if (ServletConfig.class.equals(type)) {
            final ServletConfig servletConfig = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ServletConfig.class);
            ThreadLocalContextManager.SERVLET_CONFIG.set(servletConfig);
        } else if (Configuration.class.equals(type)) {
            final Configuration config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Configuration.class);
            ThreadLocalContextManager.CONFIGURATION.set(config);
        } else if (ResourceInfo.class.equals(type)) {
            final ResourceInfo config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ResourceInfo.class);
            ThreadLocalContextManager.RESOURCE_INFO.set(config);
        } else if (ResourceContext.class.equals(type)) {
            final ResourceContext config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ResourceContext.class);
            ThreadLocalContextManager.RESOURCE_CONTEXT.set(config);
        } else if (Application.class.equals(type)) {
            final Application config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Application.class);
            ThreadLocalContextManager.APPLICATION.set(config);
        } else {
            final Message message = exchange.getInMessage();
            final ContextProvider<?> provider = ProviderFactory.getInstance(message).createContextProvider(type, message);
            if (provider != null) {
                final Object value = provider.createContext(message);
                Map<String, Object> map = ThreadLocalContextManager.OTHERS.get();
                if (map == null) {
                    map = new HashMap<>();
                    ThreadLocalContextManager.OTHERS.set(map);
                }
                map.put(type.getName(), value);
            }
        }
    }
}
Also used : HttpHeaders(jakarta.ws.rs.core.HttpHeaders) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) ResourceInfo(jakarta.ws.rs.container.ResourceInfo) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ResourceContext(jakarta.ws.rs.container.ResourceContext) Configuration(jakarta.ws.rs.core.Configuration) Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Request(jakarta.ws.rs.core.Request) ServletRequest(jakarta.servlet.ServletRequest) ServletConfig(jakarta.servlet.ServletConfig) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(jakarta.ws.rs.ext.Providers) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) SecurityContext(jakarta.ws.rs.core.SecurityContext) Application(jakarta.ws.rs.core.Application) HashMap(java.util.HashMap) Map(java.util.Map) UriInfo(jakarta.ws.rs.core.UriInfo)

Example 2 with ResourceContext

use of jakarta.ws.rs.container.ResourceContext in project resteasy by resteasy.

the class SynchronousDispatcher method pushContextObjects.

@SuppressWarnings("unchecked")
public void pushContextObjects(final HttpRequest request, final HttpResponse response) {
    @SuppressWarnings("rawtypes") Map contextDataMap = ResteasyContext.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, response));
    contextDataMap.put(ResteasyAsynchronousContext.class, request.getAsyncContext());
    ResourceContext resourceContext = new ResourceContext() {

        @Override
        public <T> T getResource(Class<T> resourceClass) {
            return providerFactory.injectedInstance(resourceClass, request, response);
        }

        @Override
        public <T> T initResource(T resource) {
            providerFactory.injectProperties(resource, request, response);
            return resource;
        }
    };
    contextDataMap.put(ResourceContext.class, resourceContext);
    contextDataMap.putAll(defaultContextObjects);
    contextDataMap.put(Cleanables.class, new Cleanables());
    contextDataMap.put(PostResourceMethodInvokers.class, new PostResourceMethodInvokers());
}
Also used : ResourceContext(jakarta.ws.rs.container.ResourceContext) Cleanables(org.jboss.resteasy.plugins.server.Cleanables) HashMap(java.util.HashMap) Map(java.util.Map) RequestImpl(org.jboss.resteasy.specimpl.RequestImpl)

Aggregations

ResourceContext (jakarta.ws.rs.container.ResourceContext)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ServletConfig (jakarta.servlet.ServletConfig)1 ServletRequest (jakarta.servlet.ServletRequest)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 ResourceInfo (jakarta.ws.rs.container.ResourceInfo)1 Application (jakarta.ws.rs.core.Application)1 Configuration (jakarta.ws.rs.core.Configuration)1 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)1 Request (jakarta.ws.rs.core.Request)1 SecurityContext (jakarta.ws.rs.core.SecurityContext)1 UriInfo (jakarta.ws.rs.core.UriInfo)1 Providers (jakarta.ws.rs.ext.Providers)1 ContextProvider (org.apache.cxf.jaxrs.ext.ContextProvider)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)1 Message (org.apache.cxf.message.Message)1 Cleanables (org.jboss.resteasy.plugins.server.Cleanables)1