Search in sources :

Example 6 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.

the class ReactorNettyClientHttpEngineTest method testThatMessageBodyWriterHeadersAreRespected.

@Test
public void testThatMessageBodyWriterHeadersAreRespected() {
    final String WRITER_ESTABLISHED_HEADER_VAL = "Binary";
    class StringWriter implements MessageBodyWriter<String> {

        @Override
        public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return type == String.class;
        }

        @Override
        public long getSize(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return s.length();
        }

        @Override
        public void writeTo(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            httpHeaders.add("Content-Encoding", WRITER_ESTABLISHED_HEADER_VAL);
            entityStream.write(s.getBytes());
        }
    }
    final WebTarget target = client.target(url("/headers/content-encoding")).register(new StringWriter());
    final Response response = target.request().post(Entity.text("marcel sent me some text!"));
    assertEquals(200, response.getStatus());
    assertEquals(WRITER_ESTABLISHED_HEADER_VAL, response.readEntity(String.class));
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) ClientResponse(org.jboss.resteasy.client.jaxrs.internal.ClientResponse) Response(jakarta.ws.rs.core.Response) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) GenericType(jakarta.ws.rs.core.GenericType) OutputStream(java.io.OutputStream) MediaType(jakarta.ws.rs.core.MediaType) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebTarget(jakarta.ws.rs.client.WebTarget) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) Test(org.junit.Test)

Example 7 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.

the class ReactorNettyClientHttpEngineTest method testCaseInsensitiveHeaderReplace.

@Test
public void testCaseInsensitiveHeaderReplace() {
    final String CONTENT_TYPE_CLIENT_HEADER_VAL = "text/plain";
    final String CONTENT_TYPE_WRITER_HEADER_VAL = "application/json";
    final String CONTENT_TYPE_UC_HEADER_NAME = "CONTENT-TYPE";
    final String CONTENT_TYPE_LC_HEADER_NAME = "content-type";
    class StringWriter implements MessageBodyWriter<String> {

        @Override
        public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return type == String.class;
        }

        @Override
        public long getSize(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return s.length();
        }

        @Override
        public void writeTo(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
            httpHeaders.replace(CONTENT_TYPE_LC_HEADER_NAME, Collections.singletonList(CONTENT_TYPE_WRITER_HEADER_VAL));
            entityStream.write(s.getBytes(StandardCharsets.UTF_8));
        }
    }
    final WebTarget target = client.target(url("/headers/content-type")).register(new StringWriter());
    final Response response = target.request().header(CONTENT_TYPE_UC_HEADER_NAME, CONTENT_TYPE_CLIENT_HEADER_VAL).post(Entity.text("{'inputKey' : 'value'}"));
    assertEquals(200, response.getStatus());
    assertEquals(CONTENT_TYPE_WRITER_HEADER_VAL, response.readEntity(String.class));
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) ClientResponse(org.jboss.resteasy.client.jaxrs.internal.ClientResponse) Response(jakarta.ws.rs.core.Response) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) GenericType(jakarta.ws.rs.core.GenericType) OutputStream(java.io.OutputStream) MediaType(jakarta.ws.rs.core.MediaType) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebTarget(jakarta.ws.rs.client.WebTarget) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) Test(org.junit.Test)

Example 8 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.

the class ClientWebTarget method queryParamsNoTemplate.

@Override
public ResteasyWebTarget queryParamsNoTemplate(MultivaluedMap<String, Object> parameters) throws IllegalArgumentException, NullPointerException {
    client.abortIfClosed();
    if (parameters == null)
        throw new NullPointerException(Messages.MESSAGES.parametersWasNull());
    ResteasyUriBuilder copy;
    if (uriBuilder instanceof ResteasyUriBuilder) {
        copy = (ResteasyUriBuilder) uriBuilder.clone();
    } else {
        copy = ResteasyUriBuilder.fromTemplate(uriBuilder.toTemplate());
    }
    for (Map.Entry<String, List<Object>> entry : parameters.entrySet()) {
        String[] stringValues = toStringValues(entry.getValue().toArray());
        for (String val : stringValues) {
            copy.clientQueryParam(entry.getKey(), val);
        }
    }
    return newInstance(client, copy, configuration);
}
Also used : ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) List(java.util.List) HashMap(java.util.HashMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 9 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.

the class HttpAuthorizationFilter method repeatRequest.

private static boolean repeatRequest(final ClientRequestContext request, final ClientResponseContext response, final String authHeader) {
    if (authHeader == null) {
        return false;
    }
    final Client client = request.getClient();
    final String method = request.getMethod();
    final MediaType mediaType = request.getMediaType();
    final Invocation.Builder builder = client.target(request.getUri()).request(mediaType);
    final MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<>();
    for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
        if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
            continue;
        }
        newHeaders.put(entry.getKey(), entry.getValue());
    }
    newHeaders.add(HttpHeaders.AUTHORIZATION, authHeader);
    builder.headers(newHeaders);
    builder.property(RESPONSE_PROCESSED, true);
    final Invocation invocation;
    if (request.getEntity() == null) {
        invocation = builder.build(method);
    } else {
        invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
    }
    final Response newResponse = invocation.invoke();
    if (newResponse.hasEntity()) {
        response.setEntityStream(newResponse.readEntity(InputStream.class));
    }
    final MultivaluedMap<String, String> headers = response.getHeaders();
    headers.clear();
    headers.putAll(newResponse.getStringHeaders());
    response.setStatus(newResponse.getStatus());
    return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
Also used : Invocation(jakarta.ws.rs.client.Invocation) InputStream(java.io.InputStream) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) Response(jakarta.ws.rs.core.Response) MediaType(jakarta.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Client(jakarta.ws.rs.client.Client) LimitMap(dev.resteasy.client.util.common.LimitMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap)

Example 10 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.

the class AsyncIOTest method testWriters.

@Test
public void testWriters() {
    // vertx runs on the IO thread so we can't allow blocking interceptors
    WebTarget target = client.target(generateURL("/async-io/blocking/reject-blocking-interceptor"));
    try {
        target.request().get(String.class);
        Assert.fail();
    } catch (InternalServerErrorException x) {
    // good
    }
    testWriters("text", "OK");
    testWriters("bytes", "OK");
    testWriters("default-text", "K");
    testWriters("boolean", "true");
    testWriters("number", "42");
    testWriters("input-stream", "OK");
    // Does not handle async because I think the current blocking implementation is wrong
    // testWriters("reader", "OK");
    testWriters("data-source", "OK");
    testWriters("source", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>");
    testWriters("document", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><foo/>");
    testWriters("file", "OK");
    testWriters("file-range", "OK");
    testWriters("streaming-output", "OK");
    testWriters("iioimage", IIOImage.class, image -> {
        Assert.assertEquals(1, image.getRenderedImage().getHeight());
        Assert.assertEquals(1, image.getRenderedImage().getWidth());
    });
    testWriters("form-url-encoded", MultivaluedMap.class, map -> {
        Assert.assertEquals(1, map.size());
        Assert.assertEquals(Arrays.asList("bar"), map.get("foo"));
    });
    testWriters("jax-rs-form", Form.class, form -> {
        MultivaluedMap<String, String> map = form.asMap();
        Assert.assertEquals(1, map.size());
        Assert.assertEquals(Arrays.asList("bar"), map.get("foo"));
    });
    // atom provider
    testWriters("atom-feed", Feed.class, feed -> {
        Assert.assertEquals("fubar", feed.getLanguage());
    });
    // jaxb provider
    // This does not work
    // testWriters("jaxb-xml-see-also", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>");
    testWriters("jaxb-xml-root-element", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><jaxbXmlRootElement><foo>bar</foo></jaxbXmlRootElement>");
    testWriters("jaxb-element", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><jaxbXmlRootElement><foo>bar</foo></jaxbXmlRootElement>");
    testWriters("jaxb-xml-type", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><jaxbXmlType><foo>bar</foo></jaxbXmlType>");
    testWriters("jaxb-collection", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><collection><jaxbXmlRootElement><foo>bar</foo></jaxbXmlRootElement></collection>");
    testWriters("jaxb-map", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><map><entry key=\"foo\"><jaxbXmlRootElement><foo>bar</foo></jaxbXmlRootElement></entry></map>");
    // multipart provider
    testWriters("multipart-output", MultipartInput.class, multipart -> {
        Assert.assertEquals(1, multipart.getParts().size());
        Assert.assertTrue(MediaType.TEXT_PLAIN_TYPE.isCompatible(multipart.getParts().get(0).getMediaType()));
        try {
            Assert.assertEquals("foo", multipart.getParts().get(0).getBody(String.class, String.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    testWriters("multipart-form-data-output", MultipartFormDataInput.class, multipart -> {
        Assert.assertEquals(1, multipart.getParts().size());
        Assert.assertTrue(MediaType.TEXT_PLAIN_TYPE.isCompatible(multipart.getParts().get(0).getMediaType()));
        try {
            Assert.assertEquals("bar", multipart.getParts().get(0).getBody(String.class, String.class));
            Assert.assertEquals("bar", multipart.getFormDataPart("foo", String.class, String.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    testWriters("multipart-related-output", MultipartRelatedInput.class, multipart -> {
        Assert.assertEquals(1, multipart.getParts().size());
        Assert.assertTrue(MediaType.TEXT_PLAIN_TYPE.isCompatible(multipart.getParts().get(0).getMediaType()));
        try {
            Assert.assertEquals("foo", multipart.getParts().get(0).getBody(String.class, String.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    testWriters("multipart-list", new GenericType<List<String>>() {
    }, multipart -> {
        Assert.assertEquals(1, multipart.size());
        Assert.assertEquals("bar", multipart.get(0));
    });
    testWriters("multipart-map", new GenericType<Map<String, String>>() {
    }, multipart -> {
        Assert.assertEquals(1, multipart.size());
        Assert.assertEquals("bar", multipart.get("foo"));
    });
    testWriters("multipart-form-annotation", AsyncIOResource.MyForm.class, multipart -> {
        Assert.assertEquals("bar", multipart.foo);
    }, new MultipartForm() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return MultipartForm.class;
        }
    });
    testWriters("multipart-mime", MimeMultipart.class, multipart -> {
        try {
            Assert.assertEquals(1, multipart.getCount());
            Assert.assertEquals("OK", multipart.getBodyPart(0).getContent());
        } catch (MessagingException | IOException e) {
            throw new RuntimeException(e);
        }
    });
    testWriters("multipart-xop-related", AsyncIOResource.XopRelatedForm.class, multipart -> {
        Assert.assertArrayEquals(AsyncIOResource.OK_BYTES, multipart.foo);
    }, new XopWithMultipartRelated() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return XopWithMultipartRelated.class;
        }
    });
    // jsonp provider
    testWriters("jsonp-array", JsonArray.class, array -> {
        Assert.assertEquals(1, array.size());
        Assert.assertTrue(array.get(0) instanceof JsonString);
        Assert.assertEquals("foo", ((JsonString) array.get(0)).getString());
    });
    testWriters("jsonp-structure", JsonArray.class, array -> {
        Assert.assertEquals(1, array.size());
        Assert.assertTrue(array.get(0) instanceof JsonString);
        Assert.assertEquals("foo", ((JsonString) array.get(0)).getString());
    });
    testWriters("jsonp-object", JsonObject.class, object -> {
        Assert.assertEquals(1, object.size());
        Assert.assertTrue(object.get("foo") instanceof JsonString);
        Assert.assertEquals("bar", object.getString("foo"));
    });
    testWriters("jsonp-value", JsonString.class, val -> {
        Assert.assertEquals("foo", val.getString());
    });
    // SSE
    testWriters("sse", SseEventInputImpl.class, val -> {
        try {
            InboundSseEvent event = val.read();
            Assert.assertEquals("foo", event.getName());
            Assert.assertEquals("bar\ngee", event.readData());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    // Jackson
    testWriters("jackson", "{\"foo\":\"bar\"}");
}
Also used : MessagingException(jakarta.mail.MessagingException) InboundSseEvent(jakarta.ws.rs.sse.InboundSseEvent) MultipartForm(org.jboss.resteasy.annotations.providers.multipart.MultipartForm) JsonString(jakarta.json.JsonString) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) List(java.util.List) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) WebTarget(jakarta.ws.rs.client.WebTarget) JsonString(jakarta.json.JsonString) XopWithMultipartRelated(org.jboss.resteasy.annotations.providers.multipart.XopWithMultipartRelated) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)51 Map (java.util.Map)32 List (java.util.List)26 MediaType (jakarta.ws.rs.core.MediaType)20 Response (jakarta.ws.rs.core.Response)13 IOException (java.io.IOException)12 Type (java.lang.reflect.Type)11 ArrayList (java.util.ArrayList)11 WebTarget (jakarta.ws.rs.client.WebTarget)10 OutputStream (java.io.OutputStream)10 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 WebApplicationException (jakarta.ws.rs.WebApplicationException)9 LinkedHashMap (java.util.LinkedHashMap)9 MessageBodyWriter (jakarta.ws.rs.ext.MessageBodyWriter)8 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)8 Client (jakarta.ws.rs.client.Client)7 MultivaluedHashMap (jakarta.ws.rs.core.MultivaluedHashMap)7 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)6 InputStream (java.io.InputStream)6