Search in sources :

Example 1 with Response

use of org.apache.http.client.fluent.Response in project moco by dreamhead.

the class MocoConnectionTest method should_keep_alive_for_1_0_keep_alive_request.

@Test
public void should_keep_alive_for_1_0_keep_alive_request() throws Exception {
    server.response("foo");
    running(server, new Runnable() {

        @Override
        public void run() throws Exception {
            Response response = Request.Get(root()).version(HttpVersion.HTTP_1_0).addHeader("Connection", "keep-alive").execute();
            String connection = response.returnResponse().getFirstHeader("Connection").getValue();
            assertThat(connection, is("keep-alive"));
        }
    });
}
Also used : Response(org.apache.http.client.fluent.Response) Runnable(com.github.dreamhead.moco.Runnable) AbstractMocoHttpTest(com.github.dreamhead.moco.AbstractMocoHttpTest) Test(org.junit.Test)

Example 2 with Response

use of org.apache.http.client.fluent.Response in project intellij-community by JetBrains.

the class StatisticsHttpClientSender method send.

@Override
public void send(@NotNull String url, @NotNull String content) throws StatServiceException {
    try {
        HttpConfigurable.getInstance().prepareURL(url);
        Response response = Request.Post(url).bodyForm(Form.form().add("content", content).add("uuid", PermanentInstallationID.get()).add("patch", String.valueOf(false)).add("ide", ApplicationNamesInfo.getInstance().getProductName()).build()).execute();
        final HttpResponse httpResponse = response.returnResponse();
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new StatServiceException("Error during data sending... Code: " + httpResponse.getStatusLine().getStatusCode());
        }
        final Header errors = httpResponse.getFirstHeader("errors");
        if (errors != null) {
            String value = errors.getValue();
            throw new StatServiceException("Error during updating statistics " + (!StringUtil.isEmptyOrSpaces(value) ? " : " + value : ""));
        }
    } catch (StatServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new StatServiceException("Error during data sending...", e);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse)

Example 3 with Response

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

the class SimpleHttpDistributionTransportTest method testRetrievePackagesRemotelyFailing.

@Test
public void testRetrievePackagesRemotelyFailing() throws Exception {
    DistributionTransportSecret secret = mock(DistributionTransportSecret.class);
    Map<String, String> credentialsMap = new HashMap<String, String>();
    credentialsMap.put("username", "foo");
    credentialsMap.put("password", "foo");
    when(secret.asCredentialsMap()).thenReturn(credentialsMap);
    DistributionTransportSecretProvider secretProvider = mock(DistributionTransportSecretProvider.class);
    when(secretProvider.getSecret(any(URI.class))).thenReturn(secret);
    Executor executor = mock(Executor.class);
    Response response = mock(Response.class);
    HttpResponse httpResponse = mock(HttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(statusLine.getStatusCode()).thenReturn(404);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(response.returnResponse()).thenReturn(httpResponse);
    when(executor.execute(any(Request.class))).thenReturn(response);
    DistributionEndpoint endpoint = new DistributionEndpoint("http://127.0.0.1:8080/some/resource");
    DistributionPackageBuilder packageBuilder = mock(DistributionPackageBuilder.class);
    SimpleHttpDistributionTransport simpleHttpDistributionTransport = new SimpleHttpDistributionTransport(mock(DefaultDistributionLog.class), endpoint, packageBuilder, secretProvider, new HttpConfiguration(1000, 1000));
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    DistributionRequest distributionRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    RemoteDistributionPackage retrievedPackage = simpleHttpDistributionTransport.retrievePackage(resourceResolver, distributionRequest, new DistributionTransportContext());
    assertNull(retrievedPackage);
}
Also used : HashMap(java.util.HashMap) Request(org.apache.http.client.fluent.Request) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) HttpResponse(org.apache.http.HttpResponse) DistributionTransportSecretProvider(org.apache.sling.distribution.transport.DistributionTransportSecretProvider) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) URI(java.net.URI) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) StatusLine(org.apache.http.StatusLine) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionTransportSecret(org.apache.sling.distribution.transport.DistributionTransportSecret) Executor(org.apache.http.client.fluent.Executor) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 4 with Response

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

the class SimpleHttpDistributionTransportTest method testRetrievePackagesRemotelyWorking.

@Test
public void testRetrievePackagesRemotelyWorking() throws Exception {
    DistributionTransportSecret secret = mock(DistributionTransportSecret.class);
    Map<String, String> credentialsMap = new HashMap<String, String>();
    credentialsMap.put("username", "foo");
    credentialsMap.put("password", "foo");
    when(secret.asCredentialsMap()).thenReturn(credentialsMap);
    DistributionTransportSecretProvider secretProvider = mock(DistributionTransportSecretProvider.class);
    when(secretProvider.getSecret(any(URI.class))).thenReturn(secret);
    Executor executor = mock(Executor.class);
    Response response = mock(Response.class);
    HttpResponse httpResponse = mock(HttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(statusLine.getStatusCode()).thenReturn(200);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    HttpEntity entity = mock(HttpEntity.class);
    InputStream stream = new ByteArrayInputStream("package binary stuff".getBytes("UTF-8"));
    when(entity.getContent()).thenReturn(stream);
    when(httpResponse.getEntity()).thenReturn(entity);
    when(response.returnResponse()).thenReturn(httpResponse);
    when(executor.execute(any(Request.class))).thenReturn(response);
    DistributionEndpoint endpoint = new DistributionEndpoint("http://127.0.0.1:8080/some/resource");
    DistributionPackageBuilder packageBuilder = mock(DistributionPackageBuilder.class);
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    when(packageBuilder.readPackage(any(ResourceResolver.class), any(InputStream.class))).thenReturn(distributionPackage);
    SimpleHttpDistributionTransport simpleHttpDistributionTransport = new SimpleHttpDistributionTransport(mock(DefaultDistributionLog.class), endpoint, packageBuilder, secretProvider, new HttpConfiguration(1000, 1000));
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    DistributionRequest distributionRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, "/");
    DistributionTransportContext distributionContext = mock(DistributionTransportContext.class);
    when(distributionContext.get(any(String.class), same(Executor.class))).thenReturn(executor);
    when(distributionContext.containsKey(any(String.class))).thenReturn(true);
    RemoteDistributionPackage retrievedPackage = simpleHttpDistributionTransport.retrievePackage(resourceResolver, distributionRequest, distributionContext);
    assertNotNull(retrievedPackage);
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) DistributionTransportSecretProvider(org.apache.sling.distribution.transport.DistributionTransportSecretProvider) URI(java.net.URI) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionTransportSecret(org.apache.sling.distribution.transport.DistributionTransportSecret) Executor(org.apache.http.client.fluent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) HttpResponse(org.apache.http.HttpResponse) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) StatusLine(org.apache.http.StatusLine) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 5 with Response

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

Response (org.apache.http.client.fluent.Response)8 Test (org.junit.Test)6 URI (java.net.URI)4 HttpResponse (org.apache.http.HttpResponse)4 Executor (org.apache.http.client.fluent.Executor)4 Request (org.apache.http.client.fluent.Request)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)4 AbstractMocoHttpTest (com.github.dreamhead.moco.AbstractMocoHttpTest)3 Runnable (com.github.dreamhead.moco.Runnable)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)3 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)3 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)3 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)3 DistributionTransportSecret (org.apache.sling.distribution.transport.DistributionTransportSecret)3 DistributionTransportSecretProvider (org.apache.sling.distribution.transport.DistributionTransportSecretProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StatusLine (org.apache.http.StatusLine)2