Search in sources :

Example 1 with Providers

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

the class ConfigurationInheritanceTest method testContextResolverInheritance.

@Test
public void testContextResolverInheritance() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget parentWebTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        WebTarget childWebTarget = parentWebTarget.path("path");
        List<String> result = new ArrayList<>();
        childWebTarget.register(new ClientRequestFilter() {

            @Context
            Providers providers;

            @Override
            public void filter(ClientRequestContext requestContext) throws IOException {
                ContextResolver<String> contextResolver = providers.getContextResolver(String.class, MediaType.WILDCARD_TYPE);
                if (contextResolver != null) {
                    String context = contextResolver.getContext(getClass());
                    result.add(context);
                }
            }
        });
        // Registration on parent MUST not affect the child
        ContextResolver<String> parentContextResolver = new ContextResolver<String>() {

            @Override
            public String getContext(Class<?> type) {
                return "ParentContext";
            }
        };
        parentWebTarget.register(parentContextResolver);
        childWebTarget.request().get().close();
        Assert.assertTrue(result.isEmpty());
        // Child MUST only use the snapshot configuration of the parent
        // taken at child creation time.
        ContextResolver<String> childContextResolver = new ContextResolver<String>() {

            @Override
            public String getContext(Class<?> type) {
                return null;
            }
        };
        childWebTarget.register(childContextResolver);
        childWebTarget.request().get().close();
        Assert.assertEquals(1, result.size());
        Assert.assertEquals(null, result.get(0));
    } finally {
        client.close();
    }
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) Context(jakarta.ws.rs.core.Context) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Providers(jakarta.ws.rs.ext.Providers) WebTarget(jakarta.ws.rs.client.WebTarget) ContextResolver(jakarta.ws.rs.ext.ContextResolver) Client(jakarta.ws.rs.client.Client) Test(org.junit.Test)

Example 2 with Providers

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

the class MultipartSignedReader method readFrom.

public SignedInput readFrom(Class<SignedInput> 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);
    }
    try {
        ByteArrayDataSource ds = new ByteArrayDataSource(entityStream, mediaType.toString());
        MimeMultipart mm = new MimeMultipart(ds);
        MultipartSignedInputImpl input = new MultipartSignedInputImpl();
        input.setType(baseType);
        input.setGenericType(baseGenericType);
        input.setAnnotations(annotations);
        input.setBody(mm);
        Providers providers = ResteasyContext.getContextData(Providers.class);
        input.setProviders(providers);
        return input;
    } catch (MessagingException e) {
        throw new ReaderException(e);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) MimeMultipart(jakarta.mail.internet.MimeMultipart) MessagingException(jakarta.mail.MessagingException) Providers(jakarta.ws.rs.ext.Providers) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) ReaderException(org.jboss.resteasy.spi.ReaderException)

Example 3 with Providers

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

the class JsonSerialization method toByteArray.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static byte[] toByteArray(Object token, ResteasyProviderFactory factory) throws IOException {
    MessageBodyWriter writer = factory.getMessageBodyWriter(token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE);
    if (writer == null)
        throw new NullPointerException(Messages.MESSAGES.couldNotFindMessageBodyWriterForJSON());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Providers old = ResteasyContext.getContextData(Providers.class);
    ResteasyContext.pushContext(Providers.class, factory);
    try {
        writer.writeTo(token, token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<String, Object>(), baos);
        return baos.toByteArray();
    } finally {
        ResteasyContext.popContextData(Providers.class);
        if (old != null)
            ResteasyContext.pushContext(Providers.class, old);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) Providers(jakarta.ws.rs.ext.Providers)

Example 4 with Providers

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

the class ClientResponse method readFrom.

// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
@Override
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
    Type useGeneric = genericType == null ? type : genericType;
    Class<?> useType = type;
    media = media == null ? MediaType.WILDCARD_TYPE : media;
    annotations = annotations == null ? this.annotations : annotations;
    boolean isMarshalledEntity = false;
    if (type.equals(MarshalledEntity.class)) {
        isMarshalledEntity = true;
        ParameterizedType param = (ParameterizedType) useGeneric;
        useGeneric = param.getActualTypeArguments()[0];
        useType = Types.getRawType(useGeneric);
    }
    Providers current = ResteasyContext.getContextData(Providers.class);
    ResteasyContext.pushContext(Providers.class, configuration);
    Object obj = null;
    try {
        InputStream is = getEntityStream();
        if (is == null) {
            throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
        }
        if (isMarshalledEntity) {
            is = new InputStreamToByteArray(is);
        }
        ReaderInterceptor[] readerInterceptors = configuration.getReaderInterceptors(null, null);
        final Object finalObj;
        final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
        AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, configuration.getProviderFactory(), useType, useGeneric, annotations, media, getStringHeaders(), is, properties, tracingLogger);
        try {
            finalObj = context.proceed();
            obj = finalObj;
        } finally {
            tracingLogger.logDuration("RI_SUMMARY", timestamp, context.getProcessedInterceptorCount());
        }
        if (isMarshalledEntity) {
            InputStreamToByteArray isba = (InputStreamToByteArray) is;
            final byte[] bytes = isba.toByteArray();
            return new MarshalledEntity<Object>() {

                @Override
                public byte[] getMarshalledBytes() {
                    return bytes;
                }

                @Override
                public Object getEntity() {
                    return finalObj;
                }
            };
        } else {
            return finalObj;
        }
    } catch (ProcessingException pe) {
        throw pe;
    } catch (Exception ex) {
        throw new ProcessingException(ex);
    } finally {
        if (!Publisher.class.isAssignableFrom(type) && !EventInput.class.isAssignableFrom(type)) {
            ResteasyContext.popContextData(Providers.class);
            if (current != null)
                ResteasyContext.pushContext(Providers.class, current);
            if (obj instanceof ProvidersContextRetainer)
                ((ProvidersContextRetainer) obj).setProviders(configuration);
        }
    }
}
Also used : AbstractReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarshalledEntity(org.jboss.resteasy.spi.MarshalledEntity) ProvidersContextRetainer(org.jboss.resteasy.core.ProvidersContextRetainer) Providers(jakarta.ws.rs.ext.Providers) ClientReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext) ProcessingException(jakarta.ws.rs.ProcessingException) IOException(java.io.IOException) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(jakarta.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InputStreamToByteArray(org.jboss.resteasy.util.InputStreamToByteArray) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 5 with Providers

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

the class PriorityTest method testContextResolverPriorityOverride_2.

@Test
public void testContextResolverPriorityOverride_2() {
    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("K");
                return null;
            }
        }, 1);
        webTarget.register(new ContextResolver<String>() {

            @Override
            public String getContext(Class<?> type) {
                result.append("O");
                return null;
            }
        }, 0);
        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)

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