use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.
the class GroupHttpHeadersFromSameRepoWhenNotInPathMaskTest method assertGetContentAndLength.
private void assertGetContentAndLength(ArtifactStore store, String path, String content) throws IndyClientException, IOException {
IndyClientHttp http = httpModule.getHttp();
try (HttpResources resources = http.getRaw(client.content().contentPath(store.getKey(), path))) {
HttpResponse response = resources.getResponse();
assertThat("Request " + store.getKey() + ":" + path + " failed with status: " + response.getStatusLine(), response.getStatusLine().getStatusCode(), equalTo(200));
Header header = response.getFirstHeader("Content-Length");
assertThat("Content-Length header missing: " + store.getKey() + ":" + path, header, notNullValue());
String clStr = header.getValue();
assertThat(clStr, notNullValue());
long length = content.getBytes().length;
Long len = Long.parseLong(clStr);
assertThat("Error: " + store.getKey() + ":" + path + ": had incorrect retrieved content-length (" + len + "); should have been: " + length, len, equalTo(Long.valueOf(length)));
assertThat("Unexpected response content: " + store.getKey() + ":" + path, IOUtils.toString(resources.getResponseEntityContent()), equalTo(content));
}
}
use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.
the class IndyClientHttp method postRaw.
public HttpResources postRaw(final String path, Object value, final Map<String, String> headers) throws IndyClientException {
checkRequestValue(value);
connect();
CloseableHttpResponse response = null;
try {
final HttpPost req = newRawPost(buildUrl(baseUrl, path));
if (headers != null) {
for (String key : headers.keySet()) {
req.setHeader(key, headers.get(key));
}
}
req.setEntity(new StringEntity(objectMapper.writeValueAsString(value)));
final CloseableHttpClient client = newClient();
response = client.execute(req, newContext());
return new HttpResources(req, response, client);
} catch (final IOException e) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
// DO NOT CLOSE!!!! We're handing off control of the response to the caller!
// closeQuietly( response );
}
}
use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.
the class IndyContentClientModule method get.
public InputStream get(final StoreKey key, final String path) throws IndyClientException {
final HttpResources resources = http.getRaw(contentPath(key, path));
if (resources.getStatusCode() != 200) {
IOUtils.closeQuietly(resources);
if (resources.getStatusCode() == 404) {
return null;
}
throw new IndyClientException(resources.getStatusCode(), "Response returned status: %s.", resources.getStatusLine());
}
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Returning stream that should contain: {} bytes", resources.getResponse().getFirstHeader("Content-Length"));
try {
return resources.getResponseStream();
} catch (final IOException e) {
IOUtils.closeQuietly(resources);
throw new IndyClientException("Failed to open response content stream: %s", e, e.getMessage());
}
}
use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.
the class IndyClientHttp method execute.
public HttpResources execute(HttpRequestBase request) throws IndyClientException {
connect();
CloseableHttpResponse response = null;
try {
final CloseableHttpClient client = newClient();
response = client.execute(request, newContext());
return new HttpResources(request, response, client);
} catch (final IOException e) {
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
// DO NOT CLOSE!!!! We're handing off control of the response to the caller!
// closeQuietly( response );
}
}
use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.
the class IndyDiagnosticsClientModule method getDiagnosticBundle.
public ZipInputStream getDiagnosticBundle() throws IndyClientException {
HttpGet get = new HttpGet(buildUrl(getHttp().getBaseUrl(), "/diag/bundle"));
HttpResources resources = getHttp().getRaw(get);
HttpResponse response = resources.getResponse();
StatusLine sl = response.getStatusLine();
if (sl.getStatusCode() != 200) {
throw new IndyClientException(sl.getStatusCode(), "Error retrieving diagnostic bundle: %s", new IndyResponseErrorDetails(response));
}
try {
return new ZipInputStream(resources.getResponseStream());
} catch (IOException e) {
throw new IndyClientException("Failed to read bundle stream from response: %s", e, new IndyResponseErrorDetails(response));
}
}
Aggregations