Example usage for org.apache.http.client.fluent Request Post

List of usage examples for org.apache.http.client.fluent Request Post

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request Post.

Prototype

public static Request Post(final String uri) 

Source Link

Usage

From source file:com.enitalk.configs.BotConfig.java

@Bean
@Primary//from   w  w  w.ja v  a2  s  . c  o m
public LoadingCache<String, String> tokenCache() {
    LoadingCache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS)
            .build(new CacheLoader<String, String>() {

                @Override
                public String load(String key) throws Exception {
                    String id = jackson.createObjectNode().put("login", env.getProperty("bot.login"))
                            .put("password", env.getProperty("bot.pass")).toString();
                    byte[] auth = Request.Post(env.getProperty("bot.auth"))
                            .bodyString(id, ContentType.APPLICATION_JSON).execute().returnContent().asBytes();
                    JsonNode tree = jackson.readTree(auth);
                    logger.info("Bot token came {}", tree);
                    String authToken = tree.path("token").asText();
                    return authToken;
                }

            });
    return cache;
}

From source file:com.enitalk.controllers.youtube.BotAware.java

public void sendTag(ObjectNode dest) throws IOException, ExecutionException {
    String auth = botAuth();/*from  w  ww  .  j  a  va  2  s .  co  m*/
    String tagResponse = Request.Post(env.getProperty("bot.sendTag"))
            .addHeader("Authorization", "Bearer " + auth)
            .bodyString(dest.toString(), ContentType.APPLICATION_JSON).socketTimeout(20000).connectTimeout(5000)
            .execute().returnContent().asString();

    logger.info("Tag command sent to a bot {}, response {}", dest, tagResponse);
}

From source file:com.github.aistomin.http.PostRequest.java

@Override
public String execute() throws Exception {
    final Request request = Request.Post(this.resource);
    for (final Map.Entry<String, String> item : this.heads.entrySet()) {
        request.addHeader(item.getKey(), item.getValue());
    }//from   w w w  .jav a 2  s . com
    final HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setRedirectStrategy(new LaxRedirectStrategy());
    builder.setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
        public boolean retryRequest(final HttpResponse response, final int count, final HttpContext context) {
            return count <= PostRequest.RETRY_COUNT;
        }

        public long getRetryInterval() {
            return PostRequest.RETRY_INTERVAL;
        }
    });
    return Executor.newInstance(builder.build()).execute(request).returnContent().asString();
}

From source file:org.mule.module.http.functional.listener.HttpListenerContentTypeTestCase.java

@Test
public void rejectsInvalidContentTypeWithoutBody() throws Exception {
    Request request = Request.Post(getUrl()).addHeader(CONTENT_TYPE, "application");
    testRejectContentType(request, "Invalid Content-Type");
}

From source file:io.gravitee.gateway.standalone.PostContentGatewayTest.java

@Test
public void call_post_content() throws Exception {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("case1/request_content.json");
    String content = StringUtils.copy(is);

    Request request = Request.Post("http://localhost:8082/test/my_team").bodyString(content,
            ContentType.APPLICATION_JSON);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(content, responseContent);
}

From source file:biz.dfch.activiti.wrapper.service.ActivitiService.java

public void invokeProcess(ProcessMetadata processMetadata) {

    LOG.info("Sending request to start process at activity server '" + activitiUri + "'");

    try {//from  w w w.  j a  va 2s.c  o m
        String response = Request.Post(getRequestUri()).setHeader("Authorization", createBasicAuth())
                .bodyString(objectMapper.writeValueAsString(createBody(processMetadata)),
                        ContentType.APPLICATION_JSON)
                .execute().returnContent().asString();
        LOG.info("Got response from activiti engine: " + objectMapper.writeValueAsString(response));
    } catch (IOException e) {
        throw new ActivityException("Exception occured while sending request to Activiti", e);
    }
}

From source file:org.mule.module.http.functional.listener.HttpListenerParseRequestTestCase.java

private void sendUrlEncodedPost(String path, int port) throws IOException {
    Request.Post(getUrl(path, port)).bodyForm(new BasicNameValuePair("key", "value"))
            .connectTimeout(RECEIVE_TIMEOUT).socketTimeout(RECEIVE_TIMEOUT).execute();
}

From source file:uk.org.thegatekeeper.dw.booking.BookingServiceIT.java

@Test
public void testConcurrentBooking() throws Exception {
    JdiGateController controller = new JdiGateController(debugger);
    Gate gate = controller.stopFirstThread().inClass(PaymentService.class).beforeMethod("pay").build();

    final List<Throwable> exceptions = new ArrayList<Throwable>();
    Thread booking = new Thread(new Runnable() {
        public void run() {
            try {
                Request.Post("http://localhost:8080/booking/book/3/34").execute().returnContent().asString();
            } catch (Throwable t) {
                exceptions.add(t);//from  w  w w.  j  a  va  2 s.co m
            }
        }
    });

    booking.start();
    gate.waitFor(5, TimeUnit.SECONDS);

    String status = Request.Post("http://localhost:8080/booking/book/3/34").execute().returnContent()
            .asString();
    assertThat(status, containsString("\"success\":true"));

    gate.open();
    booking.join();

    assertEquals(1, exceptions.size());
}

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 * ???json?post?/*from   w w w.j a  v a2  s .com*/
 * 
 * @throws IOException
 * @throws
 * 
 */
public static String fetchJsonHttpResponse(String contentUrl, Map<String, String> headerMap,
        JsonObject bodyJson) throws IOException {
    Executor executor = Executor.newInstance(httpClient);
    Request request = Request.Post(contentUrl);
    if (headerMap != null && !headerMap.isEmpty()) {
        for (Map.Entry<String, String> m : headerMap.entrySet()) {
            request.addHeader(m.getKey(), m.getValue());
        }
    }
    if (bodyJson != null) {
        request.bodyString(bodyJson.toString(), ContentType.APPLICATION_JSON);
    }
    return executor.execute(request).returnContent().asString();
}

From source file:com.m3958.apps.anonymousupload.integration.java.FileUploadTest.java

@Test
public void testPostOne() throws ClientProtocolException, IOException, URISyntaxException {

    File f = new File("README.md");

    Assert.assertTrue(f.exists());//from   w  ww. j  a  v a  2s  .c om

    String c = Request.Post(new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).build())
            .body(MultipartEntityBuilder.create()
                    .addBinaryBody("afile", f, ContentType.MULTIPART_FORM_DATA, f.getName()).build())
            .execute().returnContent().asString();

    String url = c.trim();
    Assert.assertTrue(url.endsWith(".md"));

    String cc = Request.Get(url).execute().returnContent().asString();
    Assert.assertTrue(cc.contains("vertx runmod"));

    testComplete();
}