Search in sources :

Example 26 with Executor

use of org.apache.http.client.fluent.Executor in project streamsx.topology by IBMStreams.

the class RemoteEdgeContext method setSubmissionInstance.

static void setSubmissionInstance(AppEntity entity) throws IOException {
    Instance cfgInstance = getConfigInstance(entity);
    if (cfgInstance != null) {
        StreamsConnection sc = cfgInstance.getStreamsConnection();
        boolean verify = cfgInstance.getStreamsConnection().isVerify();
        JsonObject deploy = deploy(entity.submission);
        Function<Executor, String> authenticatorO = sc.getAuthenticator();
        deploy.addProperty(ContextProperties.SSL_VERIFY, verify);
        JsonObject service;
        if (authenticatorO instanceof ICP4DAuthenticator) {
            ICP4DAuthenticator authenticator = (ICP4DAuthenticator) authenticatorO;
            service = authenticator.config(verify);
        } else if (authenticatorO instanceof StandaloneAuthenticator) {
            StandaloneAuthenticator authenticator = (StandaloneAuthenticator) authenticatorO;
            service = authenticator.config(verify);
            String buildServiceUrl = getConfigBuildServiceUrl(entity);
            if (buildServiceUrl == null) {
                buildServiceUrl = System.getenv(Util.STREAMS_BUILD_URL);
            }
            if (buildServiceUrl != null) {
                // Copy so we don't affect instance. Version of gson we
                // use lacks deepCopy() so we serialize / parse to copy.
                String json = service.toString();
                service = new JsonParser().parse(json).getAsJsonObject();
                JsonObject connInfo = service.getAsJsonObject(CONNECTION_INFO);
                if (connInfo.has(SERVICE_BUILD_ENDPOINT)) {
                    connInfo.remove(SERVICE_BUILD_ENDPOINT);
                }
                connInfo.addProperty(SERVICE_BUILD_ENDPOINT, buildServiceUrl);
            }
        } else {
            throw new IllegalStateException("Invalid Instance for Streams V5: " + cfgInstance);
        }
        deploy.add(StreamsKeys.SERVICE_DEFINITION, service);
    }
}
Also used : ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) Executor(org.apache.http.client.fluent.Executor) Instance(com.ibm.streamsx.rest.Instance) JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) JsonParser(com.google.gson.JsonParser)

Example 27 with Executor

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

the class SimpleHttpDistributionTransport method retrievePackage.

@Nullable
public RemoteDistributionPackage retrievePackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest distributionRequest, @Nonnull DistributionTransportContext distributionContext) throws DistributionException {
    log.debug("pulling from {}", distributionEndpoint.getUri());
    try {
        URI distributionURI = RequestUtils.appendDistributionRequest(distributionEndpoint.getUri(), distributionRequest);
        Executor executor = getExecutor(distributionContext);
        // TODO : add queue parameter
        InputStream inputStream = HttpTransportUtils.fetchNextPackage(executor, distributionURI, httpConfiguration);
        if (inputStream == null) {
            return null;
        }
        try {
            final DistributionPackage responsePackage = packageBuilder.readPackage(resourceResolver, inputStream);
            responsePackage.getInfo().put(PACKAGE_INFO_PROPERTY_ORIGIN_URI, distributionURI);
            log.debug("pulled package with info {}", responsePackage.getInfo());
            return new DefaultRemoteDistributionPackage(responsePackage, executor, distributionURI);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    } catch (HttpHostConnectException e) {
        log.debug("could not connect to {} - skipping", distributionEndpoint.getUri());
    } catch (Exception ex) {
        log.error("cannot retrieve packages", ex);
    }
    return null;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) AbstractDistributionPackage(org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage) Executor(org.apache.http.client.fluent.Executor) InputStream(java.io.InputStream) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) 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) Nullable(javax.annotation.Nullable)

Example 28 with Executor

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

the class SimpleHttpDistributionTransport method getExecutor.

private Executor getExecutor(DistributionTransportContext distributionContext) {
    if (distributionContext.containsKey(contextKeyExecutor)) {
        return distributionContext.get(contextKeyExecutor, Executor.class);
    }
    Executor executor = Executor.newInstance();
    DistributionTransportSecret secret = secretProvider.getSecret(distributionEndpoint.getUri());
    executor = authenticate(secret, executor);
    distributionContext.put(contextKeyExecutor, executor);
    return executor;
}
Also used : Executor(org.apache.http.client.fluent.Executor) DistributionTransportSecret(org.apache.sling.distribution.transport.DistributionTransportSecret)

Example 29 with Executor

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

the class SimpleHttpDistributionTransportTest method testDeliverPackage.

@Test
public void testDeliverPackage() 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);
    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);
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    when(distributionPackage.getInfo()).thenReturn(new DistributionPackageInfo("type"));
    InputStream stream = mock(InputStream.class);
    when(distributionPackage.createInputStream()).thenReturn(stream);
    DistributionTransportContext distributionContext = mock(DistributionTransportContext.class);
    when(distributionContext.get(any(String.class), same(Executor.class))).thenReturn(executor);
    when(distributionContext.containsKey(any(String.class))).thenReturn(true);
    simpleHttpDistributionTransport.deliverPackage(resourceResolver, distributionPackage, distributionContext);
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) 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) 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) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) 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 30 with Executor

use of org.apache.http.client.fluent.Executor in project blueocean-plugin by jenkinsci.

the class HttpRequest method executeInternal.

private Content executeInternal() throws IOException {
    String uriPath = urlParts.size() > 0 ? UriTemplate.fromTemplate(requestUrl).expand(urlParts) : requestUrl;
    URIBuilder uri;
    String fullUrl;
    try {
        uri = new URIBuilder(baseUrl + uriPath);
        fullUrl = uri.toString();
    } catch (URISyntaxException ex) {
        throw new RuntimeException("could not parse request URL: " + baseUrl + requestUrl, ex);
    }
    logger.info("request url: " + fullUrl);
    Request request;
    switch(method) {
        case GET:
            request = Request.Get(fullUrl);
            break;
        case POST:
            request = Request.Post(fullUrl);
            break;
        case PUT:
            request = Request.Put(fullUrl);
            break;
        case PATCH:
            request = Request.Patch(fullUrl);
            break;
        case DELETE:
            request = Request.Delete(fullUrl);
            break;
        default:
            throw new RuntimeException("Invalid method: " + method);
    }
    headers.forEach(request::setHeader);
    if (requestBody != null) {
        request.bodyString(requestBody, ContentType.parse(contentType));
    }
    Executor exec = Executor.newInstance();
    // use 'Authorization: Basic' for username/password
    if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
        String authHost = uri.getPort() != -1 ? String.format("%s:%s", uri.getHost(), uri.getPort()) : uri.getHost();
        exec.authPreemptive(authHost).auth(username, password);
    }
    try {
        Response response = exec.execute(request);
        HttpResponse httpResponse = response.returnResponse();
        int returnedStatusCode = httpResponse.getStatusLine().getStatusCode();
        if (expectedStatus != returnedStatusCode) {
            throw new RuntimeException(String.format("Status code %s did not match expected %s", returnedStatusCode, expectedStatus));
        }
        // manually build content to avoid 'already consumed' exception from response.returnContent()
        return new ContentResponseHandler().handleEntity(httpResponse.getEntity());
    } catch (HttpResponseException ex) {
        throw new RuntimeException("Unexpected error during request", ex);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) ContentResponseHandler(org.apache.http.client.fluent.ContentResponseHandler) Executor(org.apache.http.client.fluent.Executor) Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

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