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);
}
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations