Example usage for org.springframework.http HttpHeaders getFirst

List of usage examples for org.springframework.http HttpHeaders getFirst

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders getFirst.

Prototype

@Override
@Nullable
public String getFirst(String headerName) 

Source Link

Document

Return the first header value for the given header name, if any.

Usage

From source file:org.zalando.riptide.Actions.java

/**
 * Normalizes the {@code Location} and {@code Content-Location} headers of any given response by resolving them
 * against the given {@code uri}.// w w  w  . j a v a 2 s . c  o m
 *
 * @param uri the base uri to resolve against
 * @return a function that normalizes responses
 */
public static ThrowingFunction<ClientHttpResponse, ClientHttpResponse, IOException> normalize(final URI uri) {
    return response -> {
        final HttpHeaders headers = new HttpHeaders();
        headers.putAll(response.getHeaders());

        Optional.ofNullable(headers.getLocation()).map(uri::resolve).ifPresent(headers::setLocation);

        Optional.ofNullable(headers.getFirst(CONTENT_LOCATION)).map(uri::resolve)
                .ifPresent(location -> headers.set(CONTENT_LOCATION, location.toASCIIString()));

        return new ForwardingClientHttpResponse() {

            @Override
            protected ClientHttpResponse delegate() {
                return response;
            }

            @Override
            public HttpHeaders getHeaders() {
                return headers;
            }

        };
    };
}

From source file:com.cisco.cta.taxii.adapter.httpclient.HasHeaderMatcher.java

@Override
protected String featureValueOf(HttpHeaders headers) {
    return headers.getFirst(name);
}

From source file:com.orange.cepheus.broker.ConfigurationTest.java

@Test
public void getHeadersForBrokerWithNullParameters() {
    configuration.setRemoteServiceName(null);
    configuration.setRemoteServicePath(null);
    configuration.setRemoteAuthToken(null);

    HttpHeaders httpHeaders = new HttpHeaders();
    configuration.addRemoteHeaders(httpHeaders);

    assertNull(httpHeaders.getFirst("Fiware-Service"));
    assertNull(httpHeaders.getFirst("Fiware-ServicePath"));
    assertNull(httpHeaders.getFirst("X-Auth-Token"));
}

From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java

@Test
public void test01_01_fileDownload() throws IOException {
    ResponseEntity<byte[]> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_1",
            byte[].class);
    ClassPathResource images = new ClassPathResource("/image/Duke.png");

    byte[] expected = StreamUtils.copyToByteArray(images.getInputStream());

    HttpHeaders headers = response.getHeaders();
    System.out.println("test01_01_fileDownload: X-Track=" + headers.getFirst("X-Track"));
    assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=Duke.png"));

    MediaType contentType = headers.getContentType();
    assertThat(contentType.getType(), is("image"));
    assertThat(contentType.getSubtype(), is("png"));

    assertThat(response.getBody(), is(expected));
}

From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java

@Test
public void test01_02_fileDownload() {
    ResponseEntity<String> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_2",
            String.class);

    HttpHeaders headers = response.getHeaders();
    System.out.println("test01_02_fileDownload: X-Track=" + headers.getFirst("X-Track"));

    assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=framework.txt"));

    MediaType contentType = headers.getContentType();
    assertThat(contentType.getType(), is("text"));
    assertThat(contentType.getSubtype(), is("plain"));
    assertThat(contentType.getParameter("charset"), equalToIgnoringCase("UTF-8"));

    assertThat(response.getBody(), is("Spring Framework"));

}

From source file:com.orange.cepheus.broker.ConfigurationTest.java

@Test
public void getHeadersForBroker() {
    configuration.setRemoteServiceName("SERVICE");
    configuration.setRemoteServicePath("PATH");
    configuration.setRemoteAuthToken("TOKEN");
    configuration.setRemoteForwardUpdateContext(false);

    HttpHeaders httpHeaders = new HttpHeaders();
    configuration.addRemoteHeaders(httpHeaders);

    assertEquals("SERVICE", httpHeaders.getFirst("Fiware-Service"));
    assertEquals("PATH", httpHeaders.getFirst("Fiware-ServicePath"));
    assertEquals("TOKEN", httpHeaders.getFirst("X-Auth-Token"));
}

From source file:com.applechip.android.showcase.rest.HttpGetGzipCompressedActivity.java

private void refreshResults(ResponseEntity<String> response) {
    if (response == null) {
        return;//from w w  w. j  a v  a 2 s.  c o  m
    }

    HttpHeaders headers = response.getHeaders();
    StringBuilder sb = new StringBuilder();
    sb.append("Date: ").append(headers.getFirst("Date")).append("\n");
    sb.append("Status: ").append(headers.getFirst("Status")).append("\n");
    sb.append("Content-Type: ").append(headers.getFirst("Content-Type")).append("\n");
    sb.append("Content-Encoding: ").append(headers.getFirst("Content-Encoding")).append("\n");
    sb.append("Content-Length: ").append(headers.getFirst("Content-Length")).append("\n");

    TextView textView = (TextView) findViewById(R.id.text_view_headers);
    textView.setText(sb.toString());

    String results = response.getBody() + "\n";

    textView = (TextView) findViewById(R.id.text_view_results);
    textView.setText(results);
}

From source file:com.applechip.android.showcase.rest.HttpGetGzipCompressedJsonActivity.java

private void refreshResults(ResponseEntity<TwitterSearchResults> response) {
    if (response == null) {
        return;/*from w ww. java 2s  .c om*/
    }

    HttpHeaders headers = response.getHeaders();
    StringBuilder sb = new StringBuilder();
    sb.append("Date: ").append(headers.getFirst("Date")).append("\n");
    sb.append("Status: ").append(headers.getFirst("Status")).append("\n");
    sb.append("Content-Type: ").append(headers.getFirst("Content-Type")).append("\n");
    sb.append("Content-Encoding: ").append(headers.getFirst("Content-Encoding")).append("\n");
    sb.append("Content-Length: ").append(headers.getFirst("Content-Length")).append("\n");

    TextView textView = (TextView) findViewById(R.id.text_view_headers);
    textView.setText(sb.toString());

    TwitterSearchResults results = response.getBody();

    ListView listView = (ListView) findViewById(R.id.list_view_result_items);
    listView.setAdapter(new TweetListAdapter(this, results.getResults()));
}

From source file:org.terasoluna.gfw.functionaltest.app.logging.LoggingTest.java

@Test
public void test01_04_checkConsistencyXtrackMDCRequestToResponse() {
    // test Check consistency HTTP Request Header to Response Header
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("X-Track", "12345678901234567890123456789012");
    ResponseEntity<byte[]> response = restTemplate.exchange(
            applicationContextUrl + "/logging/xTrackMDCPutFilter/1_4", HttpMethod.GET,
            new HttpEntity<byte[]>(requestHeaders), byte[].class);

    HttpHeaders headers = response.getHeaders();
    assertThat(headers.getFirst("X-Track"), is("12345678901234567890123456789012"));
}

From source file:com.orange.ngsi.client.UpdateContextRequestTest.java

@Test
public void performPostWith200() throws Exception {

    protocolRegistry.unregisterHost(brokerUrl);

    HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(brokerUrl);
    Assert.assertEquals("application/json", httpHeaders.getFirst("Content-Type"));
    Assert.assertEquals("application/json", httpHeaders.getFirst("Accept"));

    httpHeaders.add("Fiware-Service", serviceName);
    httpHeaders.add("Fiware-ServicePath", servicePath);

    String responseBody = json(jsonConverter, createUpdateContextResponseTempSensor());

    this.mockServer.expect(requestTo(brokerUrl + "/ngsi10/updateContext")).andExpect(method(HttpMethod.POST))
            .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE))
            .andExpect(header("Accept", MediaType.APPLICATION_JSON_VALUE))
            .andExpect(header("Fiware-Service", serviceName))
            .andExpect(header("Fiware-ServicePath", servicePath))
            .andExpect(jsonPath("$.updateAction").value(UpdateAction.UPDATE.getLabel()))
            .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

    ngsiClient.updateContext(brokerUrl, httpHeaders, createUpdateContextTempSensor(0)).get();

    this.mockServer.verify();
}