Search in sources :

Example 6 with Executor

use of org.apache.http.client.fluent.Executor 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)

Example 7 with Executor

use of org.apache.http.client.fluent.Executor in project acs-aem-commons by Adobe-Consulting-Services.

the class HttpClientFactoryImplTest method testAuthenticatedGet.

@Test
public void testAuthenticatedGet() throws Exception {
    config.put("username", username);
    config.put("password", password);
    impl.activate(config);
    Request get = impl.get("/auth");
    Executor exec = impl.getExecutor();
    String str = exec.execute(get).handleResponse(new BasicResponseHandler());
    assertThat(str, is("OK"));
}
Also used : Executor(org.apache.http.client.fluent.Executor) HttpRequest(org.mockserver.model.HttpRequest) Request(org.apache.http.client.fluent.Request) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 8 with Executor

use of org.apache.http.client.fluent.Executor in project acs-aem-commons by Adobe-Consulting-Services.

the class HttpClientFactoryImplTest method testAnonymousGet.

@Test
public void testAnonymousGet() throws Exception {
    impl.activate(config);
    Request get = impl.get("/anon");
    Executor exec = impl.getExecutor();
    String str = exec.execute(get).handleResponse(new BasicResponseHandler());
    assertThat(str, is("OK"));
}
Also used : Executor(org.apache.http.client.fluent.Executor) HttpRequest(org.mockserver.model.HttpRequest) Request(org.apache.http.client.fluent.Request) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 9 with Executor

use of org.apache.http.client.fluent.Executor in project substitution-schedule-parser by vertretungsplanme.

the class LoginHandlerTest method testPostAuthFailCheckUrl.

@Test(expected = CredentialInvalidException.class)
public void testPostAuthFailCheckUrl() throws JSONException, IOException, CredentialInvalidException {
    stubPostAuth();
    Executor exec = newExecutor();
    LoginHandler handler = new LoginHandler(dataPostCheckUrl, wrong, null);
    // should throw CredentialInvalidException
    handler.handleLogin(exec, null);
}
Also used : Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Example 10 with Executor

use of org.apache.http.client.fluent.Executor in project substitution-schedule-parser by vertretungsplanme.

the class LoginHandlerTest method testBasicAuthFail.

@Test
public void testBasicAuthFail() throws JSONException, IOException, CredentialInvalidException {
    stubBasicAuth();
    Executor exec = newExecutor();
    LoginHandler handler = new LoginHandler(dataBasic, wrong, null);
    handler.handleLogin(exec, null);
    // loading page should succeed
    int status = exec.execute(Request.Get(baseurl + "/index.html")).returnResponse().getStatusLine().getStatusCode();
    assertEquals(401, status);
}
Also used : Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Aggregations

Executor (org.apache.http.client.fluent.Executor)46 Test (org.junit.Test)26 Request (org.apache.http.client.fluent.Request)12 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)11 HttpResponse (org.apache.http.HttpResponse)6 Response (org.apache.http.client.fluent.Response)6 URI (java.net.URI)5 Cookie (org.apache.http.cookie.Cookie)5 Test (org.junit.jupiter.api.Test)5 JsonObject (com.google.gson.JsonObject)4 InputStream (java.io.InputStream)4 DistributionTransportSecret (org.apache.sling.distribution.transport.DistributionTransportSecret)4 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)3 HashMap (java.util.HashMap)3 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 JsonParser (com.google.gson.JsonParser)2 Instance (com.ibm.streamsx.rest.Instance)2 ICP4DAuthenticator (com.ibm.streamsx.rest.internal.ICP4DAuthenticator)2 StandaloneAuthenticator (com.ibm.streamsx.rest.internal.StandaloneAuthenticator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2