Java Code Examples for org.apache.brooklyn.util.http.HttpTool#execAndConsume()

The following examples show how to use org.apache.brooklyn.util.http.HttpTool#execAndConsume() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrooklynWebServerTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void verifySecurityInitializedExplicitUser() throws Exception {
    webServer = new BrooklynWebServer(newManagementContext(brooklynProperties));
    webServer.start();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("myuser", "somepass"));
    HttpClient client = HttpTool.httpClientBuilder()
        .credentials(new UsernamePasswordCredentials("myuser", "somepass"))
        .uri(webServer.getRootUrl())
        .build();

    try {
        HttpToolResponse response = HttpTool.execAndConsume(client, new HttpGet(webServer.getRootUrl()));
        assertEquals(response.getResponseCode(), 401);
    } finally {
        webServer.stop();
    }
}
 
Example 2
Source File: BrooklynWebServerTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private void verifyHttpsFromConfig(BrooklynProperties brooklynProperties) throws Exception {
    webServer = new BrooklynWebServer(MutableMap.of(), newManagementContext(brooklynProperties));
    webServer.skipSecurity();
    webServer.start();
    
    try {
        KeyStore keyStore = load("client.ks", "password");
        KeyStore trustStore = load("client.ts", "password");
        SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, "password", trustStore, (SecureRandom)null, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpToolResponse response = HttpTool.execAndConsume(
                HttpTool.httpClientBuilder()
                        .port(webServer.getActualPort())
                        .https(true)
                        .socketFactory(socketFactory)
                        .build(),
                new HttpGet(webServer.getRootUrl()));
        assertEquals(response.getResponseCode(), 200);
    } finally {
        webServer.stop();
    }
}
 
Example 3
Source File: BrooklynLauncherTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test(groups="Integration")
public void testStartsWebServerWithCredentials() throws Exception {
    launcher = newLauncherForTests(true)
            .restServerPort("10000+")
            .brooklynProperties(BrooklynWebConfig.USERS, "myname")
            .brooklynProperties(BrooklynWebConfig.PASSWORD_FOR_USER("myname"), "mypassword")
            .start();
    String uri = launcher.getServerDetails().getWebServerUrl();
    
    HttpToolResponse response = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(), new HttpGet(uri));
    assertEquals(response.getResponseCode(), 401);
    
    HttpToolResponse response2 = HttpTool.execAndConsume(
            HttpTool.httpClientBuilder()
                    .uri(uri)
                    .credentials(new UsernamePasswordCredentials("myname", "mypassword"))
                    .build(), 
            new HttpGet(uri));
    assertEquals(response2.getResponseCode(), 200);
}
 
Example 4
Source File: CorsFilterLauncherTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void test1CorsIsEnabledOnAllDomainsGET() throws IOException {
    final String shouldAllowOrigin = "http://foo.bar.com";
    setCorsFilterFeature(true, ImmutableList.<String>of());
    HttpClient client = client();
    // preflight request
    HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("server/ha/state", "GET", shouldAllowOrigin));
    List<String> accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN);
    assertEquals(accessControlAllowOrigin.size(), 1);
    assertEquals(accessControlAllowOrigin.get(0), "*", "Should allow GET requests made from " + shouldAllowOrigin);

    assertEquals(response.getHeaderLists().get(HEADER_AC_ALLOW_HEADERS).size(), 1);
    assertEquals(response.getHeaderLists().get(HEADER_AC_ALLOW_HEADERS).get(0), "x-csrf-token", "Should have asked and allowed x-csrf-token header from " + shouldAllowOrigin);
    assertOkayResponse(response, "");

    HttpUriRequest httpRequest = RequestBuilder.get(getBaseUriRest() + "server/ha/state")
            .addHeader("Origin", shouldAllowOrigin)
            .addHeader(HEADER_AC_REQUEST_METHOD, "GET")
            .build();
    response = HttpTool.execAndConsume(client, httpRequest);
    accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN);
    assertEquals(accessControlAllowOrigin.size(), 1);
    assertEquals(accessControlAllowOrigin.get(0), "*", "Should allow GET requests made from " + shouldAllowOrigin);
    assertOkayResponse(response, "\"MASTER\"");
}
 
Example 5
Source File: HazelcastNodeIntegrationTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Integration"})
public void testHazelcastRestInterface() throws URISyntaxException {
    hazelcastNode = app.createAndManageChild(EntitySpec.create(HazelcastNode.class));
    app.start(ImmutableList.of(testLocation));

    EntityAsserts.assertAttributeEqualsEventually(hazelcastNode, Startable.SERVICE_UP, true);
    EntityAsserts.assertAttributeEquals(hazelcastNode, HazelcastNode.NODE_PORT, 5701);

    String baseUri = String.format("http://%s:%d/hazelcast/rest/cluster", hazelcastNode.getAttribute(Attributes.HOSTNAME), hazelcastNode.getAttribute(HazelcastNode.NODE_PORT)); 
    HttpToolResponse response = HttpTool.execAndConsume(
            HttpTool.httpClientBuilder().build(),
            new HttpGet(baseUri));
    assertEquals(response.getResponseCode(), 200);
}
 
Example 6
Source File: ElasticSearchNodeIntegrationTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Integration"})
public void testDocumentCount() throws URISyntaxException {
    elasticSearchNode = app.createAndManageChild(EntitySpec.create(ElasticSearchNode.class));
    app.start(ImmutableList.of(testLocation));
    
    EntityAsserts.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, true);
    
    EntityAsserts.assertAttributeEquals(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 0);
    
    String baseUri = "http://" + elasticSearchNode.getAttribute(Attributes.HOSTNAME) + ":" + elasticSearchNode.getAttribute(Attributes.HTTP_PORT);
    
    HttpToolResponse pingResponse = HttpTool.execAndConsume(
            HttpTool.httpClientBuilder().build(),
            new HttpGet(baseUri));
    assertEquals(pingResponse.getResponseCode(), 200);
    
    String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}";
    
    HttpToolResponse putResponse = HttpTool.httpPut(
            HttpTool.httpClientBuilder()
                .port(elasticSearchNode.getAttribute(Attributes.HTTP_PORT))
                .build(), 
            new URI(baseUri + "/mydocuments/docs/1"), 
            ImmutableMap.<String, String>of(), 
            Strings.toByteArray(document)); 
    assertEquals(putResponse.getResponseCode(), 201);
    
    HttpToolResponse getResponse = HttpTool.execAndConsume(
            HttpTool.httpClientBuilder().build(),
            new HttpGet(baseUri + "/mydocuments/docs/1/_source"));
    assertEquals(getResponse.getResponseCode(), 200);
    assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");
    
    EntityAsserts.assertAttributeEqualsEventually(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 1);
}
 
Example 7
Source File: BrooklynWebServerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyHttp() throws Exception {
    webServer = new BrooklynWebServer(newManagementContext(brooklynProperties));
    webServer.skipSecurity();
    try {
        webServer.start();

        HttpToolResponse response = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(), new HttpGet(webServer.getRootUrl()));
        assertEquals(response.getResponseCode(), 200);
    } finally {
        webServer.stop();
    }
}
 
Example 8
Source File: BrooklynWebServerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySecurityInitialized() throws Exception {
    webServer = new BrooklynWebServer(newManagementContext(brooklynProperties));
    webServer.start();
    try {
        HttpToolResponse response = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(), new HttpGet(webServer.getRootUrl()));
        assertEquals(response.getResponseCode(), 401);
    } finally {
        webServer.stop();
    }
}
 
Example 9
Source File: BrooklynWebServerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider="keystorePaths")
public void verifyHttps(String keystoreUrl) throws Exception {
    Map<String,?> flags = ImmutableMap.<String,Object>builder()
            .put("httpsEnabled", true)
            .put("keystoreUrl", keystoreUrl)
            .put("keystorePassword", "password")
            .build();
    webServer = new BrooklynWebServer(flags, newManagementContext(brooklynProperties));
    webServer.skipSecurity().start();
    
    try {
        KeyStore keyStore = load("client.ks", "password");
        KeyStore trustStore = load("client.ts", "password");
        SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, "password", trustStore, (SecureRandom)null, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpToolResponse response = HttpTool.execAndConsume(
                HttpTool.httpClientBuilder()
                        .port(webServer.getActualPort())
                        .https(true)
                        .socketFactory(socketFactory)
                        .build(),
                new HttpGet(webServer.getRootUrl()));
        assertEquals(response.getResponseCode(), 200);
    } finally {
        webServer.stop();
    }
}
 
Example 10
Source File: BrooklynLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Integration")
public void testStartsWebServerWithoutAuthentication() throws Exception {
    launcher = newLauncherForTests(true)
            .start();
    String uri = launcher.getServerDetails().getWebServerUrl();
    
    HttpToolResponse response = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(), new HttpGet(uri));
    assertEquals(response.getResponseCode(), 200);
}
 
Example 11
Source File: CorsFilterLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void test1CorsIsEnabledOnOneOriginGET() throws IOException {
    final String shouldAllowOrigin = "http://foo.bar.com";
    final String thirdPartyOrigin = "http://foo.bar1.com";
    setCorsFilterFeature(true, ImmutableList.of(shouldAllowOrigin));

    HttpClient client = client();
    // preflight request
    HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("server/ha/state", "GET", shouldAllowOrigin));
    assertAcAllowOrigin(response, shouldAllowOrigin, "GET");
    assertOkayResponse(response, "");

    HttpUriRequest httpRequest = RequestBuilder.get(getBaseUriRest() + "server/ha/state")
            .addHeader("Origin", shouldAllowOrigin)
            .addHeader(HEADER_AC_REQUEST_METHOD, "GET")
            .build();
    response = HttpTool.execAndConsume(client, httpRequest);
    assertAcAllowOrigin(response, shouldAllowOrigin, "GET", false);
    assertOkayResponse(response, "\"MASTER\"");

    // preflight request
    response = HttpTool.execAndConsume(client, httpOptionsRequest("server/ha/state", "GET", thirdPartyOrigin));
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "");

    httpRequest = RequestBuilder.get(getBaseUriRest() + "server/ha/state")
            .addHeader("Origin", thirdPartyOrigin)
            .addHeader(HEADER_AC_REQUEST_METHOD, "GET")
            .build();
    response = HttpTool.execAndConsume(client, httpRequest);
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "\"MASTER\"");
}
 
Example 12
Source File: CorsFilterLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void test1CorsIsEnabledOnOneOriginPOST() throws IOException {
    final String shouldAllowOrigin = "http://foo.bar.com";
    final String thirdPartyOrigin = "http://foo.bar1.com";
    setCorsFilterFeature(true, ImmutableList.of(shouldAllowOrigin));
    HttpClient client = client();
    // preflight request
    HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", "POST", shouldAllowOrigin));
    assertAcAllowOrigin(response, shouldAllowOrigin, "POST");
    assertOkayResponse(response, "");
    
    response = HttpTool.httpPost(
            client, URI.create(getBaseUriRest() + "script/groovy"),
            ImmutableMap.<String,String>of(
                    "Origin", shouldAllowOrigin,
                    HttpHeaders.CONTENT_TYPE, "application/text"),
            "return 0;".getBytes());
    assertAcAllowOrigin(response, shouldAllowOrigin, "POST", false);
    assertOkayResponse(response, "{\"result\":\"0\"}");

    // preflight request
    response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", "POST", thirdPartyOrigin));
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "");

    response = HttpTool.httpPost(
            client, URI.create(getBaseUriRest() + "script/groovy"),
            ImmutableMap.<String,String>of(
                    "Origin", thirdPartyOrigin,
                    HttpHeaders.CONTENT_TYPE, "application/text"),
            "return 0;".getBytes());
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "{\"result\":\"0\"}");
}
 
Example 13
Source File: CorsFilterLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void test1CorsIsEnabledOnAllDomainsByDefaultPOST() throws IOException {
    final String shouldAllowOrigin = "http://foo.bar.com";
    BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY);
    BrooklynRestApiLauncher apiLauncher = baseLauncher().withoutJsgui();
    // In this test, management context has no value set for Allowed Origins.
    ManagementContext mgmt = LocalManagementContextForTests.builder(true)
            .useAdditionalProperties(MutableMap.<String, Object>of(
                    BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY, true)
            ).build();
    apiLauncher.managementContext(mgmt);
    useServerForTest(apiLauncher.start());
    HttpClient client = client();
    // preflight request
    HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", "POST", shouldAllowOrigin));
    List<String> accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN);
    assertEquals(accessControlAllowOrigin.size(), 1);
    assertEquals(accessControlAllowOrigin.get(0), "*", "Should allow POST requests made from " + shouldAllowOrigin);
    assertEquals(response.getHeaderLists().get(HEADER_AC_ALLOW_HEADERS).size(), 1);
    assertEquals(response.getHeaderLists().get(HEADER_AC_ALLOW_HEADERS).get(0), "x-csrf-token", "Should have asked and allowed x-csrf-token header from " + shouldAllowOrigin);
    assertOkayResponse(response, "");
    
    response = HttpTool.httpPost(
            client, URI.create(getBaseUriRest() + "script/groovy"),
            ImmutableMap.<String,String>of(
                    "Origin", shouldAllowOrigin,
                    HttpHeaders.CONTENT_TYPE, "application/text"),
            "return 0;".getBytes());
    accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN);
    assertEquals(accessControlAllowOrigin.size(), 1);
    assertEquals(accessControlAllowOrigin.get(0), "*", "Should allow GET requests made from " + shouldAllowOrigin);
    assertOkayResponse(response, "{\"result\":\"0\"}");
}
 
Example 14
Source File: CorsFilterLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void test2CorsIsDisabled() throws IOException {
    BrooklynFeatureEnablement.disable(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY);
    final String shouldAllowOrigin = "http://foo.bar.com";
    setCorsFilterFeature(false, null);

    HttpClient client = client();
    HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("server/ha/state", "GET", shouldAllowOrigin));
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "");

    response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", shouldAllowOrigin, "POST"));
    assertAcNotAllowOrigin(response);
    assertOkayResponse(response, "");
}
 
Example 15
Source File: ElasticSearchClusterIntegrationTest.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Test(groups = {"Integration"})
public void testPutAndGet() throws URISyntaxException {
    elasticSearchCluster = app.createAndManageChild(EntitySpec.create(ElasticSearchCluster.class)
            .configure(DynamicCluster.INITIAL_SIZE, 3));
    app.start(ImmutableList.of(testLocation));
    
    EntityAsserts.assertAttributeEqualsEventually(elasticSearchCluster, Startable.SERVICE_UP, true);
    assertEquals(elasticSearchCluster.getMembers().size(), 3);
    assertEquals(clusterDocumentCount(), 0);
    
    ElasticSearchNode anyNode = (ElasticSearchNode)elasticSearchCluster.getMembers().iterator().next();
    
    String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}";
    
    String putBaseUri = "http://" + anyNode.getAttribute(Attributes.HOSTNAME) + ":" + anyNode.getAttribute(Attributes.HTTP_PORT);
    
    HttpToolResponse putResponse = HttpTool.httpPut(
            HttpTool.httpClientBuilder()
                .port(anyNode.getAttribute(Attributes.HTTP_PORT))
                .build(), 
            new URI(putBaseUri + "/mydocuments/docs/1"), 
            ImmutableMap.<String, String>of(), 
            Strings.toByteArray(document)); 
    assertEquals(putResponse.getResponseCode(), 201);
    
    for (Entity entity : elasticSearchCluster.getMembers()) {
        ElasticSearchNode node = (ElasticSearchNode)entity;
        String getBaseUri = "http://" + node.getAttribute(Attributes.HOSTNAME) + ":" + node.getAttribute(Attributes.HTTP_PORT);
        HttpToolResponse getResponse = HttpTool.execAndConsume(
                HttpTool.httpClientBuilder().build(),
                new HttpGet(getBaseUri + "/mydocuments/docs/1/_source"));
        assertEquals(getResponse.getResponseCode(), 200);
        assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");
    }
    Asserts.succeedsEventually(new Runnable() {
        @Override
        public void run() {
            int count = clusterDocumentCount();
            assertTrue(count >= 1, "count="+count);
            LOG.debug("Document count is {}", count);
        }});
}