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