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:ch.ralscha.extdirectspring_itest.MyModelControlerTest.java

@Test
public void testApiFingerprinted() throws IOException {
    HttpGet g = new HttpGet("http://localhost:9998/controller/api-1.1.1.js?group=itest_base");
    CloseableHttpResponse response = this.client.execute(g);
    try {/*from w  ww  . ja  va  2  s.  c o m*/
        String responseString = EntityUtils.toString(response.getEntity());
        String contentType = response.getFirstHeader("Content-Type").getValue();
        ApiControllerTest.compare(responseString, contentType, api(), ApiRequestParams.builder().build());
        SimpleServiceTest.assertCacheHeaders(response, true);
    } finally {
        IOUtils.closeQuietly(response);
    }
}

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

@Test
public void testApi() throws IOException {
    HttpGet g = new HttpGet("http://localhost:9998/controller/api.js?group=itest_base_service");
    CloseableHttpResponse response = this.client.execute(g);
    try {/*  w w w.  j av a  2 s  . c om*/
        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);
    }
}

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

@Test
public void testApiDebug() throws IOException {
    HttpGet g = new HttpGet("http://localhost:9998/controller/api-debug.js?group=itest_base_service");
    CloseableHttpResponse response = this.client.execute(g);
    try {//from  www. j a v  a2 s.  c  o  m
        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);
    }
}

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

@Test
public void testApiFingerprinted() throws IOException {
    HttpGet g = new HttpGet("http://localhost:9998/controller/api-1.1.1.js?group=itest_base_service");
    CloseableHttpResponse response = this.client.execute(g);
    try {/* w  w w  .j av a 2 s.co  m*/
        String responseString = EntityUtils.toString(response.getEntity());
        String contentType = response.getFirstHeader("Content-Type").getValue();
        ApiControllerTest.compare(responseString, contentType, api(), ApiRequestParams.builder().build());
        SimpleServiceTest.assertCacheHeaders(response, true);
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:com.erudika.para.security.LinkedInAuthFilter.java

/**
 * Calls the LinkedIn API to get the user profile using a given access token.
 * @param appid app identifier of the parent app, use null for root app
 * @param accessToken access token//w  w w .j  ava 2s  .  c o m
 * @return {@link UserAuthentication} object or null if something went wrong
 * @throws IOException ex
 */
public UserAuthentication getOrCreateUser(String appid, String accessToken) throws IOException {
    UserAuthentication userAuth = null;
    if (accessToken != null) {
        User user = new User();
        user.setAppid(appid);
        HttpGet profileGet = new HttpGet(PROFILE_URL + accessToken);
        CloseableHttpResponse resp2 = httpclient.execute(profileGet);
        HttpEntity respEntity = resp2.getEntity();
        String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();

        if (respEntity != null && Utils.isJsonType(ctype)) {
            Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());

            if (profile != null && profile.containsKey("id")) {
                String linkedInID = (String) profile.get("id");
                String email = (String) profile.get("emailAddress");
                String pic = (String) profile.get("pictureUrl");
                String fName = (String) profile.get("firstName");
                String lName = (String) profile.get("lastName");
                String name = fName + " " + lName;

                user.setIdentifier(Config.LINKEDIN_PREFIX.concat(linkedInID));
                user = User.readUserForIdentifier(user);
                if (user == null) {
                    //user is new
                    user = new User();
                    user.setActive(true);
                    user.setEmail(StringUtils.isBlank(email) ? linkedInID + "@linkedin.com" : email);
                    user.setName(StringUtils.isBlank(name) ? "No Name" : name);
                    user.setPassword(new UUID().toString());
                    user.setPicture(pic);
                    user.setIdentifier(Config.LINKEDIN_PREFIX.concat(linkedInID));
                    String id = user.create();
                    if (id == null) {
                        throw new AuthenticationServiceException(
                                "Authentication failed: cannot create new user.");
                    }
                } else {
                    if (!StringUtils.equals(user.getPicture(), pic)) {
                        user.setPicture(pic);
                        user.update();
                    }
                }
                userAuth = new UserAuthentication(new AuthenticatedUserDetails(user));
            }
            EntityUtils.consumeQuietly(resp2.getEntity());
        }
    }
    return userAuth;
}

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

protected void mockHttpHeader(CloseableHttpResponse response, String headerName, String value) {
    Header header = value != null ? new BasicHeader(headerName, value) : null;
    given(response.getFirstHeader(headerName)).willReturn(header);
}

From source file:org.jboss.additional.testsuite.jdkall.present.web.servlet.headers.CookieHeaderServletTestCase.java

@Test
@Ignore/*from w  ww .ja  v  a 2  s  . c  o m*/
@OperateOnDeployment(DEPLOYMENT2)
public void cookieHeaderCommaSeparatorTest(@ArquillianResource URL url2) throws Exception {
    URL testURL = new URL(url2.toString() + "cookieHeaderServlet2");

    CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpGet request = new HttpGet(testURL.toString());

    CloseableHttpResponse response = null;
    response = httpClient.execute(request);
    response = httpClient.execute(request);
    Assert.assertTrue("The cookie length should be 2.",
            response.getFirstHeader("cookies.length").getValue().compareTo("2") == 0);
    Assert.assertTrue("The cookie value1 should be example_cookie.",
            response.getFirstHeader("cookies.value1").getValue().compareTo("example_cookie") == 0);
    Assert.assertTrue("The cookie value2 should be example2_cookie.",
            response.getFirstHeader("cookies.value2").getValue().compareTo("example2_cookie") == 0);
    IOUtils.closeQuietly(response);
    httpClient.close();

}

From source file:piecework.content.concrete.RemoteResource.java

private synchronized void addDetails(CloseableHttpResponse response) {
    Header contentTypeHeader = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
    if (contentTypeHeader != null)
        this.contentType = contentTypeHeader.getValue();
    Header contentLengthHeader = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    if (contentLengthHeader != null) {
        try {//www . ja  v  a  2 s. c  o m
            this.contentLength = StringUtils.isNotEmpty(contentLengthHeader.getValue())
                    ? Long.valueOf(contentLengthHeader.getValue())
                    : -1;
        } catch (NumberFormatException nfe) {
            LOG.warn("Unable to format content length from " + contentLengthHeader.getValue() + " for "
                    + uri.toString());
        }
    }
    Header lastModifiedHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
    if (lastModifiedHeader != null) {
        Date lastModifiedDate = StringUtils.isNotEmpty(lastModifiedHeader.getValue())
                ? DateUtils.parseDate(lastModifiedHeader.getValue())
                : null;
        if (lastModifiedDate != null)
            this.lastModified = lastModifiedDate.getTime();
    }
    Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
    if (eTagHeader != null)
        this.eTag = StringUtils.isNotEmpty(eTagHeader.getValue()) ? eTagHeader.getValue() : null;

    this.initialized = true;
}