Search in sources :

Example 61 with CloseableHttpClient

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

Example 62 with CloseableHttpClient

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

Example 63 with CloseableHttpClient

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) XmlTest(org.opennms.core.test.xml.XmlTest) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 64 with CloseableHttpClient

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"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 65 with CloseableHttpClient

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HDIException(com.microsoft.azure.hdinsight.sdk.common.HDIException)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)430 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)238 HttpGet (org.apache.http.client.methods.HttpGet)212 Test (org.junit.Test)206 HttpResponse (org.apache.http.HttpResponse)108 HttpEntity (org.apache.http.HttpEntity)92 IOException (java.io.IOException)90 HttpPost (org.apache.http.client.methods.HttpPost)85 StringEntity (org.apache.http.entity.StringEntity)67 InputStream (java.io.InputStream)57 StatusLine (org.apache.http.StatusLine)41 HttpHost (org.apache.http.HttpHost)36 URI (java.net.URI)35 RequestConfig (org.apache.http.client.config.RequestConfig)32 Header (org.apache.http.Header)24 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 File (java.io.File)22 HttpPut (org.apache.http.client.methods.HttpPut)22 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19