use of org.apache.http.impl.client.CloseableHttpClient 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());
}
});
}
use of org.apache.http.impl.client.CloseableHttpClient in project intellij-community by JetBrains.
the class CCStepicConnector method deleteTask.
public static void deleteTask(@NotNull final Integer task) {
final HttpDelete request = new HttpDelete(EduStepicNames.STEPIC_API_URL + EduStepicNames.STEP_SOURCES + task);
ApplicationManager.getApplication().invokeLater(() -> {
try {
final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
if (client == null)
return;
final CloseableHttpResponse response = client.execute(request);
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
EntityUtils.consume(responseEntity);
final StatusLine line = response.getStatusLine();
if (line.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
LOG.error("Failed to delete task " + responseString);
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
});
}
use of org.apache.http.impl.client.CloseableHttpClient in project opennms by OpenNMS.
the class NCSNorthbounderIT method testTestServlet.
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/fmpm", path = "src/test/resources/test-webapp") })
public void testTestServlet() throws Exception {
TestServlet.reset();
CloseableHttpClient client = HttpClientBuilder.create().build();
try {
HttpEntity entity = new StringEntity(xml);
HttpPost method = new HttpPost("http://localhost:10342/fmpm/restful/NotificationMessageRelay");
method.setEntity(entity);
HttpResponse response = client.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(xml, TestServlet.getPosted());
} finally {
IOUtils.closeQuietly(client);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project azure-tools-for-java by Microsoft.
the class LivyTask method call.
@Override
public String call() throws Exception {
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
HttpGet httpGet = new HttpGet(path);
httpGet.addHeader("Content-Type", "application/json");
CloseableHttpResponse response = httpclient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
HttpEntity httpEntity = response.getEntity();
return IOUtils.toString(httpEntity.getContent(), Charset.forName("utf-8"));
}
use of org.apache.http.impl.client.CloseableHttpClient in project azure-tools-for-java by Microsoft.
the class MultiRestTask method call.
@Override
public List<String> call() throws Exception {
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
List<String> results = new ArrayList<>();
for (String path : paths) {
HttpGet httpGet = new HttpGet(path);
httpGet.addHeader("Content-Type", "application/json");
CloseableHttpResponse response = httpclient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
if (code == 200 || code == 201) {
results.add(EntityUtils.toString(response.getEntity()));
} else {
throw new HDIException(response.getStatusLine().getReasonPhrase(), code);
}
}
return results;
}
Aggregations