Search in sources :

Example 61 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.

the class ServerBinaryDownloader method download.

protected synchronized boolean download(final DownloadableFile downloadableFile) throws Exception {
    File toDownload = downloadableFile.getLocalFile();
    LOG.info("Downloading " + toDownload);
    String url = downloadableFile.url(urlGenerator);
    final HttpRequestBase request = new HttpGet(url);
    request.setConfig(RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT_IN_MILLISECONDS).build());
    try (CloseableHttpClient httpClient = httpClientBuilder.build();
        CloseableHttpResponse response = httpClient.execute(request)) {
        LOG.info("Got server response");
        if (response.getEntity() == null) {
            LOG.error("Unable to read file from the server response");
            return false;
        }
        handleInvalidResponse(response, url);
        try (BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(downloadableFile.getLocalFile()))) {
            response.getEntity().writeTo(outStream);
            LOG.info("Piped the stream to " + downloadableFile);
        }
    }
    return true;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 62 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.

the class GoHttpClientHttpInvokerRequestExecutor method doExecuteRequest.

@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
    HttpPost postMethod = new HttpPost(config.getServiceUrl());
    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    entity.setContentType(getContentType());
    postMethod.setEntity(entity);
    BasicHttpContext context = null;
    if (environment.useSslContext()) {
        context = new BasicHttpContext();
        context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal());
    }
    try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) {
        validateResponse(response);
        InputStream responseBody = getResponseBody(response);
        return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 63 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.

the class AgentUpgradeService method checkForUpgrade.

void checkForUpgrade(String agentMd5, String launcherMd5, String agentPluginsMd5, String tfsImplMd5) throws Exception {
    HttpGet method = getAgentLatestStatusGetMethod();
    try (final CloseableHttpResponse response = httpClient.execute(method)) {
        if (response.getStatusLine().getStatusCode() != 200) {
            LOGGER.error(String.format("[Agent Upgrade] Got status %d %s from Go", response.getStatusLine().getStatusCode(), response.getStatusLine()));
            return;
        }
        validateMd5(agentMd5, response, SystemEnvironment.AGENT_CONTENT_MD5_HEADER, "itself");
        validateMd5(launcherMd5, response, SystemEnvironment.AGENT_LAUNCHER_CONTENT_MD5_HEADER, "launcher");
        validateMd5(agentPluginsMd5, response, SystemEnvironment.AGENT_PLUGINS_ZIP_MD5_HEADER, "plugins");
        validateMd5(tfsImplMd5, response, SystemEnvironment.AGENT_TFS_SDK_MD5_HEADER, "tfs-impl jar");
    } catch (IOException ioe) {
        String message = String.format("[Agent Upgrade] Couldn't connect to: %s: %s", urlService.getAgentLatestStatusUrl(), ioe.toString());
        LOGGER.error(message);
        LOGGER.debug(message, ioe);
        throw ioe;
    } finally {
        method.releaseConnection();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 64 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.

the class AgentUpgradeServiceTest method setUp.

@Before
public void setUp() throws Exception {
    systemEnvironment = mock(SystemEnvironment.class);
    urlService = mock(URLService.class);
    GoAgentServerHttpClient httpClient = mock(GoAgentServerHttpClient.class);
    jvmExitter = mock(AgentUpgradeService.JvmExitter.class);
    agentUpgradeService = spy(new AgentUpgradeService(urlService, httpClient, systemEnvironment, jvmExitter));
    httpMethod = mock(HttpGet.class);
    doReturn(httpMethod).when(agentUpgradeService).getAgentLatestStatusGetMethod();
    closeableHttpResponse = mock(CloseableHttpResponse.class);
    when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(httpMethod)).thenReturn(closeableHttpResponse);
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) URLService(com.thoughtworks.go.util.URLService) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GoAgentServerHttpClient(com.thoughtworks.go.agent.common.ssl.GoAgentServerHttpClient) BasicStatusLine(org.apache.http.message.BasicStatusLine) Before(org.junit.Before)

Example 65 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.

the class HttpService method upload.

public int upload(String url, long size, File artifactFile, Properties artifactChecksums) throws IOException {
    String absolutePath = artifactFile.getAbsolutePath();
    if (!artifactFile.exists()) {
        String message = "Failed to find file [" + absolutePath + "]";
        LOGGER.error(message);
        throw new FileNotFoundException(message);
    }
    LOGGER.info(String.format("Uploading file [%s] to url [%s]", absolutePath, url));
    HttpPost filePost = createHttpPostForUpload(url, size, artifactFile, artifactChecksums);
    try (CloseableHttpResponse response = execute(filePost)) {
        return response.getStatusLine().getStatusCode();
    } catch (IOException e) {
        LOGGER.error("Error while uploading file [" + artifactFile.getAbsolutePath() + "]", e);
        throw e;
    } finally {
        filePost.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

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