Java Code Examples for com.sun.jersey.core.util.MultivaluedMapImpl#add()

The following examples show how to use com.sun.jersey.core.util.MultivaluedMapImpl#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientUtils.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a POST using the specified url and form data.
 *
 * @param uri the uri to post to
 * @param formData the data to post
 * @return the client response of the post
 */
public ClientResponse post(URI uri, Map<String, String> formData) throws ClientHandlerException, UniformInterfaceException {
    // convert the form data
    MultivaluedMapImpl entity = new MultivaluedMapImpl();
    for (String key : formData.keySet()) {
        entity.add(key, formData.get(key));
    }

    // get the resource
    WebResource.Builder resourceBuilder = client.resource(uri).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_FORM_URLENCODED);

    // add the form data if necessary
    if (!entity.isEmpty()) {
        resourceBuilder = resourceBuilder.entity(entity);
    }

    // perform the request
    return resourceBuilder.post(ClientResponse.class);
}
 
Example 2
Source File: NiFiTestUser.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to create a token with the specified username and password.
 *
 * @param url the url
 * @param username the username
 * @param password the password
 * @return response
 * @throws Exception ex
 */
public ClientResponse testCreateToken(String url, String username, String password) throws Exception {
    // convert the form data
    MultivaluedMapImpl entity = new MultivaluedMapImpl();
    entity.add("username", username);
    entity.add("password", password);

    // get the resource
    WebResource.Builder resourceBuilder = addProxiedEntities(client.resource(url).accept(MediaType.TEXT_PLAIN).type(MediaType.APPLICATION_FORM_URLENCODED)).entity(entity);

    // perform the request
    return resourceBuilder.post(ClientResponse.class);
}
 
Example 3
Source File: RemoteNiFiUtils.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Issues a registration request for this NiFi instance for the instance at the baseApiUri.
 *
 * @param baseApiUri uri to register with
 * @return response
 */
public ClientResponse issueRegistrationRequest(String baseApiUri) {
    final URI uri = URI.create(String.format("%s/controller/users", baseApiUri));

    // set up the query params
    MultivaluedMapImpl entity = new MultivaluedMapImpl();
    entity.add("justification", "A Remote instance of NiFi has attempted to create a reference to this NiFi. This action must be approved first.");

    // create the web resource
    WebResource webResource = client.resource(uri);

    // get the client utils and make the request
    return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).entity(entity).post(ClientResponse.class);
}
 
Example 4
Source File: ChannelFinderClientImpl.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
FindByMap(Map<String, String> map) {
    MultivaluedMapImpl mMap = new MultivaluedMapImpl();
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        for (String value : Arrays.asList(entry.getValue().split(","))) {
            mMap.add(key, value.trim());
        }
    }
    this.map = mMap;
}
 
Example 5
Source File: MailgunServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private ClientResponse sendSimpleMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  MultivaluedMapImpl formData = new MultivaluedMapImpl();
  formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.add("to", recipient);
  formData.add("subject", "Simple Mailgun Example");
  formData.add("text", "Plaintext content");
  return webResource
      .type(MediaType.APPLICATION_FORM_URLENCODED)
      .post(ClientResponse.class, formData);
}
 
Example 6
Source File: MailgunServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("VariableDeclarationUsageDistance")
private ClientResponse sendSimpleMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  MultivaluedMapImpl formData = new MultivaluedMapImpl();
  formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.add("to", recipient);
  formData.add("subject", "Simple Mailgun Example");
  formData.add("text", "Plaintext content");
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  return webResource
      .type(MediaType.APPLICATION_FORM_URLENCODED)
      .post(ClientResponse.class, formData);
}
 
Example 7
Source File: HttpMultiValuedMapGetOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
protected MultivaluedMap<String, String> getQueryParams(TestPojo input)
{
  MultivaluedMapImpl map = new MultivaluedMapImpl();

  map.add(input.getName1(), input.getValue11());
  map.add(input.getName1(), input.getValue12());
  map.add(input.getName2(), input.getValue21());
  map.add(input.getName2(), input.getValue22());
  map.add(input.getName2(), input.getValue23());

  return map;
}
 
Example 8
Source File: GetPostTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
	URI getUri = new URI(SERVICE_URI + "test/getObject");
	MultivaluedMapImpl params = new MultivaluedMapImpl();
	params.add("name", "test");
	HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();
	HttpResponse response = client.execute(request);
	assertEquals(200, response.getStatus());
	assertTrue(response.getEntity(TestObject.class).name.equals("test"));
}
 
Example 9
Source File: SecureGetTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailsWithHostNameValidationOn() throws Exception {

	AbstractConfiguration cm = ConfigurationManager.getConfigInstance();

	String name = "GetPostSecureTest" + ".testFailsWithHostNameValidationOn";

	String configPrefix = name + "." + "ribbon";

	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.SecurePort, Integer.toString(testServer2.getPort()));
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsHostnameValidationRequired, "true"); // <--
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsClientAuthRequired, "true");
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStore, FILE_KS1.getAbsolutePath());
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStorePassword, PASSWORD);
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStore, FILE_TS1.getAbsolutePath());
	cm.setProperty(configPrefix + "." + CommonClientConfigKey.TrustStorePassword, PASSWORD);

	RestClient rc = (RestClient) ClientFactory.getNamedClient(name);

	testServer1.accept();

	URI getUri = new URI(SERVICE_URI1 + "test/");
	MultivaluedMapImpl params = new MultivaluedMapImpl();
	params.add("name", "test");
       HttpRequest request = HttpRequest.newBuilder().uri(getUri).queryParams("name", "test").build();

	try{
		rc.execute(request);

		fail("expecting ssl hostname validation error");
	}catch(ClientHandlerException che){
		assertTrue(che.getMessage().indexOf("hostname in certificate didn't match") > -1);
	}
}
 
Example 10
Source File: Rest.java    From hop with Apache License 2.0 4 votes vote down vote up
MultivaluedMapImpl createMultivalueMap( String paramName, String paramValue ) {
  MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
  queryParams.add( paramName, UriComponent.encode( paramValue, UriComponent.Type.QUERY_PARAM ) );
  return queryParams;
}
 
Example 11
Source File: HttpMultiValuedMapGetOperatorTest.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Test
public void testOperator() throws Exception
{
  final List<Map<String, String[]>> receivedRequests = new ArrayList<Map<String, String[]>>();
  Handler handler = new AbstractHandler()
  {
    @Override
    public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
      receivedRequests.add(request.getParameterMap());
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println(request.getParameterNames().nextElement());
      ((Request)request).setHandled(true);
    }

  };

  Server server = new Server(0);
  server.setHandler(handler);
  server.start();

  String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/context";

  TestHttpGetMultiValuedMapOperator operator = new TestHttpGetMultiValuedMapOperator();
  operator.setUrl(url);
  operator.setup(null);

  CollectorTestSink<String> sink = TestUtils.setSink(operator.output, new CollectorTestSink<String>());

  TestPojo pojo = new TestPojo();
  pojo.setName1(KEY1);
  pojo.setValue11(VAL1);
  pojo.setValue12(VAL2);
  pojo.setName2(KEY2);
  pojo.setValue21(VAL1);
  pojo.setValue22(VAL2);
  pojo.setValue23(VAL3);

  operator.input.process(pojo);

  long startTms = System.currentTimeMillis();
  long waitTime = 10000L;

  while (receivedRequests.size() < 3 && System.currentTimeMillis() - startTms < waitTime) {
    Thread.sleep(250);
  }

  Assert.assertEquals("request count", 1, receivedRequests.size());
  Assert.assertEquals("parameter key count", 2, receivedRequests.get(0).size());
  Assert.assertEquals("parameter value count", 2, receivedRequests.get(0).get(KEY1).length);
  Assert.assertEquals("parameter value count", 3, receivedRequests.get(0).get(KEY2).length);
  Assert.assertEquals("parameter value", VAL1, receivedRequests.get(0).get(KEY1)[0]);
  Assert.assertEquals("parameter value", VAL2, receivedRequests.get(0).get(KEY1)[1]);
  Assert.assertEquals("parameter value", VAL1, receivedRequests.get(0).get(KEY2)[0]);
  Assert.assertEquals("parameter value", VAL2, receivedRequests.get(0).get(KEY2)[1]);
  Assert.assertEquals("parameter value", VAL3, receivedRequests.get(0).get(KEY2)[2]);
  Assert.assertNull("parameter value", receivedRequests.get(0).get("randomkey"));


  Assert.assertEquals("emitted size", 1, sink.collectedTuples.size());
  MultivaluedMapImpl map = new MultivaluedMapImpl();
  map.add(pojo.getName1(), pojo.getValue11());
  map.add(pojo.getName1(), pojo.getValue12());
  map.add(pojo.getName2(), pojo.getValue21());
  map.add(pojo.getName2(), pojo.getValue22());
  map.add(pojo.getName2(), pojo.getValue23());
  Map.Entry<String,List<String>> entry = map.entrySet().iterator().next();
  Assert.assertEquals("emitted tuples", entry.getKey(), sink.collectedTuples.get(0).trim());
}
 
Example 12
Source File: Rest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
MultivaluedMapImpl createMultivalueMap( String paramName, String paramValue ) {
  MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
  queryParams.add( paramName, UriComponent.encode( paramValue, UriComponent.Type.QUERY_PARAM ) );
  return queryParams;
}