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