use of org.apache.http.client.fluent.Executor in project opennms by OpenNMS.
the class DiscoveryIT method canDiscoverRemoteNodes.
@Test
public void canDiscoverRemoteNodes() throws ClientProtocolException, IOException {
Date startOfTest = new Date();
final String tomcatIp = minionSystem.getContainerInfo(ContainerAlias.TOMCAT).networkSettings().ipAddress();
final InetSocketAddress opennmsHttp = minionSystem.getServiceAddress(ContainerAlias.OPENNMS, 8980);
final HttpHost opennmsHttpHost = new HttpHost(opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort());
HttpClient instance = HttpClientBuilder.create().setRedirectStrategy(// Ignore the 302 response to the POST
new LaxRedirectStrategy()).build();
Executor executor = Executor.newInstance(instance).auth(opennmsHttpHost, "admin", "admin").authPreemptive(opennmsHttpHost);
// Configure Discovery with the specific address of our Tomcat server
// No REST endpoint is currently available to configure the Discovery daemon
// so we resort to POSTin nasty form data
executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=AddSpecific", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("specificipaddress", tomcatIp).add("specifictimeout", "2000").add("specificretries", "1").add("initialsleeptime", "30000").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=SaveAndRestart", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("initialsleeptime", "1").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
InetSocketAddress pgsql = minionSystem.getServiceAddress(ContainerAlias.POSTGRES, 5432);
HibernateDaoFactory daoFactory = new HibernateDaoFactory(pgsql);
EventDao eventDao = daoFactory.getDao(EventDaoHibernate.class);
Criteria criteria = new CriteriaBuilder(OnmsEvent.class).eq("eventUei", EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI).ge("eventTime", startOfTest).toCriteria();
await().atMost(1, MINUTES).pollInterval(10, SECONDS).until(DaoUtils.countMatchingCallable(eventDao, criteria), greaterThan(0));
}
use of org.apache.http.client.fluent.Executor 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);
}
use of org.apache.http.client.fluent.Executor 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);
}
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);
}
}
}
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"));
}
Aggregations