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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations