Java Code Examples for org.springframework.http.HttpHeaders#get()

The following examples show how to use org.springframework.http.HttpHeaders#get() . 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: RestTemplateInterceptor.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * add response tag
 * @param response
 * @param sofaTracerSpan
 */
private void appendRestTemplateResponseSpanTags(ClientHttpResponse response,
                                                SofaTracerSpan sofaTracerSpan) {
    if (sofaTracerSpan == null) {
        return;
    }
    HttpHeaders headers = response.getHeaders();
    //content length
    if (headers != null && headers.get("Content-Length") != null
        && !headers.get("Content-Length").isEmpty()) {
        List<String> contentLengthList = headers.get("Content-Length");
        String len = contentLengthList.get(0);
        sofaTracerSpan.setTag(CommonSpanTags.RESP_SIZE, Long.valueOf(len));
    }
    // current thread name
    sofaTracerSpan.setTag(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
}
 
Example 2
Source File: SockJsClient.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private HttpHeaders getHttpRequestHeaders(@Nullable HttpHeaders webSocketHttpHeaders) {
	if (getHttpHeaderNames() == null || webSocketHttpHeaders == null) {
		return webSocketHttpHeaders;
	}
	else {
		HttpHeaders httpHeaders = new HttpHeaders();
		for (String name : getHttpHeaderNames()) {
			List<String> values = webSocketHttpHeaders.get(name);
			if (values != null) {
				httpHeaders.put(name, values);
			}
		}
		return httpHeaders;
	}
}
 
Example 3
Source File: PaginationUtilUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 4
Source File: PaginationUtilUnitTest.java    From jhipster with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    Page<String> page = new PageImpl<>(new ArrayList<>(), PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder, page);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertThat(strHeaders).isNotNull();
    assertThat(strHeaders).hasSize(1);
    String headerData = strHeaders.get(0);
    assertThat(headerData.split(",")).hasSize(4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
        + "</api/_search/example?page=5&size=50>; rel=\"prev\","
        + "</api/_search/example?page=7&size=50>; rel=\"last\","
        + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertThat(headerData).isEqualTo(expectedData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertThat(xTotalCountHeaders).hasSize(1);
    assertThat((long) Long.valueOf(xTotalCountHeaders.get(0))).isEqualTo(400L);
}
 
Example 5
Source File: PaginationUtilUnitTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 6
Source File: PaginationUtilUnitTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 7
Source File: WebSocketConfig.java    From bearchoke with Apache License 2.0 6 votes vote down vote up
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    if (request instanceof ServletServerHttpRequest) {

        HttpHeaders headers = request.getHeaders();

        if (headers.containsKey(ServerConstants.X_AUTH_TOKEN)) {
            List<String> authToken = headers.get(ServerConstants.X_AUTH_TOKEN);

            if (log.isDebugEnabled()) {
                log.debug("Header auth token: " + authToken.get(0));
            }

            attributes.put(ServerConstants.X_AUTH_TOKEN, headers.get(ServerConstants.X_AUTH_TOKEN));
        }
    }
    return true;
}
 
Example 8
Source File: PaginationUtilUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 9
Source File: PaginationUtilUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 10
Source File: PaginationUtilUnitTest.java    From flair-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content,new PageRequest(6, 50),400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 11
Source File: ServletServerHttpRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getHeadersWithEmptyContentTypeAndEncoding() {
	String headerName = "MyHeader";
	String headerValue1 = "value1";
	String headerValue2 = "value2";
	mockRequest.addHeader(headerName, headerValue1);
	mockRequest.addHeader(headerName, headerValue2);
	mockRequest.setContentType("");
	mockRequest.setCharacterEncoding("");

	HttpHeaders headers = request.getHeaders();
	assertNotNull("No HttpHeaders returned", headers);
	assertTrue("Invalid headers returned", headers.containsKey(headerName));
	List<String> headerValues = headers.get(headerName);
	assertEquals("Invalid header values returned", 2, headerValues.size());
	assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
	assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
	assertNull(headers.getContentType());
}
 
Example 12
Source File: ServletServerHttpRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getHeaders() {
	String headerName = "MyHeader";
	String headerValue1 = "value1";
	String headerValue2 = "value2";
	mockRequest.addHeader(headerName, headerValue1);
	mockRequest.addHeader(headerName, headerValue2);
	mockRequest.setContentType("text/plain");
	mockRequest.setCharacterEncoding("UTF-8");

	HttpHeaders headers = request.getHeaders();
	assertNotNull("No HttpHeaders returned", headers);
	assertTrue("Invalid headers returned", headers.containsKey(headerName));
	List<String> headerValues = headers.get(headerName);
	assertEquals("Invalid header values returned", 2, headerValues.size());
	assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
	assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
	assertEquals("Invalid Content-Type", new MediaType("text", "plain", StandardCharsets.UTF_8),
			headers.getContentType());
}
 
Example 13
Source File: PaginationUtilUnitTest.java    From ehcache3-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 14
Source File: ServletServerHttpRequestTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getHeadersWithEmptyContentTypeAndEncoding() throws Exception {
	String headerName = "MyHeader";
	String headerValue1 = "value1";
	String headerValue2 = "value2";
	mockRequest.addHeader(headerName, headerValue1);
	mockRequest.addHeader(headerName, headerValue2);
	mockRequest.setContentType("");
	mockRequest.setCharacterEncoding("");

	HttpHeaders headers = request.getHeaders();
	assertNotNull("No HttpHeaders returned", headers);
	assertTrue("Invalid headers returned", headers.containsKey(headerName));
	List<String> headerValues = headers.get(headerName);
	assertEquals("Invalid header values returned", 2, headerValues.size());
	assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
	assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
	assertNull(headers.getContentType());
}
 
Example 15
Source File: HttpEntityMethodProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private List<String> getVaryRequestHeadersToAdd(HttpHeaders responseHeaders, HttpHeaders entityHeaders) {
	List<String> entityHeadersVary = entityHeaders.getVary();
	List<String> vary = responseHeaders.get(HttpHeaders.VARY);
	if (vary != null) {
		List<String> result = new ArrayList<>(entityHeadersVary);
		for (String header : vary) {
			for (String existing : StringUtils.tokenizeToStringArray(header, ",")) {
				if ("*".equals(existing)) {
					return Collections.emptyList();
				}
				for (String value : entityHeadersVary) {
					if (value.equalsIgnoreCase(existing)) {
						result.remove(value);
					}
				}
			}
		}
		return result;
	}
	return entityHeadersVary;
}
 
Example 16
Source File: PaginationUtilUnitTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePaginationHttpHeadersTest() {
    String baseUrl = "/api/_search/example";
    List<String> content = new ArrayList<>();
    Page<String> page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, baseUrl);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertNotNull(strHeaders);
    assertTrue(strHeaders.size() == 1);
    String headerData = strHeaders.get(0);
    assertTrue(headerData.split(",").length == 4);
    String expectedData = "</api/_search/example?page=7&size=50>; rel=\"next\","
            + "</api/_search/example?page=5&size=50>; rel=\"prev\","
            + "</api/_search/example?page=7&size=50>; rel=\"last\","
            + "</api/_search/example?page=0&size=50>; rel=\"first\"";
    assertEquals(expectedData, headerData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertTrue(xTotalCountHeaders.size() == 1);
    assertTrue(Long.valueOf(xTotalCountHeaders.get(0)).equals(400L));
}
 
Example 17
Source File: HttpUtils.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean isAjax(HttpHeaders headers) {
	List<String> values = headers.get("X-Requested-With");

	if (CollectionUtils.isEmpty(values)) {
		return false;
	}

	return values.contains("XMLHttpRequest");
}
 
Example 18
Source File: WebSessionIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private String extractSessionId(HttpHeaders headers) {
	List<String> headerValues = headers.get("Set-Cookie");
	assertNotNull(headerValues);
	assertEquals(1, headerValues.size());

	for (String s : headerValues.get(0).split(";")){
		if (s.startsWith("SESSION=")) {
			return s.substring("SESSION=".length());
		}
	}
	return null;
}
 
Example 19
Source File: WebSessionIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private String extractSessionId(HttpHeaders headers) {
	List<String> headerValues = headers.get("Set-Cookie");
	assertNotNull(headerValues);
	assertEquals(1, headerValues.size());

	for (String s : headerValues.get(0).split(";")){
		if (s.startsWith("SESSION=")) {
			return s.substring("SESSION=".length());
		}
	}
	return null;
}
 
Example 20
Source File: PaginationUtilUnitTest.java    From jhipster with Apache License 2.0 4 votes vote down vote up
@Test
public void multiplePagesTest() {
    uriBuilder.queryParam("query", "Test1, test2");
    List<String> content = new ArrayList<>();

    // Page 0
    Page<String> page = new PageImpl<>(content, PageRequest.of(0, 50), 400L);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder, page);
    List<String> strHeaders = headers.get(HttpHeaders.LINK);
    assertThat(strHeaders).isNotNull();
    assertThat(strHeaders).hasSize(1);
    String headerData = strHeaders.get(0);
    assertThat(headerData.split(",")).hasSize(3);
    String expectedData = "</api/_search/example?query=Test1%2C%20test2&page=1&size=50>; rel=\"next\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=7&size=50>; rel=\"last\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=0&size=50>; rel=\"first\"";
    assertThat(headerData).isEqualTo(expectedData);
    List<String> xTotalCountHeaders = headers.get("X-Total-Count");
    assertThat(xTotalCountHeaders).hasSize(1);
    assertThat((long) Long.valueOf(xTotalCountHeaders.get(0))).isEqualTo(400L);

    // Page 1
    uriBuilder.queryParam("page", "1");
    page = new PageImpl<>(content, PageRequest.of(1, 50), 400L);
    headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder, page);
    strHeaders = headers.get(HttpHeaders.LINK);
    assertThat(strHeaders).isNotNull();
    assertThat(strHeaders).hasSize(1);
    headerData = strHeaders.get(0);
    assertThat(headerData.split(",")).hasSize(4);
    expectedData = "</api/_search/example?query=Test1%2C%20test2&page=2&size=50>; rel=\"next\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=0&size=50>; rel=\"prev\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=7&size=50>; rel=\"last\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=0&size=50>; rel=\"first\"";
    assertThat(headerData).isEqualTo(expectedData);
    xTotalCountHeaders = headers.get("X-Total-Count");
    assertThat(xTotalCountHeaders).hasSize(1);
    assertThat((long) Long.valueOf(xTotalCountHeaders.get(0))).isEqualTo(400L);

    // Page 6
    uriBuilder.queryParam("page", "6");
    page = new PageImpl<>(content, PageRequest.of(6, 50), 400L);
    headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder, page);
    strHeaders = headers.get(HttpHeaders.LINK);
    assertThat(strHeaders).isNotNull();
    assertThat(strHeaders).hasSize(1);
    headerData = strHeaders.get(0);
    assertThat(headerData.split(",")).hasSize(4);
    expectedData = "</api/_search/example?query=Test1%2C%20test2&page=7&size=50>; rel=\"next\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=5&size=50>; rel=\"prev\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=7&size=50>; rel=\"last\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=0&size=50>; rel=\"first\"";
   assertThat(headerData).isEqualTo(expectedData);
    xTotalCountHeaders = headers.get("X-Total-Count");
    assertThat(xTotalCountHeaders).hasSize(1);
    assertThat((long) Long.valueOf(xTotalCountHeaders.get(0))).isEqualTo(400L);

    // Page 7
    uriBuilder.queryParam("page", "7");
    page = new PageImpl<>(content, PageRequest.of(7, 50), 400L);
    headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder, page);
    strHeaders = headers.get(HttpHeaders.LINK);
    assertThat(strHeaders).isNotNull();
    assertThat(strHeaders).hasSize(1);
    headerData = strHeaders.get(0);
    assertThat(headerData.split(",")).hasSize(3);
    expectedData = "</api/_search/example?query=Test1%2C%20test2&page=6&size=50>; rel=\"prev\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=7&size=50>; rel=\"last\"," +
        "</api/_search/example?query=Test1%2C%20test2&page=0&size=50>; rel=\"first\"";
    assertThat(headerData).isEqualTo(expectedData);
}