Search in sources :

Example 81 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project sling by apache.

the class CheckRootIT method testHttpRoot.

@Test
public void testHttpRoot() throws Exception {
    final HttpUriRequest get = new HttpGet(TestSuiteLauncherIT.crankstartSetup.getBaseUrl());
    HttpResponse response = null;
    long timeout = System.currentTimeMillis() + 60000L;
    boolean found = false;
    try {
        while (System.currentTimeMillis() < timeout) {
            try {
                response = client.execute(get);
                if (response.getStatusLine().getStatusCode() == 404) {
                    found = true;
                    break;
                }
            } catch (HttpHostConnectException e) {
                Thread.sleep(1000);
            }
        }
        if (!found) {
            Assert.fail("Expected to get 404 from " + get.getURI());
        }
    } finally {
        Models.closeConnection(response);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 82 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project sling by apache.

the class BasicLauncherIT method testJUnitServlet.

@Test
@Retry(timeoutMsec = U.LONG_TIMEOUT_MSEC, intervalMsec = U.STD_INTERVAL)
public void testJUnitServlet() throws Exception {
    final String path = "/system/sling/junit";
    final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
    HttpResponse response = null;
    try {
        response = client.execute(get);
        assertEquals("Expecting JUnit servlet to be installed via sling extension command, at " + get.getURI(), 200, response.getStatusLine().getStatusCode());
    } finally {
        U.closeConnection(response);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) Retry(org.apache.sling.commons.testing.junit.Retry)

Example 83 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project sling by apache.

the class BasicLauncherIT method testHttpRoot.

@Test
@Retry(timeoutMsec = U.LONG_TIMEOUT_MSEC, intervalMsec = U.STD_INTERVAL)
public void testHttpRoot() throws Exception {
    final HttpUriRequest get = new HttpGet(C.getBaseUrl());
    HttpResponse response = null;
    try {
        response = client.execute(get);
        assertEquals("Expecting page not found at " + get.getURI(), 404, response.getStatusLine().getStatusCode());
    } finally {
        U.closeConnection(response);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) Retry(org.apache.sling.commons.testing.junit.Retry)

Example 84 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project opennms by OpenNMS.

the class MapquestGeocoder method geocode.

/**
 * {@inheritDoc}
 */
@Override
public GWTLatLng geocode(final String geolocation) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(geolocation));
    method.addHeader("User-Agent", "OpenNMS-MapQuestGeocoder/1.0");
    method.addHeader("Referer", m_referer);
    try {
        InputStream responseStream = m_httpClient.execute(method).getEntity().getContent();
        final ElementTree tree = ElementTree.fromStream(responseStream);
        if (tree == null) {
            throw new GeocoderException("an error occurred connecting to the MapQuest geocoding service (no XML tree was found)");
        }
        final ElementTree statusCode = tree.find("//statusCode");
        if (statusCode == null || !statusCode.getText().equals("0")) {
            final String code = (statusCode == null ? "unknown" : statusCode.getText());
            final ElementTree messageTree = tree.find("//message");
            final String message = (messageTree == null ? "unknown" : messageTree.getText());
            throw new GeocoderException("an error occurred when querying MapQuest (statusCode=" + code + ", message=" + message + ")");
        }
        final List<ElementTree> locations = tree.findAll("//location");
        if (locations.size() > 1) {
            LOG.warn("more than one location returned for query: {}", geolocation);
        } else if (locations.size() == 0) {
            throw new GeocoderException("MapQuest returned an OK status code, but no locations");
        }
        final ElementTree location = locations.get(0);
        // first, check the quality
        if (m_minimumQuality != null) {
            final Quality geocodeQuality = Quality.valueOf(location.find("//geocodeQuality").getText().toUpperCase());
            if (geocodeQuality.compareTo(m_minimumQuality) < 0) {
                throw new GeocoderException("response did not meet minimum quality requirement (" + geocodeQuality + " is less specific than " + m_minimumQuality + ")");
            }
        }
        // then, extract the lat/lng
        final ElementTree latLng = location.find("//latLng");
        Double latitude = Double.valueOf(latLng.find("//lat").getText());
        Double longitude = Double.valueOf(latLng.find("//lng").getText());
        return new GWTLatLng(latitude, longitude);
    } catch (GeocoderException e) {
        throw e;
    } catch (Throwable e) {
        throw new GeocoderException("unable to get lat/lng from MapQuest", e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ElementTree(net.simon04.jelementtree.ElementTree)

Example 85 with HttpUriRequest

use of org.apache.http.client.methods.HttpUriRequest in project opennms by OpenNMS.

the class NominatimGeocoder method geocode.

/**
 * {@inheritDoc}
 */
@Override
public GWTLatLng geocode(final String geolocation) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(geolocation));
    method.addHeader("User-Agent", "OpenNMS-MapquestGeocoder/1.0");
    if (m_referer != null) {
        method.addHeader("Referer", m_referer);
    }
    CloseableHttpResponse response = null;
    try {
        response = m_clientWrapper.execute(method);
        InputStream 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) {
            LOG.warn("more than one location returned for query: {}", geolocation);
        } else if (places.size() == 0) {
            throw new GeocoderException("Nominatim returned an OK status code, but no places");
        }
        final ElementTree place = places.get(0);
        Double latitude = Double.valueOf(place.getAttribute("lat"));
        Double longitude = Double.valueOf(place.getAttribute("lon"));
        return new GWTLatLng(latitude, longitude);
    } catch (GeocoderException e) {
        throw e;
    } catch (Throwable e) {
        throw new GeocoderException("unable to get lat/lng from Nominatim", e);
    } finally {
        m_clientWrapper.close(response);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ElementTree(net.simon04.jelementtree.ElementTree)

Aggregations

HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)185 Test (org.junit.Test)62 TestRequest (com.android.volley.mock.TestRequest)52 HttpGet (org.apache.http.client.methods.HttpGet)45 URI (java.net.URI)43 HttpResponse (org.apache.http.HttpResponse)41 HttpEntity (org.apache.http.HttpEntity)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)36 HttpPost (org.apache.http.client.methods.HttpPost)22 IOException (java.io.IOException)19 Header (org.apache.http.Header)18 JSONObject (org.json.JSONObject)18 BufferedReader (java.io.BufferedReader)17 InputStreamReader (java.io.InputStreamReader)17 PrintWriter (java.io.PrintWriter)17 StringWriter (java.io.StringWriter)17 HttpHost (org.apache.http.HttpHost)13 HttpPut (org.apache.http.client.methods.HttpPut)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 StringEntity (org.apache.http.entity.StringEntity)10