Search in sources :

Example 66 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project Payara by payara.

the class RestClientBase method buildMultivalueMap.

private MultivaluedMap<String, Object> buildMultivalueMap(Map<String, Object> payload) {
    MultivaluedMap formData = new MultivaluedHashMap();
    for (final Map.Entry<String, Object> entry : payload.entrySet()) {
        Object value = entry.getValue();
        if (JsonObject.NULL.equals(value)) {
            value = null;
        } else if (value != null) {
            value = value.toString();
        }
        formData.add(entry.getKey(), value);
    }
    return formData;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JsonObject(javax.json.JsonObject) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 67 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project scheduling by ow2-proactive.

the class WorkflowVariablesTransformerTest method testTwoVariablesMap.

@Test
public void testTwoVariablesMap() {
    Map<String, String> expectedVariables = Maps.newHashMap();
    expectedVariables.put("KEY1", "VALUE1");
    expectedVariables.put("KEY2", "VALUE2");
    PathSegment pathSegment = mock(PathSegment.class);
    MultivaluedMap<String, String> multivalueMap = new MultivaluedHashMap();
    multivalueMap.put("KEY1", Lists.newArrayList("VALUE1"));
    multivalueMap.put("KEY2", Lists.newArrayList("VALUE2"));
    when(pathSegment.getMatrixParameters()).thenReturn(multivalueMap);
    Map<String, String> variables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
    assertThat(variables, is(expectedVariables));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Example 68 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project javaee7-samples by javaee-samples.

the class TestServlet method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>JAX-RS 2 Client Invocation Async API</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>JAX-RS 2 Client Invocation Async API at " + request.getContextPath() + "</h1>");
    out.println("Initializing client...<br>");
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/resource");
    // GET
    out.print("Building a GET request ...<br>");
    Invocation i1 = target.request().buildGet();
    out.print("GET request ready ...<br>");
    // POST
    out.print("Building a POST request...<br>");
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.add("name", "Name");
    map.add("age", "17");
    Invocation i2 = target.request().buildPost(Entity.form(map));
    out.print("POSTed request ready...<br>");
    Future<Response> f1 = i1.submit();
    Future<String> f2 = i2.submit(String.class);
    try {
        Response r1 = f1.get();
        out.println("Response from r1: " + r1.readEntity(String.class) + "<br>");
        String r2 = f2.get();
        out.println("Response from r2: " + r2 + "<br>");
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println("... done.<br>");
    out.println("</body>");
    out.println("</html>");
}
Also used : Invocation(javax.ws.rs.client.Invocation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter)

Example 69 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project javaee7-samples by javaee-samples.

the class TestServlet method processRequest.

/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Request Binding</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Resource Validation in JAX-RS</h1>");
    Client client = ClientBuilder.newClient();
    List<WebTarget> targets = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        targets.add(client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/names" + (i + 1)));
    }
    for (WebTarget target : targets) {
        out.println("<h2>Using target: " + target.getUri() + "</h2>");
        MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
        out.print("<br><br>POSTing with valid data ...<br>");
        map.add("firstName", "Sheldon");
        map.add("lastName", "Cooper");
        map.add("email", "random@example.com");
        Response r = target.request().post(Entity.form(map));
        printResponseStatus(out, r, 200);
        out.println();
        out.print("<br><br>POSTing with invalid (null) \"firstName\" ...<br>");
        map.putSingle("firstName", null);
        r = target.request().post(Entity.form(map));
        printResponseStatus(out, r, 200);
        out.println();
        out.print("<br><br>POSTing with invalid (null) \"lastName\" ...<br>");
        map.putSingle("firstName", "Sheldon");
        map.putSingle("lastName", null);
        r = target.request().post(Entity.form(map));
        printResponseStatus(out, r, 400);
        out.print("<br><br>POSTing with invalid (missing @) email \"email\" ...<br>");
        map.putSingle("lastName", "Cooper");
        map.putSingle("email", "randomexample.com");
        r = target.request().post(Entity.form(map));
        printResponseStatus(out, r, 400);
        out.print("<br><br>POSTing with invalid (missing .com) email \"email\" ...<br>");
        map.putSingle("email", "random@examplecom");
        r = target.request().post(Entity.form(map));
        printResponseStatus(out, r, 400);
    }
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/nameadd");
    out.println("<h2>Using target: " + target.getUri() + "</h2>");
    out.print("<br><br>POSTing using @Valid (all vaild data) ...<br>");
    Response r = target.request().post(Entity.json(new Name("Sheldon", "Cooper", "sheldon@cooper.com")));
    printResponseStatus(out, r, 200);
    out.print("<br><br>POSTing using @Valid, with invalid (null) \"firstName\" ...<br>");
    r = target.request().post(Entity.json(new Name(null, "Cooper", "sheldon@cooper.com")));
    printResponseStatus(out, r, 400);
    out.print("<br><br>POSTing using @Valid, with invalid (null) \"lastName\" ...<br>");
    r = target.request().post(Entity.json(new Name("Sheldon", null, "sheldon@cooper.com")));
    printResponseStatus(out, r, 400);
    out.print("<br><br>POSTing using @Valid, with invalid (missing @) email \"email\" ...<br>");
    r = target.request().post(Entity.json(new Name("Sheldon", "Cooper", "sheldoncooper.com")));
    printResponseStatus(out, r, 400);
    out.println("<br>... done.<br>");
    out.println("</body>");
    out.println("</html>");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) PrintWriter(java.io.PrintWriter)

Example 70 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project jersey by jersey.

the class TestContainerRequest method setEntity.

void setEntity(final Object requestEntity, final MessageBodyWorkers workers) {
    final Object entity;
    final GenericType entityType;
    if (requestEntity instanceof GenericEntity) {
        entity = ((GenericEntity) requestEntity).getEntity();
        entityType = new GenericType(((GenericEntity) requestEntity).getType());
    } else {
        entity = requestEntity;
        entityType = new GenericType(requestEntity.getClass());
    }
    final byte[] entityBytes;
    if (entity != null) {
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStream stream = null;
        try {
            stream = workers.writeTo(entity, entity.getClass(), entityType.getType(), new Annotation[0], getMediaType(), new MultivaluedHashMap<String, Object>(getHeaders()), getPropertiesDelegate(), output, Collections.<WriterInterceptor>emptyList());
        } catch (final IOException | WebApplicationException ex) {
            LOGGER.log(Level.SEVERE, "Transforming entity to input stream failed.", ex);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (final IOException e) {
                // ignore
                }
            }
        }
        entityBytes = output.toByteArray();
    } else {
        entityBytes = new byte[0];
    }
    setEntity(new ByteArrayInputStream(entityBytes));
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) GenericType(javax.ws.rs.core.GenericType) WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntity(javax.ws.rs.core.GenericEntity)

Aggregations

MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)133 Response (javax.ws.rs.core.Response)98 Builder (javax.ws.rs.client.Invocation.Builder)78 JSONException (org.codehaus.jettison.json.JSONException)77 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)77 Test (org.testng.annotations.Test)75 JSONObject (org.codehaus.jettison.json.JSONObject)73 Parameters (org.testng.annotations.Parameters)73 BaseTest (org.xdi.oxauth.BaseTest)73 TokenRequest (org.xdi.oxauth.client.TokenRequest)39 Test (org.junit.Test)28 URISyntaxException (java.net.URISyntaxException)27 OxAuthCryptoProvider (org.xdi.oxauth.model.crypto.OxAuthCryptoProvider)21 RegisterResponse (org.xdi.oxauth.client.RegisterResponse)18 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UserInfoRequest (org.xdi.oxauth.client.UserInfoRequest)12 URI (java.net.URI)11 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9