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));
}
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));
}
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);
}
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();
}
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\"}");
}
Aggregations