Example usage for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getFirstHeader.

Prototype

Header getFirstHeader(String str);

Source Link

Usage

From source file:org.apache.activemq.artemis.tests.integration.rest.util.RestMessageContext.java

public int postMessage(String content, String type) throws IOException {
    String postUri;//from w  w w  .  java2 s . c  om
    String nextMsgUri = contextMap.get(KEY_MSG_CREATE_NEXT);
    if (nextMsgUri == null) {
        postUri = contextMap.get(KEY_MSG_CREATE);
    } else {
        postUri = nextMsgUri;
    }
    CloseableHttpResponse response = connection.post(postUri, type, content);
    int code = -1;
    try {
        code = ResponseUtil.getHttpCode(response);
        // check redirection
        if (code == 307) {
            Header redirLoc = response.getFirstHeader("Location");
            contextMap.put(KEY_MSG_CREATE_NEXT, redirLoc.getValue());
            code = postMessage(content, type);// do it again.
        } else if (code == 201) {
            Header header = response.getFirstHeader(KEY_MSG_CREATE_NEXT);
            contextMap.put(KEY_MSG_CREATE_NEXT, header.getValue());
        }
    } finally {
        response.close();
    }
    return code;
}

From source file:org.apache.activemq.artemis.tests.integration.rest.util.RestMessageContext.java

public boolean acknowledgement(boolean ackValue) throws IOException {
    String ackUri = contextMap.get(KEY_MSG_ACK);
    if (ackUri != null) {
        CloseableHttpResponse response = connection.post(ackUri, "application/x-www-form-urlencoded",
                "acknowledge=" + ackValue);
        int code = ResponseUtil.getHttpCode(response);
        if (code == 200) {
            contextMap.put(KEY_MSG_ACK_NEXT, response.getFirstHeader(KEY_MSG_ACK_NEXT).getValue());
        }//from   www  .  j a v  a2  s. co m
        return true;
    }
    return false;
}

From source file:com.spectralogic.ds3client.networking.NetworkClientImpl.java

@Override
public WebResponse getResponse(final Ds3Request request) throws IOException {
    try (final RequestExecutor requestExecutor = new RequestExecutor(this.client, host, request)) {
        int redirectCount = 0;
        do {/*  w ww .j  a va 2s.c om*/
            final CloseableHttpResponse response = requestExecutor.execute();
            String requestId = "Unknown";
            if (response.containsHeader(REQUEST_ID_HEADER)) {
                requestId = response.getFirstHeader(REQUEST_ID_HEADER).getValue();
            }
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {
                redirectCount++;
                LOG.info("Performing retry - attempt: {} for request #{}", redirectCount, requestId);
                response.close();
            } else {
                LOG.info("Server responded with {} for request #{}", response.getStatusLine().getStatusCode(),
                        requestId);
                return new WebResponseImpl(response);
            }
        } while (redirectCount < this.connectionDetails.getRetries());

        throw new TooManyRedirectsException(redirectCount);
    }
}

From source file:ch.ralscha.extdirectspring_itest.SimpleServiceTest.java

@Test
@PerfTest(invocations = 150, threads = 5)
public void testPoll() throws IOException {
    String _id = String.valueOf(id.incrementAndGet());
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {/* w ww .  j  av  a 2s  .  c o m*/

        HttpGet get = new HttpGet("http://localhost:9998/controller/poll/simpleService/poll/poll?id=" + _id);
        response = client.execute(get);

        assertThat(response.getFirstHeader("Content-Type").getValue())
                .isEqualTo("application/json;charset=UTF-8");

        String responseString = EntityUtils.toString(response.getEntity());
        Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
        assertThat(rootAsMap).hasSize(3);
        assertThat(rootAsMap.get("type")).isEqualTo("event");
        assertThat(rootAsMap.get("name")).isEqualTo("poll");
        assertThat(rootAsMap.get("data")).isEqualTo(_id);
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}

From source file:com.supermap.desktop.icloud.online.AuthenticatorImpl.java

/**
 * ?location:location?jsessionid,execution,_eventId,lt?
 * post//from  w  w w  .j  a v  a  2s .  c om
 *
 * @param client
 * @param token
 * @param uri
 * @param content
 * @return
 * @throws IOException
 * @throws AuthenticationException
 */
private String sendUsernamePassword(CloseableHttpClient client, UsernamePassword token, String uri,
        RequestContent content) throws IOException, AuthenticationException {
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> formparams = new ArrayList();
    formparams.add(new BasicNameValuePair("username", token.username));
    formparams.add(new BasicNameValuePair("password", token.password));
    formparams.add(new BasicNameValuePair("lt", content.lt));
    formparams.add(new BasicNameValuePair("execution", content.execution));
    formparams.add(new BasicNameValuePair("_eventId", content._eventId));
    UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, StandardCharsets.UTF_8);
    post.setHeader("Cookie", content.jsessionid);
    post.setEntity(uefEntity);
    CloseableHttpResponse response = client.execute(post);
    try {
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            return null;
        }
        if (code == 302) {
            Header header = response.getFirstHeader("Location");
            if (header == null) {
                throw new AuthenticationException(
                        "unexpected response:code 302 without Location response header");
            }
            return header.getValue();
        }
        String entity = getEntityText(response);
        throw new AuthenticationException("login failed. code:" + code + ";entity:" + entity);
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:com.networknt.traceability.TraceabilityHandlerTest.java

@Test
public void testGetWithoutTid() throws Exception {
    String url = "http://localhost:8080/get";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {/*  w  w  w  . ja va2  s.c  o  m*/
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("get", s);
            Header tid = response.getFirstHeader(Constants.TRACEABILITY_ID);
            Assert.assertNull(tid);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.networknt.traceability.TraceabilityHandlerTest.java

@Test
public void testGetWithTid() throws Exception {
    String url = "http://localhost:8080/get";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader(Constants.TRACEABILITY_ID, "12345");
    try {/*  w w w  .  j  a  va2 s . c  om*/
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("get", s);
            Header tid = response.getFirstHeader(Constants.TRACEABILITY_ID);
            Assert.assertEquals("12345", tid.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.springframework.boot.cli.command.init.InitializrService.java

private ProjectGenerationResponse createResponse(CloseableHttpResponse httpResponse, HttpEntity httpEntity)
        throws IOException {
    ProjectGenerationResponse response = new ProjectGenerationResponse(ContentType.getOrDefault(httpEntity));
    response.setContent(FileCopyUtils.copyToByteArray(httpEntity.getContent()));
    String fileName = extractFileName(httpResponse.getFirstHeader("Content-Disposition"));
    if (fileName != null) {
        response.setFileName(fileName);//from   ww w.j  av  a 2  s  .  co  m
    }
    return response;
}

From source file:ch.ralscha.extdirectspring_itest.InfoServiceTest.java

@Test
public void testApi() throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {/* w  ww  . ja  v  a  2 s.c  o  m*/
        HttpGet g = new HttpGet("http://localhost:9998/controller/api.js?group=itest_info_service");
        response = client.execute(g);
        String responseString = EntityUtils.toString(response.getEntity());
        String contentType = response.getFirstHeader("Content-Type").getValue();
        ApiControllerTest.compare(responseString, contentType, api(), ApiRequestParams.builder().build());
        SimpleServiceTest.assertCacheHeaders(response, false);
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}