Search in sources :

Example 21 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 22 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)

Example 23 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project intellij-community by JetBrains.

the class EduStepicClient method getFromStepic.

static <T> T getFromStepic(String link, final Class<T> container, @NotNull final CloseableHttpClient client) throws IOException {
    if (!link.startsWith("/"))
        link = "/" + link;
    final HttpGet request = new HttpGet(EduStepicNames.STEPIC_API_URL + link);
    final CloseableHttpResponse response = client.execute(request);
    final StatusLine statusLine = response.getStatusLine();
    final HttpEntity responseEntity = response.getEntity();
    final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
    EntityUtils.consume(responseEntity);
    if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("Stepic returned non 200 status code " + responseString);
    }
    return deserializeStepicResponse(container, responseString);
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 24 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project intellij-community by JetBrains.

the class EduStepicAuthorizedClient method getTokens.

@Nullable
private static StepicWrappers.TokenInfo getTokens(@NotNull final List<NameValuePair> parameters) {
    final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    final HttpPost request = new HttpPost(EduStepicNames.TOKEN_URL);
    request.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
    try {
        final CloseableHttpClient client = EduStepicClient.getHttpClient();
        final CloseableHttpResponse response = client.execute(request);
        final StatusLine statusLine = response.getStatusLine();
        final HttpEntity responseEntity = response.getEntity();
        final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
        EntityUtils.consume(responseEntity);
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            return gson.fromJson(responseString, StepicWrappers.TokenInfo.class);
        } else {
            LOG.warn("Failed to Login: " + statusLine.getStatusCode() + statusLine.getReasonPhrase());
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    }
    return null;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) GsonBuilder(com.google.gson.GsonBuilder) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Gson(com.google.gson.Gson) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project intellij-community by JetBrains.

the class CCStepicConnector method postTask.

public static void postTask(final Project project, @NotNull final Task task, final int lessonId) {
    final HttpPost request = new HttpPost(EduStepicNames.STEPIC_API_URL + "/step-sources");
    final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(AnswerPlaceholder.class, new StudySerializationUtils.Json.StepicAnswerPlaceholderAdapter()).create();
    ApplicationManager.getApplication().invokeLater(() -> {
        final String requestBody = gson.toJson(new StepicWrappers.StepSourceWrapper(project, task, lessonId));
        request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        try {
            final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
            if (client == null)
                return;
            final CloseableHttpResponse response = client.execute(request);
            final StatusLine line = response.getStatusLine();
            final HttpEntity responseEntity = response.getEntity();
            final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
            EntityUtils.consume(responseEntity);
            if (line.getStatusCode() != HttpStatus.SC_CREATED) {
                LOG.error("Failed to push " + responseString);
                return;
            }
            final JsonObject postedTask = new Gson().fromJson(responseString, JsonObject.class);
            final JsonObject stepSource = postedTask.getAsJsonArray("step-sources").get(0).getAsJsonObject();
            task.setStepId(stepSource.getAsJsonPrimitive("id").getAsInt());
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
    });
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) GsonBuilder(com.google.gson.GsonBuilder) StudySerializationUtils(com.jetbrains.edu.learning.StudySerializationUtils) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) StatusLine(org.apache.http.StatusLine) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1314 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)502 HttpGet (org.apache.http.client.methods.HttpGet)485 Test (org.junit.Test)370 IOException (java.io.IOException)362 HttpPost (org.apache.http.client.methods.HttpPost)286 HttpEntity (org.apache.http.HttpEntity)248 StringEntity (org.apache.http.entity.StringEntity)229 JsonNode (com.fasterxml.jackson.databind.JsonNode)126 StatusLine (org.apache.http.StatusLine)121 URI (java.net.URI)118 InputStream (java.io.InputStream)112 ArrayList (java.util.ArrayList)87 Deployment (org.activiti.engine.test.Deployment)87 RequestConfig (org.apache.http.client.config.RequestConfig)87 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)87 HttpPut (org.apache.http.client.methods.HttpPut)79 Map (java.util.Map)75 Header (org.apache.http.Header)75 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)73