Search in sources :

Example 6 with HttpResources

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));
    }
}
Also used : Header(org.apache.http.Header) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse)

Example 7 with HttpResources

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 );
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResources.entityToString(org.commonjava.indy.client.core.helper.HttpResources.entityToString) IOException(java.io.IOException)

Example 8 with HttpResources

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());
    }
}
Also used : HttpResources(org.commonjava.indy.client.core.helper.HttpResources) IndyClientException(org.commonjava.indy.client.core.IndyClientException) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Example 9 with HttpResources

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 );
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 10 with HttpResources

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));
    }
}
Also used : StatusLine(org.apache.http.StatusLine) ZipInputStream(java.util.zip.ZipInputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse) IndyClientException(org.commonjava.indy.client.core.IndyClientException) IndyResponseErrorDetails(org.commonjava.indy.client.core.IndyResponseErrorDetails) IOException(java.io.IOException)

Aggregations

HttpResources (org.commonjava.indy.client.core.helper.HttpResources)10 IOException (java.io.IOException)6 HttpResponse (org.apache.http.HttpResponse)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)3 IndyClientException (org.commonjava.indy.client.core.IndyClientException)3 InputStream (java.io.InputStream)2 StatusLine (org.apache.http.StatusLine)2 HttpPost (org.apache.http.client.methods.HttpPost)2 IndyClientHttp (org.commonjava.indy.client.core.IndyClientHttp)2 IndyResponseErrorDetails (org.commonjava.indy.client.core.IndyResponseErrorDetails)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 Header (org.apache.http.Header)1 StringEntity (org.apache.http.entity.StringEntity)1 Mirror (org.apache.maven.settings.Mirror)1 Settings (org.apache.maven.settings.Settings)1 SettingsXpp3Reader (org.apache.maven.settings.io.xpp3.SettingsXpp3Reader)1