use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class JUnitHttpServerTest method testWebapp.
@Test
@JUnitHttpServer(port = 9162, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") })
public void testWebapp() throws Exception {
HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/index.html");
final CloseableHttpResponse response = m_clientWrapper.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
LOG.debug("got response:\n{}", responseString);
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseString.contains("This is a webapp."));
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class ScvEnabledRestClientImpl method getResponse.
// Setup a client with pre-emptive authentication
private CloseableHttpResponse getResponse(HttpGet httpget) throws Exception {
CloseableHttpResponse response = null;
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), getCredentials());
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
response = httpclient.execute(target, httpget, localContext);
return response;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class RequestTracker method getClientWrapper.
public synchronized HttpClientWrapper getClientWrapper() {
if (m_clientWrapper == null) {
m_clientWrapper = HttpClientWrapper.create().setSocketTimeout(m_timeout).setConnectionTimeout(m_timeout).setRetries(m_retries).useBrowserCompatibleCookies().dontReuseConnections();
final HttpPost post = new HttpPost(m_baseURL + "/REST/1.0/user/" + m_user);
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", m_user));
params.add(new BasicNameValuePair("pass", m_password));
CloseableHttpResponse response = null;
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
post.setEntity(entity);
response = m_clientWrapper.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode != HttpStatus.SC_OK) {
throw new RequestTrackerException("Received a non-200 response code from the server: " + responseCode);
} else {
if (response.getEntity() != null) {
EntityUtils.consume(response.getEntity());
}
LOG.warn("got user session for username: {}", m_user);
}
} catch (final Exception e) {
LOG.warn("Unable to get session (by requesting user details)", e);
} finally {
m_clientWrapper.close(response);
}
}
return m_clientWrapper;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class RequestTracker method getTicketAttributes.
private Map<String, String> getTicketAttributes(final String ticketQuery) throws RequestTrackerException {
if (ticketQuery == null) {
LOG.error("No ticket query specified!");
throw new RequestTrackerException("No ticket query specified!");
}
getSession();
Map<String, String> ticketAttributes = Collections.emptyMap();
final HttpGet get = new HttpGet(m_baseURL + "/REST/1.0/ticket/" + ticketQuery);
CloseableHttpResponse response = null;
try {
response = getClientWrapper().execute(get);
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode != HttpStatus.SC_OK) {
throw new RequestTrackerException("Received a non-200 response code from the server: " + responseCode);
} else {
if (response.getEntity() == null) {
LOG.debug("no entity returned by HTTP client");
}
ticketAttributes = parseResponseStream(response.getEntity().getContent());
}
} catch (final Exception e) {
LOG.error("HTTP exception attempting to get ticket.", e);
} finally {
getClientWrapper().close(response);
}
if (ticketAttributes.size() == 0) {
LOG.debug("matcher did not match {}", IN_TOKENS_PATTERN.pattern());
return null;
}
return ticketAttributes;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class RequestTracker method getQueue.
public RTQueue getQueue(long id) throws RequestTrackerException {
getSession();
Map<String, String> attributes = Collections.emptyMap();
final HttpGet get = new HttpGet(m_baseURL + "/REST/1.0/queue/" + id);
CloseableHttpResponse response = null;
try {
response = getClientWrapper().execute(get);
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode != HttpStatus.SC_OK) {
throw new RequestTrackerException("Received a non-200 response code from the server: " + responseCode);
} else {
if (response.getEntity() == null) {
LOG.debug("no entity returned by HTTP client");
}
attributes = parseResponseStream(response.getEntity().getContent());
}
} catch (final Exception e) {
LOG.error("An exception occurred while getting queue #{}", id, e);
return null;
} finally {
getClientWrapper().close(response);
}
if (attributes.containsKey("id") && attributes.containsKey("name")) {
final String queueId = attributes.get("id").replace("queue/", "");
final long longId = Long.parseLong(queueId);
final String name = attributes.get("name").trim();
final String priority = attributes.get("finalpriority").trim();
LOG.debug("name = {}, priority = {}", name, priority);
if ("".equals(name) && "".equals(priority)) {
LOG.debug("We got a response back, but it had no name or priority; assuming we have no access to this queue.");
return new RTInaccessibleQueue(longId);
}
return new RTQueue(longId, attributes.get("name"));
} else {
LOG.debug("id or name missing ({}, {})", attributes.get("id"), attributes.get("name"));
return null;
}
}
Aggregations