Example usage for org.apache.http.client.methods HttpUriRequest addHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest addHeader

Introduction

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

Prototype

void addHeader(String str, String str2);

Source Link

Usage

From source file:org.opennms.features.geocoder.nominatim.NominatimGeocoderService.java

@Override
public Coordinates getCoordinates(final String address) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(address));
    method.addHeader("User-Agent", "OpenNMS-NominatimGeocoderService/1.0");
    if (m_referer != null && !"".equals(m_referer)) {
        method.addHeader("Referer", m_referer);
    }/*w  w w .  jav a 2  s  .co  m*/

    InputStream responseStream = null;
    CloseableHttpResponse response = null;
    try {
        response = m_clientWrapper.execute(method);
        final StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new GeocoderException("Nominatim returned a non-OK response code: "
                    + statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        responseStream = response.getEntity().getContent();
        final ElementTree tree = ElementTree.fromStream(responseStream);
        if (tree == null) {
            throw new GeocoderException(
                    "an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
        }

        final List<ElementTree> places = tree.findAll("//place");
        if (places.size() > 1) {
            m_log.warn("More than one location returned for query: {}", address);
        } else if (places.size() == 0) {
            throw new GeocoderException("Nominatim returned an OK status code, but no places");
        }
        final ElementTree place = places.get(0);

        final Float longitude = Float.valueOf(place.getAttribute("lon"));
        final Float latitude = Float.valueOf(place.getAttribute("lat"));
        return new Coordinates(longitude, latitude);
    } catch (final GeocoderException e) {
        throw e;
    } catch (final Throwable e) {
        throw new GeocoderException("unable to get lon/lat from Nominatim", e);
    } finally {
        IOUtils.closeQuietly(responseStream);
        m_clientWrapper.close(response);
    }
}

From source file:org.wso2.am.integration.tests.header.CORSHeadersTestCase.java

@Test(groups = { "wso2.am" }, description = "Checking CORS headers in pre-flight response")
public void CheckCORSHeadersInPreFlightResponse() throws Exception {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpUriRequest option = new HttpOptions(getAPIInvocationURLHttp(API_CONTEXT, API_VERSION));
    option.addHeader("Origin", "http://localhost");
    HttpResponse response = httpclient.execute(option);

    assertEquals(response.getStatusLine().getStatusCode(), HTTP_RESPONSE_CODE_OK, "Response code mismatch.");

    Header[] responseHeaders = response.getAllHeaders();

    log.info("Response Headers: CheckCORSHeadersInPreFlightResponse");
    for (Header header : responseHeaders) {
        log.info(header.getName() + " : " + header.getValue());
    }/*from w w  w .j  a v a2 s  .  c  o m*/

    Header header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_ORIGIN_HEADER);
    assertNotNull(header, ACCESS_CONTROL_ALLOW_ORIGIN_HEADER + " header is not available in the response.");
    assertEquals(header.getValue(), ACCESS_CONTROL_ALLOW_ORIGIN_HEADER_VALUE,
            ACCESS_CONTROL_ALLOW_ORIGIN_HEADER + " header value mismatch.");

    header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_METHODS_HEADER);
    assertNotNull(header, ACCESS_CONTROL_ALLOW_METHODS_HEADER + " header is not available in the response.");
    assertEquals(header.getValue(), ACCESS_CONTROL_ALLOW_METHODS_HEADER_VALUE,
            ACCESS_CONTROL_ALLOW_METHODS_HEADER + " header value mismatch.");

    header = pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_HEADERS_HEADER);
    assertNotNull(header, ACCESS_CONTROL_ALLOW_HEADERS_HEADER + " header is not available in the response.");
    assertEquals(header.getValue(), ACCESS_CONTROL_ALLOW_HEADERS_HEADER_VALUE,
            ACCESS_CONTROL_ALLOW_HEADERS_HEADER + " header value mismatch.");

    assertNull(pickHeader(responseHeaders, ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER),
            ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER + " header is available in the response, "
                    + "but it should not be.");
}