Search in sources :

Example 11 with MultivaluedMap

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

the class MapMultipartFormDataWriter method asyncWriteTo.

@Override
public CompletionStage<Void> asyncWriteTo(Map<String, Object> map, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, AsyncOutputStream entityStream) {
    PartType partType = FindAnnotation.findAnnotation(annotations, PartType.class);
    MediaType partMediaType = MediaType.valueOf(partType.value());
    MultipartFormDataOutput output = new MultipartFormDataOutput();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        output.addFormData(entry.getKey(), entry.getValue(), partMediaType);
    }
    return asyncWrite(output, mediaType, httpHeaders, entityStream, annotations);
}
Also used : PartType(org.jboss.resteasy.annotations.providers.multipart.PartType) MediaType(jakarta.ws.rs.core.MediaType) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 12 with MultivaluedMap

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

the class PriorityTest method testMessageBodyReaderPriorityOverride.

@Test
public void testMessageBodyReaderPriorityOverride() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        webTarget.register((ClientResponseFilter) (containerRequestContext, containerResponseContext) -> {
            containerResponseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
            containerResponseContext.setEntityStream(new ByteArrayInputStream("hello".getBytes()));
        });
        StringBuilder result = new StringBuilder();
        webTarget.register(new MessageBodyReader<String>() {

            @Override
            public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
                result.append("K");
                return false;
            }

            @Override
            public String readFrom(Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
                return null;
            }
        }, 1);
        webTarget.register(new MessageBodyReader<String>() {

            @Override
            public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
                result.append("O");
                return false;
            }

            @Override
            public String readFrom(Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
                return null;
            }
        }, 0);
        webTarget.request().get().readEntity(String.class);
        Assert.assertEquals("OK", result.toString());
    } finally {
        client.close();
    }
}
Also used : Context(jakarta.ws.rs.core.Context) ReaderInterceptorContext(jakarta.ws.rs.ext.ReaderInterceptorContext) WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) FakeHttpServer(org.jboss.resteasy.test.common.FakeHttpServer) ClientResponseFilterRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.ClientResponseFilterRegistryImpl) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) WebApplicationException(jakarta.ws.rs.WebApplicationException) PriorityContainerResponseFilter3(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter3) PriorityContainerResponseFilter1(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter1) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) ContainerResponseFilterRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.ContainerResponseFilterRegistryImpl) PriorityContainerResponseFilter2(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter2) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) JaxrsInterceptorRegistry(org.jboss.resteasy.spi.interception.JaxrsInterceptorRegistry) OutputStream(java.io.OutputStream) PriorityClientRequestFilter3(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter3) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) Client(jakarta.ws.rs.client.Client) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) Assert.assertTrue(org.junit.Assert.assertTrue) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Test(org.junit.Test) JaxrsInterceptorRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.JaxrsInterceptorRegistryImpl) PriorityClientResponseFilter1(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter1) PriorityClientResponseFilter2(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter2) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) PriorityClientRequestFilter1(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter1) PriorityClientResponseFilter3(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter3) Entity(jakarta.ws.rs.client.Entity) PriorityClientRequestFilter2(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter2) Rule(org.junit.Rule) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) ContextResolver(jakarta.ws.rs.ext.ContextResolver) Annotation(java.lang.annotation.Annotation) Providers(jakarta.ws.rs.ext.Providers) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) Assert(org.junit.Assert) InputStream(java.io.InputStream) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) WebApplicationException(jakarta.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(jakarta.ws.rs.core.MediaType) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) Test(org.junit.Test)

Example 13 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project org.openntf.xsp.jakartaee by OpenNTF.

the class TestNoSQL method testNoSql.

@SuppressWarnings("unchecked")
@Test
public void testNoSql() throws JsonException {
    Client client = getAnonymousClient();
    // $NON-NLS-1$
    WebTarget target = client.target(getRestUrl(null) + "/nosql?lastName=CreatedUnitTest");
    {
        Response response = target.request().get();
        String json = response.readEntity(String.class);
        Map<String, Object> jsonObject = (Map<String, Object>) JsonParser.fromJson(JsonJavaFactory.instance, json);
        // $NON-NLS-1$
        List<Object> byQueryLastName = (List<Object>) jsonObject.get("byQueryLastName");
        assertNotNull(byQueryLastName, () -> String.valueOf(jsonObject));
        assertTrue(byQueryLastName.isEmpty(), () -> String.valueOf(jsonObject));
    }
    // Now use the MVC endpoint to create one, which admittedly is outside this test
    {
        MultivaluedMap<String, String> payload = new MultivaluedHashMap<>();
        // $NON-NLS-1$ //$NON-NLS-2$
        payload.putSingle("firstName", "foo");
        // $NON-NLS-1$ //$NON-NLS-2$
        payload.putSingle("lastName", "CreatedUnitTest");
        // $NON-NLS-1$
        WebTarget postTarget = client.target(getRestUrl(null) + "/nosql/create");
        Response response = postTarget.request().post(Entity.form(payload));
        assertEquals(303, response.getStatus());
    }
    // There should be at least one now
    {
        Response response = target.request().get();
        String json = response.readEntity(String.class);
        Map<String, Object> jsonObject = (Map<String, Object>) JsonParser.fromJson(JsonJavaFactory.instance, json);
        // $NON-NLS-1$
        List<Map<String, Object>> byQueryLastName = (List<Map<String, Object>>) jsonObject.get("byQueryLastName");
        assertFalse(byQueryLastName.isEmpty());
        Map<String, Object> entry = byQueryLastName.get(0);
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("CreatedUnitTest", entry.get("lastName"));
        // $NON-NLS-1$
        assertFalse(((String) entry.get("unid")).isEmpty());
    }
}
Also used : Response(jakarta.ws.rs.core.Response) List(java.util.List) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test) AbstractWebClientTest(it.org.openntf.xsp.jakartaee.AbstractWebClientTest)

Example 14 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project incubator-hugegraph by apache.

the class NeighborRankTraverser method neighborRank.

public List<Map<Id, Double>> neighborRank(Id source, List<Step> steps) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
    MultivaluedMap<Id, Node> sources = newMultivalueMap();
    sources.add(source, new Node(source, null));
    boolean sameLayerTransfer = true;
    long access = 0;
    // Results: ranks of each layer
    List<Ranks> ranks = newList();
    ranks.add(Ranks.of(source, 1.0));
    for (Step step : steps) {
        Ranks lastLayerRanks = ranks.get(ranks.size() - 1);
        Map<Id, Double> sameLayerIncrRanks = newMap();
        List<Adjacencies> adjacencies = newList();
        MultivaluedMap<Id, Node> newVertices = newMultivalueMap();
        // Traversal vertices of previous level
        for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
            Id vertex = entry.getKey();
            Iterator<Edge> edges = this.edgesOfVertex(vertex, step.edgeStep);
            Adjacencies adjacenciesV = new Adjacencies(vertex);
            Set<Id> sameLayerNodesV = newIdSet();
            Map<Integer, Set<Id>> prevLayerNodesV = newMap();
            while (edges.hasNext()) {
                HugeEdge edge = (HugeEdge) edges.next();
                Id target = edge.id().otherVertexId();
                // Determine whether it belongs to the same layer
                if (this.belongToSameLayer(sources.keySet(), target, sameLayerNodesV)) {
                    continue;
                }
                /*
                     * Determine whether it belongs to the previous layers,
                     * if it belongs to, update the weight, but don't pass
                     * any more
                     */
                if (this.belongToPrevLayers(ranks, target, prevLayerNodesV)) {
                    continue;
                }
                for (Node n : entry.getValue()) {
                    // If have loop, skip target
                    if (n.contains(target)) {
                        continue;
                    }
                    Node newNode = new Node(target, n);
                    adjacenciesV.add(newNode);
                    // Add adjacent nodes to sources of next step
                    newVertices.add(target, newNode);
                    checkCapacity(this.capacity, ++access, "neighbor rank");
                }
            }
            long degree = sameLayerNodesV.size() + prevLayerNodesV.size() + adjacenciesV.nodes().size();
            if (degree == 0L) {
                continue;
            }
            adjacenciesV.degree(degree);
            adjacencies.add(adjacenciesV);
            double incr = lastLayerRanks.getOrDefault(vertex, 0.0) * this.alpha / degree;
            // Merge the increment of the same layer node
            this.mergeSameLayerIncrRanks(sameLayerNodesV, incr, sameLayerIncrRanks);
            // Adding contributions to the previous layers
            this.contributePrevLayers(ranks, incr, prevLayerNodesV);
        }
        Ranks newLayerRanks;
        if (sameLayerTransfer) {
            // First contribute to last layer, then pass to the new layer
            this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
            newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
        } else {
            // First pass to the new layer, then contribute to last layer
            newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
            this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
        }
        ranks.add(newLayerRanks);
        // Re-init sources
        sources = newVertices;
    }
    return this.topRanks(ranks, steps);
}
Also used : Set(java.util.Set) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) List(java.util.List) Id(com.baidu.hugegraph.backend.id.Id) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) OrderLimitMap(com.baidu.hugegraph.util.OrderLimitMap) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 15 with MultivaluedMap

use of jakarta.ws.rs.core.MultivaluedMap in project incubator-hugegraph by apache.

the class LoginApiTest method testVerify.

@Test
public void testVerify() {
    Response r;
    String result;
    r = this.login("test", "test");
    result = assertResponseStatus(200, r);
    assertJsonContains(result, "token");
    String token = this.tokenFromResponse(result);
    String path = Paths.get(PATH, "verify").toString();
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
    r = client().get(path, headers);
    result = assertResponseStatus(200, r);
    assertJsonContains(result, "user_id");
    assertJsonContains(result, "user_name");
    Map<String, Object> user = JsonUtil.fromJson(result, new TypeReference<Map<String, Object>>() {
    });
    Assert.assertEquals(this.userId4Test, user.get("user_id"));
    Assert.assertEquals("test", user.get("user_name"));
    String invalidToken = "eyJhbGciOiJIUzI1NiJ9.eyJ1caVyX25hb" + "WUiOiJ0ZXN0IiwidXNlcl9pZCI6Ii02Mzp0ZXN0I" + "iwiZXhwIjoxNjI0MzUzMjUyfQ.kYot-3mSGlfSbE" + "MzxrTs84q8YanhTTxtsKPPG25CNxA";
    headers = new MultivaluedHashMap<>();
    headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + invalidToken);
    r = client().get(path, headers);
    assertResponseStatus(401, r);
    invalidToken = "123.ansfaf";
    headers = new MultivaluedHashMap<>();
    headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + invalidToken);
    r = client().get(path, headers);
    assertResponseStatus(401, r);
}
Also used : Response(jakarta.ws.rs.core.Response) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) 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