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