Search in sources :

Example 6 with Providers

use of jakarta.ws.rs.ext.Providers 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 7 with Providers

use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.

the class XopWithMultipartRelatedReader method readFrom.

public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    String boundary = mediaType.getParameters().get("boundary");
    if (boundary == null)
        throw new IOException(Messages.MESSAGES.unableToGetBoundary());
    MultipartRelatedInputImpl input = new MultipartRelatedInputImpl(mediaType, workers);
    Providers providers = ResteasyContext.getContextData(Providers.class);
    input.setProviders(providers);
    input.parse(entityStream);
    XopWithMultipartRelatedJAXBProvider xopWithMultipartRelatedJAXBProvider = new XopWithMultipartRelatedJAXBProvider(workers);
    return xopWithMultipartRelatedJAXBProvider.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream, input);
}
Also used : IOException(java.io.IOException) Providers(jakarta.ws.rs.ext.Providers)

Example 8 with Providers

use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.

the class PriorityTest method testContextResolverPriorityOverride.

@Test
public void testContextResolverPriorityOverride() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        webTarget.register(new ClientRequestFilter() {

            @Context
            Providers providers;

            @Override
            public void filter(ClientRequestContext requestContext) throws IOException {
                providers.getContextResolver(String.class, MediaType.WILDCARD_TYPE).getContext(getClass());
            }
        });
        StringBuilder result = new StringBuilder();
        webTarget.register(new ContextResolver<String>() {

            @Override
            public String getContext(Class<?> type) {
                result.append("O");
                return null;
            }
        }, 0);
        webTarget.register(new ContextResolver<String>() {

            @Override
            public String getContext(Class<?> type) {
                result.append("K");
                return null;
            }
        }, 1);
        webTarget.request().get().close();
        Assert.assertEquals("OK", result.toString());
    } finally {
        client.close();
    }
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) Context(jakarta.ws.rs.core.Context) ReaderInterceptorContext(jakarta.ws.rs.ext.ReaderInterceptorContext) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Client(jakarta.ws.rs.client.Client) Providers(jakarta.ws.rs.ext.Providers) Test(org.junit.Test)

Example 9 with Providers

use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.

the class EnvelopedInputImpl method extractEntity.

@SuppressWarnings("unchecked")
public static Object extractEntity(Class t, Type gt, Annotation[] ann, MimeBodyPart decrypted, Providers providers) {
    MultivaluedMap<String, String> mimeHeaders = new Headers<String>();
    Enumeration e = null;
    try {
        e = decrypted.getAllHeaders();
    } catch (MessagingException e1) {
        throw new RuntimeException(e1);
    }
    while (e.hasMoreElements()) {
        Header header = (Header) e.nextElement();
        mimeHeaders.add(header.getName(), header.getValue());
    }
    String contentType = "text/plain";
    if (mimeHeaders.containsKey("Content-Type"))
        contentType = mimeHeaders.getFirst("Content-Type");
    MediaType mediaType = MediaType.valueOf(contentType);
    MessageBodyReader reader = providers.getMessageBodyReader(t, gt, ann, mediaType);
    if (reader == null) {
        throw new RuntimeException(Messages.MESSAGES.couldNotFindMessageBodyReader(t.getClass().getName()));
    }
    Providers old = ResteasyContext.getContextData(Providers.class);
    ResteasyContext.pushContext(Providers.class, providers);
    try {
        InputStream inputStream = null;
        if (EnvelopedInput.class.isAssignableFrom(t)) {
            inputStream = decrypted.getRawInputStream();
        } else {
            inputStream = decrypted.getInputStream();
        }
        return reader.readFrom(t, gt, ann, mediaType, mimeHeaders, inputStream);
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    } finally {
        ResteasyContext.popContextData(Providers.class);
        if (old != null)
            ResteasyContext.pushContext(Providers.class, old);
    }
}
Also used : MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) Enumeration(java.util.Enumeration) Header(jakarta.mail.Header) MessagingException(jakarta.mail.MessagingException) Headers(org.jboss.resteasy.core.Headers) InputStream(java.io.InputStream) MediaType(jakarta.ws.rs.core.MediaType) Providers(jakarta.ws.rs.ext.Providers) MessagingException(jakarta.mail.MessagingException)

Example 10 with Providers

use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.

the class EnvelopedReader method readFrom.

public EnvelopedInput readFrom(Class<EnvelopedInput> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream entityStream) throws IOException, WebApplicationException {
    Class<?> baseType = null;
    Type baseGenericType = null;
    if (genericType != null && genericType instanceof ParameterizedType) {
        ParameterizedType param = (ParameterizedType) genericType;
        baseGenericType = param.getActualTypeArguments()[0];
        baseType = Types.getRawType(baseGenericType);
    }
    EnvelopedInputImpl input = new EnvelopedInputImpl();
    input.setType(baseType);
    input.setGenericType(baseGenericType);
    StringBuilder headerString = new StringBuilder();
    if (headers.containsKey("Content-Disposition")) {
        headerString.append("Content-Disposition: ").append(headers.getFirst("Content-Disposition")).append("\r\n");
    }
    if (headers.containsKey("Content-Type")) {
        headerString.append("Content-Type: ").append(headers.getFirst("Content-Type")).append("\r\n");
    }
    if (headers.containsKey("Content-Transfer-Encoding")) {
        headerString.append("Content-Transfer-Encoding: ").append(headers.getFirst("Content-Transfer-Encoding")).append("\r\n");
    }
    headerString.append("\r\n");
    ByteArrayInputStream is = new ByteArrayInputStream(headerString.toString().getBytes(StandardCharsets.UTF_8));
    MimeBodyPart body = null;
    try {
        body = new MimeBodyPart(new SequenceInputStream(is, entityStream));
    } catch (MessagingException e) {
        throw new ReaderException(e);
    }
    Providers providers = ResteasyContext.getContextData(Providers.class);
    input.setProviders(providers);
    input.setAnnotations(annotations);
    input.setBody(body);
    return input;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MessagingException(jakarta.mail.MessagingException) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) Providers(jakarta.ws.rs.ext.Providers) ReaderException(org.jboss.resteasy.spi.ReaderException)

Aggregations

Providers (jakarta.ws.rs.ext.Providers)12 IOException (java.io.IOException)6 MediaType (jakarta.ws.rs.core.MediaType)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 MessagingException (jakarta.mail.MessagingException)3 Client (jakarta.ws.rs.client.Client)3 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)3 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)3 WebTarget (jakarta.ws.rs.client.WebTarget)3 Context (jakarta.ws.rs.core.Context)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Test (org.junit.Test)3 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)2 ReaderInterceptorContext (jakarta.ws.rs.ext.ReaderInterceptorContext)2 WriterInterceptorContext (jakarta.ws.rs.ext.WriterInterceptorContext)2 InputStream (java.io.InputStream)2 ReaderException (org.jboss.resteasy.spi.ReaderException)2 Header (jakarta.mail.Header)1 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)1