Search in sources :

Example 86 with CloseableHttpResponse

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."));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 87 with CloseableHttpResponse

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 88 with CloseableHttpResponse

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Example 89 with CloseableHttpResponse

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;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 90 with CloseableHttpResponse

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;
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)536 HttpGet (org.apache.http.client.methods.HttpGet)242 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)171 StringEntity (org.apache.http.entity.StringEntity)127 JsonNode (com.fasterxml.jackson.databind.JsonNode)125 Test (org.junit.Test)125 HttpPost (org.apache.http.client.methods.HttpPost)107 IOException (java.io.IOException)105 HttpEntity (org.apache.http.HttpEntity)103 Deployment (org.activiti.engine.test.Deployment)87 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)67 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)57 InputStream (java.io.InputStream)53 HttpPut (org.apache.http.client.methods.HttpPut)50 Task (org.activiti.engine.task.Task)41 URI (java.net.URI)36 StatusLine (org.apache.http.StatusLine)34 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)34 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)31 BufferedReader (java.io.BufferedReader)30