Example usage for org.springframework.http HttpEntity getHeaders

List of usage examples for org.springframework.http HttpEntity getHeaders

Introduction

In this page you can find the example usage for org.springframework.http HttpEntity getHeaders.

Prototype

public HttpHeaders getHeaders() 

Source Link

Document

Returns the headers of this entity.

Usage

From source file:org.trustedanalytics.h2oscoringengine.publisher.steps.RegisteringInApplicationBrokerStepTest.java

@Test
public void register_callToAppBrokerOccured() throws Exception {
    // given//  w ww.  j  a v a  2  s.  co m
    RegisteringInApplicationBrokerStep step = new RegisteringInApplicationBrokerStep(testAppGuid, testCfApi,
            cfRestTemplateMock);

    // when
    step.register(testCredentials, basicRestTemplateMock, testServiceName, testServiceDescription);
    ArgumentCaptor<HttpEntity> requestCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    // then
    verify(basicRestTemplateMock).exchange(eq(appBrokerEndpoint), same(HttpMethod.POST),
            requestCaptor.capture(), same(String.class));
    HttpEntity<String> request = requestCaptor.getValue();
    assertThat(request.getHeaders(), equalTo(expectedHeaders));
    String actualRequestBody = request.getBody();
    assertThat(JsonDataFetcher.getStringValue(actualRequestBody, "/app/metadata/guid"), equalTo(testAppGuid));
    assertThat(JsonDataFetcher.getStringValue(actualRequestBody, "/description"),
            equalTo(testServiceDescription));
    assertThat(JsonDataFetcher.getStringValue(actualRequestBody, "/name"), equalTo(testServiceName));

}

From source file:org.openlmis.fulfillment.service.AuthServiceTest.java

@Test
public void shouldObtainAccessToken() throws Exception {
    ResponseEntity<Object> response = mock(ResponseEntity.class);
    Map<String, String> body = ImmutableMap.of("access_token", TOKEN);

    when(restTemplate.exchange(eq(AUTHORIZATION_URI), eq(HttpMethod.POST), any(HttpEntity.class),
            eq(Object.class))).thenReturn(response);

    when(response.getBody()).thenReturn(body);

    String token = authService.obtainAccessToken();
    assertThat(token, is(equalTo(TOKEN)));

    verify(restTemplate).exchange(eq(AUTHORIZATION_URI), eq(HttpMethod.POST), entityStringCaptor.capture(),
            eq(Object.class));

    HttpEntity<String> entity = entityStringCaptor.getValue();
    assertThat(entity.getHeaders().get("Authorization"), contains("Basic dHJ1c3RlZC1jbGllbnQ6c2VjcmV0"));
}

From source file:org.openlmis.fulfillment.service.BaseCommunicationServiceTest.java

protected void assertAuthHeader(HttpEntity entity) {
    assertThat(entity.getHeaders().get(HttpHeaders.AUTHORIZATION), is(singletonList("Bearer " + TOKEN)));
}

From source file:org.opencredo.couchdb.core.CouchDbDocumentTemplate.java

private HttpEntity<?> createHttpEntity(Object document) {

    if (document instanceof HttpEntity) {
        HttpEntity<?> httpEntity = (HttpEntity<?>) document;
        Assert.isTrue(httpEntity.getHeaders().getContentType().equals(MediaType.APPLICATION_JSON),
                "HttpEntity payload with non application/json content type found.");
        return httpEntity;
    }/*from   w  w  w. java2  s.c o  m*/

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Object> httpEntity = new HttpEntity<Object>(document, httpHeaders);

    return httpEntity;
}

From source file:org.openlmis.fulfillment.service.request.RequestHelperTest.java

@Test
public void shouldCreateEntityWithNoBody() {
    String token = "token";

    HttpEntity<String> entity = RequestHelper.createEntity(null, RequestHeaders.init().setAuth(token));

    assertThat(entity.getHeaders().get(HttpHeaders.AUTHORIZATION), is(singletonList(BEARER + token)));
}

From source file:org.openlmis.fulfillment.service.request.RequestHelperTest.java

@Test
public void shouldCreateEntityWithAnAuthHeader() {
    String body = "test";
    String token = "token";

    HttpEntity<String> entity = RequestHelper.createEntity(body, RequestHeaders.init().setAuth(token));

    assertThat(entity.getHeaders().get(HttpHeaders.AUTHORIZATION), is(singletonList(BEARER + token)));
    assertThat(entity.getBody(), is(body));
}

From source file:org.apigw.util.OAuthTestHelper.java

private String getAuthorizationCode(HttpEntity<Void> result) {

    String location = result.getHeaders().getLocation().toString();
    assertTrue(location.matches("http://.*code=.+"));

    String code = null;//from w ww  . j  a v  a2 s  . c  o  m
    String state = null;
    for (StringTokenizer queryTokens = new StringTokenizer(result.getHeaders().getLocation().getQuery(),
            "&="); queryTokens.hasMoreTokens();) {
        String token = queryTokens.nextToken();
        if ("code".equals(token)) {
            if (code != null) {
                fail("shouldn't have returned more than one code.");
            }

            code = queryTokens.nextToken();
        } else if ("state".equals(token)) {
            state = queryTokens.nextToken();
        }
    }

    assertEquals("gzzFqB!!!", state);
    assertNotNull(code);
    return code;

}

From source file:com.venilnoronha.dzone.feed.web.LinksRSSController.java

@RequestMapping(value = "/rss/links", method = RequestMethod.GET)
public ModelAndView linksRssByLastModified(HttpEntity<byte[]> requestEntity, HttpServletRequest request,
        HttpServletResponse response) throws ParseException {
    HttpUtils.logUserIP("LinksRSS", request);
    String lastModified = requestEntity.getHeaders().getFirst("If-Modified-Since");
    if (lastModified == null) {
        lastModified = requestEntity.getHeaders().getFirst("If-None-Match");
    }/*from  w w  w.  j  a v  a  2  s  .  c  om*/
    PageRequest page = new PageRequest(0, feedSize, Direction.DESC, "linkDate");
    Page<Link> linkPage;
    if (lastModified != null) {
        Date lastModifiedDt = HTTP_FORMAT.parse(lastModified);
        linkPage = linksRepository.findByLinkDateBetween(lastModifiedDt, new Date(), page);
    } else {
        linkPage = linksRepository.findAll(page);
    }
    Iterator<Link> linksIter = linkPage.iterator();
    List<Link> links = new ArrayList<>();
    Date newLastModified = null;
    while (linksIter.hasNext()) {
        Link link = linksIter.next();
        Date linkDate = link.getLinkDate();
        if (newLastModified == null || newLastModified.before(linkDate)) {
            newLastModified = linkDate;
        }
        links.add(link);
    }
    if (links.isEmpty() && lastModified != null) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return null;
    } else {
        response.setHeader("ETag", HTTP_FORMAT.format(newLastModified));
        response.setHeader("Last-Modified", HTTP_FORMAT.format(newLastModified));
        ModelAndView mav = new ModelAndView();
        mav.setView(new LinksRSSView(links));
        return mav;
    }
}

From source file:com.venilnoronha.dzone.feed.web.ArticlesRSSController.java

@RequestMapping(value = "/rss/articles", method = RequestMethod.GET)
public ModelAndView articlesRssByLastModified(HttpEntity<byte[]> requestEntity, HttpServletRequest request,
        HttpServletResponse response) throws ParseException {
    HttpUtils.logUserIP("ArticlesRSS", request);
    String lastModified = requestEntity.getHeaders().getFirst("If-Modified-Since");
    if (lastModified == null) {
        lastModified = requestEntity.getHeaders().getFirst("If-None-Match");
    }//from   w w w  .j  a  v  a2s  .  com
    PageRequest page = new PageRequest(0, feedSize, Direction.DESC, "articleDate");
    Page<Article> articlePage;
    if (lastModified != null) {
        Date lastModifiedDt = HTTP_FORMAT.parse(lastModified);
        articlePage = articlesRepository.findByArticleDateBetween(lastModifiedDt, new Date(), page);
    } else {
        articlePage = articlesRepository.findAll(page);
    }
    Iterator<Article> articlesIter = articlePage.iterator();
    List<Article> articles = new ArrayList<>();
    Date newLastModified = null;
    while (articlesIter.hasNext()) {
        Article article = articlesIter.next();
        Date articleDate = article.getArticleDate();
        if (newLastModified == null || newLastModified.before(articleDate)) {
            newLastModified = articleDate;
        }
        articles.add(article);
    }
    if (articles.isEmpty() && lastModified != null) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return null;
    } else {
        response.setHeader("ETag", HTTP_FORMAT.format(newLastModified));
        response.setHeader("Last-Modified", HTTP_FORMAT.format(newLastModified));
        ModelAndView mav = new ModelAndView();
        mav.setView(new ArticlesRSSView(articles));
        return mav;
    }
}

From source file:com.appglu.impl.SyncTemplate.java

/**
 * {@inheritDoc}/*from www. j  av  a2 s  .  co m*/
 */
public void downloadChangesForTables(final List<TableVersion> tables,
        final InputStreamCallback inputStreamCallback) throws AppGluRestClientException {
    RequestCallback requestCallback = new RequestCallback() {
        public void doWithRequest(ClientHttpRequest request) throws IOException {
            HttpEntity<Object> requestEntity = new HttpEntity<Object>(new TableVersionBody(tables));

            HttpHeaders requestHeaders = requestEntity.getHeaders();
            if (!requestHeaders.isEmpty()) {
                request.getHeaders().putAll(requestHeaders);
            }

            jsonMessageConverter.write(requestEntity.getBody(), requestHeaders.getContentType(), request);
        }
    };

    ResponseExtractor<Object> responseExtractor = new ResponseExtractor<Object>() {
        public Object extractData(ClientHttpResponse response) throws IOException {
            inputStreamCallback.doWithInputStream(response.getBody());
            return null;
        }
    };

    try {
        this.restOperations.execute(CHANGES_FOR_TABLES_URL, HttpMethod.POST, requestCallback,
                responseExtractor);
    } catch (RestClientException e) {
        throw new AppGluRestClientException(e.getMessage(), e);
    }
}