Search in sources :

Example 1 with MultivaluedMap

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());
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) java.util(java.util) Cookie(jakarta.ws.rs.core.Cookie) MediaType(jakarta.ws.rs.core.MediaType) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) Assertions(org.junit.jupiter.api.Assertions) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) Test(org.junit.jupiter.api.Test)

Example 2 with MultivaluedMap

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);
}
Also used : Variant(jakarta.ws.rs.core.Variant) JerseyClientBuilder(org.glassfish.jersey.client.JerseyClientBuilder) RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(jakarta.ws.rs.client.Invocation.Builder) WebTarget(jakarta.ws.rs.client.WebTarget) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with MultivaluedMap

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);
    }
}
Also used : Form(jakarta.ws.rs.core.Form) MediaType(jakarta.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 4 with MultivaluedMap

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();
    }
}
Also used : Context(jakarta.ws.rs.core.Context) WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) WebApplicationException(jakarta.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuntimeType(jakarta.ws.rs.RuntimeType) Set(java.util.Set) WebTarget(jakarta.ws.rs.client.WebTarget) Entity(jakarta.ws.rs.client.Entity) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ContextResolver(jakarta.ws.rs.ext.ContextResolver) Annotation(java.lang.annotation.Annotation) ResteasyProviderFactoryImpl(org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) FakeHttpServer(org.jboss.resteasy.test.common.FakeHttpServer) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Configuration(jakarta.ws.rs.core.Configuration) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ArrayList(java.util.ArrayList) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) ConfigurationInheritanceTestFeature6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature6) ConfigurationInheritanceTestFeature1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature1) ConfigurationInheritanceTestFeature5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature5) ConfigurationInheritanceTestFeature4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature4) ConfigurationInheritanceTestFeature3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature3) ConfigurationInheritanceTestFeature2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature2) OutputStream(java.io.OutputStream) ConfigurationInheritanceTestFilter6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter6) Client(jakarta.ws.rs.client.Client) ConfigurationInheritanceTestFilter4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter4) ConfigurationInheritanceTestFilter5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter5) ConfigurationInheritanceTestFilter2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter2) ConfigurationInheritanceTestMessageBodyReader4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader4) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ConfigurationInheritanceTestFilter3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter3) ConfigurationInheritanceTestMessageBodyReader5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader5) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ConfigurationInheritanceTestMessageBodyReader6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader6) ConfigurationInheritanceTestFilter1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter1) IOException(java.io.IOException) Test(org.junit.Test) Rule(org.junit.Rule) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) ConfigurationInheritanceTestMessageBodyReader1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader1) Providers(jakarta.ws.rs.ext.Providers) ConfigurationInheritanceTestMessageBodyReader2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader2) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) ConfigurationInheritanceTestMessageBodyReader3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader3) Assert(org.junit.Assert) ResteasyClientBuilderImpl(org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) RuntimeType(jakarta.ws.rs.RuntimeType) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaType(jakarta.ws.rs.core.MediaType) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 5 with MultivaluedMap

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;
}
Also used : PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) SignatureException(java.security.SignatureException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) SignatureException(java.security.SignatureException)

Aggregations

MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)31 Map (java.util.Map)19 List (java.util.List)16 MediaType (jakarta.ws.rs.core.MediaType)15 IOException (java.io.IOException)12 Response (jakarta.ws.rs.core.Response)10 OutputStream (java.io.OutputStream)10 WebTarget (jakarta.ws.rs.client.WebTarget)9 Type (java.lang.reflect.Type)9 MessageBodyWriter (jakarta.ws.rs.ext.MessageBodyWriter)8 Test (org.junit.Test)8 WebApplicationException (jakarta.ws.rs.WebApplicationException)7 Client (jakarta.ws.rs.client.Client)6 Annotation (java.lang.annotation.Annotation)6 Collectors (java.util.stream.Collectors)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Priority (jakarta.annotation.Priority)4 Priorities (jakarta.ws.rs.Priorities)4