use of jakarta.ws.rs.core.MultivaluedMap in project minijax by minijax.
the class HttpHeadersTest method testMultiple.
@Test
void testMultiple() throws Exception {
final Map<String, List<String>> headerMap = new HashMap<>();
headerMap.computeIfAbsent("X-Foo", k -> new ArrayList<>()).add("bar");
headerMap.computeIfAbsent("X-Foo", k -> new ArrayList<>()).add("baz");
final WebSocketHttpExchange exchange = mock(WebSocketHttpExchange.class);
when(exchange.getRequestHeaders()).thenReturn(headerMap);
final MinijaxUndertowWebSocketHttpHeaders httpHeaders = new MinijaxUndertowWebSocketHttpHeaders(exchange);
assertEquals(Arrays.asList("bar", "baz"), httpHeaders.getRequestHeader("X-Foo"));
final MultivaluedMap<String, String> multiMap = httpHeaders.getRequestHeaders();
assertNotNull(multiMap);
assertEquals(multiMap, httpHeaders.getRequestHeaders());
}
use of jakarta.ws.rs.core.MultivaluedMap in project hugegraph-common by hugegraph.
the class AbstractRestClient method buildRequest.
private Pair<Builder, Entity<?>> buildRequest(String path, String id, Object object, MultivaluedMap<String, Object> headers, Map<String, Object> params) {
WebTarget target = this.target;
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> param : params.entrySet()) {
target = target.queryParam(param.getKey(), param.getValue());
}
}
Builder builder = id == null ? target.path(path).request() : target.path(path).path(encode(id)).request();
String encoding = null;
if (headers != null && !headers.isEmpty()) {
// Add headers
builder = builder.headers(headers);
encoding = (String) headers.getFirst("Content-Encoding");
}
// Add auth header
this.attachAuthToRequest(builder);
/*
* We should specify the encoding of the entity object manually,
* because Entity.json() method will reset "content encoding =
* null" that has been set up by headers before.
*/
Entity<?> entity;
if (encoding == null) {
entity = Entity.json(object);
} else {
Variant variant = new Variant(MediaType.APPLICATION_JSON_TYPE, (String) null, encoding);
entity = Entity.entity(object, variant);
}
return Pair.of(builder, entity);
}
use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.
the class InMemoryClientEngine method loadHttpMethod.
public // throws Exception
void loadHttpMethod(// throws Exception
ClientInvocation request, // throws Exception
MockHttpRequest mockHttpRequest) {
if (request.getEntity() instanceof Form) {
commitHeaders(request, mockHttpRequest);
Form form = (Form) request.getEntity();
MultivaluedMap<String, String> map = form.asMap();
for (Map.Entry<String, List<String>> formParam : map.entrySet()) {
String key = formParam.getKey();
for (String value : formParam.getValue()) {
mockHttpRequest.getFormParameters().add(key, value);
}
}
} else if (request.getEntity() != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MediaType bodyContentType = request.getHeaders().getMediaType();
request.getHeaders().asMap().add(HttpHeaders.CONTENT_TYPE, bodyContentType.toString());
try {
request.writeRequestBody(baos);
} catch (IOException e) {
throw new RuntimeException(e);
}
// commit headers after byte array is complete.
commitHeaders(request, mockHttpRequest);
mockHttpRequest.content(baos.toByteArray());
mockHttpRequest.contentType(bodyContentType);
} else {
commitHeaders(request, mockHttpRequest);
}
}
use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.
the class ConfigurationInheritanceTest method testMessageBodyReaderInheritance.
@Test
public void testMessageBodyReaderInheritance() {
Client client = ClientBuilder.newClient();
try {
fakeHttpServer.start();
WebTarget parentWebTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
WebTarget childWebTarget = parentWebTarget.path("path");
childWebTarget.register((ClientResponseFilter) (containerRequestContext, containerResponseContext) -> {
containerResponseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
containerResponseContext.setEntityStream(new ByteArrayInputStream("hello".getBytes()));
});
// Registration on parent MUST not affect the child
AtomicInteger parentMessageBodyReaderCounter = new AtomicInteger(0);
MessageBodyReader<String> parentMessageBodyReader = new MessageBodyReader<String>() {
@Override
public String readFrom(Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return null;
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
parentMessageBodyReaderCounter.incrementAndGet();
return false;
}
};
parentWebTarget.register(parentMessageBodyReader);
childWebTarget.request().get().readEntity(String.class);
Assert.assertEquals(0, parentMessageBodyReaderCounter.get());
// Child MUST only use the snapshot configuration of the parent
// taken at child creation time.
AtomicInteger childMessageBodyReaderCounter = new AtomicInteger(0);
MessageBodyReader<String> childMessageBodyReader = new MessageBodyReader<String>() {
@Override
public String readFrom(Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return null;
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
childMessageBodyReaderCounter.incrementAndGet();
return false;
}
};
childWebTarget.register(childMessageBodyReader);
childWebTarget.request().get().readEntity(String.class);
Assert.assertEquals(1, childMessageBodyReaderCounter.get());
Assert.assertEquals(0, parentMessageBodyReaderCounter.get());
} finally {
client.close();
}
}
use of jakarta.ws.rs.core.MultivaluedMap in project resteasy by resteasy.
the class DKIMSignature method sign.
/**
* Headers can be a {@literal Map<String, Object> or a Map<String, List<Object>>}. This gives some compatibility with
* JAX-RS's MultivaluedMap. If a map of lists, every value of each header duplicate will be added.
* <p>
*
* @param headers headers map
* @param body if null, bh field will not be set or provided
* @param defaultKey will be used if privateKey is null
* @throws SignatureException if security exception occurred
*/
public void sign(Map headers, byte[] body, PrivateKey defaultKey) throws SignatureException {
PrivateKey key = privateKey == null ? defaultKey : privateKey;
if (key == null) {
throw new SignatureException(Messages.MESSAGES.privateKeyIsNull());
}
attributes.put(VERSION, "1");
attributes.put(ALGORITHM, SigningAlgorithm.SHA256withRSA.getRfcNotation());
attributes.put(CANONICALIZATION, "simple/simple");
String algorithm = SigningAlgorithm.SHA256withRSA.getJavaSecNotation();
String hashAlgorithm = SigningAlgorithm.SHA256withRSA.getJavaHashNotation();
Signature signature = null;
try {
signature = Signature.getInstance(algorithm);
signature.initSign(key);
} catch (Exception e) {
throw new SignatureException(e);
}
if (this.headers.size() > 0) {
StringBuffer headerCat = new StringBuffer();
int count = 0;
for (int i = 0; i < this.headers.size(); i++) {
String name = this.headers.get(i);
if (i > 0)
headerCat.append(":");
headerCat.append(name);
}
attributes.put(HEADERS, headerCat.toString());
updateSignatureWithHeader(headers, signature);
}
if (body != null && bodyHashRequired) {
String encodedBodyHash = calculateEncodedHash(body, hashAlgorithm);
attributes.put(BODY_HASH, encodedBodyHash);
}
StringBuffer dosetaBuffer = new StringBuffer();
boolean first = true;
for (Map.Entry<String, String> entry : attributes.entrySet()) {
if (first)
first = false;
else
dosetaBuffer.append(";");
dosetaBuffer.append(entry.getKey()).append("=").append(entry.getValue());
}
if (!first)
dosetaBuffer.append(";");
dosetaBuffer.append("b=");
String dosetaHeader = dosetaBuffer.toString();
signature.update(dosetaHeader.getBytes());
byte[] signed = signature.sign();
setSignature(signed);
String base64Signature = Base64.getEncoder().encodeToString(signed);
dosetaHeader += base64Signature;
// System.out.println("***: " + dosetaHeader);
this.headerValue = dosetaHeader;
}
Aggregations