Search in sources :

Example 6 with Request

use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.

the class RestSession method postWithHeader.

public RestResponse postWithHeader(String endPoint, Object content, Header header) throws IOException {
    Request post = Request.Post(getUrl(endPoint));
    if (header != null) {
        post.addHeader(header);
    }
    if (content != null) {
        post.addHeader(new BasicHeader("Content-Type", "application/json"));
        post.body(new StringEntity(OutputFormat.JSON_COMPACT.newGson().toJson(content), UTF_8));
    }
    return execute(post);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) Request(org.apache.http.client.fluent.Request) BasicHeader(org.apache.http.message.BasicHeader)

Example 7 with Request

use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.

the class RestSession method putWithHeader.

public RestResponse putWithHeader(String endPoint, Header header, Object content) throws IOException {
    Request put = Request.Put(getUrl(endPoint));
    if (header != null) {
        put.addHeader(header);
    }
    if (content != null) {
        put.addHeader(new BasicHeader("Content-Type", "application/json"));
        put.body(new StringEntity(OutputFormat.JSON_COMPACT.newGson().toJson(content), UTF_8));
    }
    return execute(put);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) Request(org.apache.http.client.fluent.Request) BasicHeader(org.apache.http.message.BasicHeader)

Example 8 with Request

use of org.apache.http.client.fluent.Request in project sling by apache.

the class HttpTransportUtils method fetchNextPackage.

public static InputStream fetchNextPackage(Executor executor, URI distributionURI, HttpConfiguration httpConfiguration) throws URISyntaxException, IOException {
    URI fetchUri = getFetchUri(distributionURI);
    Request fetchReq = Request.Post(fetchUri).connectTimeout(httpConfiguration.getConnectTimeout()).socketTimeout(httpConfiguration.getSocketTimeout()).addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE).useExpectContinue();
    HttpResponse httpResponse = executor.execute(fetchReq).returnResponse();
    if (httpResponse.getStatusLine().getStatusCode() != 200) {
        return null;
    }
    HttpEntity entity = httpResponse.getEntity();
    return entity.getContent();
}
Also used : HttpEntity(org.apache.http.HttpEntity) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI)

Example 9 with Request

use of org.apache.http.client.fluent.Request in project sling by apache.

the class HttpTransportUtils method deletePackage.

public static boolean deletePackage(Executor executor, URI distributionURI, String remotePackageId) throws URISyntaxException, IOException {
    URI deleteUri = getDeleteUri(distributionURI, remotePackageId);
    Request deleteReq = Request.Post(deleteUri).useExpectContinue();
    HttpResponse httpResponse = executor.execute(deleteReq).returnResponse();
    return httpResponse.getStatusLine().getStatusCode() == 200;
}
Also used : Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI)

Example 10 with Request

use of org.apache.http.client.fluent.Request in project sling by apache.

the class SimpleHttpDistributionTransport method deliverPackage.

public void deliverPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionPackage distributionPackage, @Nonnull DistributionTransportContext distributionContext) throws DistributionException {
    String hostAndPort = getHostAndPort(distributionEndpoint.getUri());
    DistributionPackageInfo info = distributionPackage.getInfo();
    URI packageOrigin = info.get(PACKAGE_INFO_PROPERTY_ORIGIN_URI, URI.class);
    if (packageOrigin != null && hostAndPort.equals(getHostAndPort(packageOrigin))) {
        log.debug("skipping distribution of package {} to same origin {}", distributionPackage.getId(), hostAndPort);
    } else {
        try {
            Executor executor = getExecutor(distributionContext);
            Request req = Request.Post(distributionEndpoint.getUri()).connectTimeout(httpConfiguration.getConnectTimeout()).socketTimeout(httpConfiguration.getSocketTimeout()).addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE).useExpectContinue();
            // add the message body digest, see https://tools.ietf.org/html/rfc3230#section-4.3.2
            if (distributionPackage instanceof AbstractDistributionPackage) {
                AbstractDistributionPackage adb = (AbstractDistributionPackage) distributionPackage;
                if (adb.getDigestAlgorithm() != null && adb.getDigestMessage() != null) {
                    req.addHeader(DIGEST_HEADER, String.format("%s=%s", adb.getDigestAlgorithm(), adb.getDigestMessage()));
                }
            }
            InputStream inputStream = null;
            try {
                inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage);
                req = req.bodyStream(inputStream, ContentType.APPLICATION_OCTET_STREAM);
                Response response = executor.execute(req);
                // throws an error if HTTP status is >= 300
                response.returnContent();
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
            log.debug("delivered packageId={}, endpoint={}", distributionPackage.getId(), distributionEndpoint.getUri());
        } catch (HttpHostConnectException e) {
            throw new RecoverableDistributionException("endpoint not available " + distributionEndpoint.getUri(), e);
        } catch (HttpResponseException e) {
            int statusCode = e.getStatusCode();
            if (statusCode == 404 || statusCode == 401) {
                throw new RecoverableDistributionException("not enough rights for " + distributionEndpoint.getUri(), e);
            }
            throw new DistributionException(e);
        } catch (Exception e) {
            throw new DistributionException(e);
        }
    }
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) DistributionRequest(org.apache.sling.distribution.DistributionRequest) HttpResponseException(org.apache.http.client.HttpResponseException) URI(java.net.URI) DistributionException(org.apache.sling.distribution.common.DistributionException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) HttpResponseException(org.apache.http.client.HttpResponseException) Response(org.apache.http.client.fluent.Response) Executor(org.apache.http.client.fluent.Executor) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) DistributionException(org.apache.sling.distribution.common.DistributionException) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) AbstractDistributionPackage(org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage)

Aggregations

Request (org.apache.http.client.fluent.Request)11 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 Result (com.google.gerrit.acceptance.PushOneCommit.Result)4 URI (java.net.URI)4 Test (org.junit.Test)4 BasicHeader (org.apache.http.message.BasicHeader)3 HttpResponse (org.apache.http.HttpResponse)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 StringEntity (org.apache.http.entity.StringEntity)2 RestResponse (com.google.gerrit.acceptance.RestResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 Executor (org.apache.http.client.fluent.Executor)1 Response (org.apache.http.client.fluent.Response)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1