Search in sources :

Example 66 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class DynamicPluginServiceTestRun method testNamespaceIsolation.

@Test
public void testNamespaceIsolation() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("value", "x");
    URL url = baseURI.resolve(String.format("plugins/%s/apply", ConstantFunction.NAME)).toURL();
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(properties)).addHeader(DynamicPluginServiceApp.NAMESPACE_HEADER, "ghost").build();
    HttpResponse response = executeHttp(request);
    Assert.assertEquals(404, response.getResponseCode());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HashMap(java.util.HashMap) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 67 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class AdminAppTestRun method testAdminService.

@Test
public void testAdminService() throws Exception {
    // Start the service
    ServiceManager serviceManager = appManager.getServiceManager(AdminApp.SERVICE_NAME).start();
    String namespaceX = "x";
    try {
        URI serviceURI = serviceManager.getServiceURL(10, TimeUnit.SECONDS).toURI();
        // dataset nn should not exist
        HttpResponse response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        // create nn as a table
        response = executeHttp(HttpRequest.put(serviceURI.resolve("create/nn/table").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // now nn should exist
        response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("true", response.getResponseBodyAsString());
        // create it again as a fileset -> should fail with conflict
        response = executeHttp(HttpRequest.put(serviceURI.resolve("create/nn/fileSet").toURL()).build());
        Assert.assertEquals(409, response.getResponseCode());
        // get the type for xx -> not found
        response = executeHttp(HttpRequest.get(serviceURI.resolve("type/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get the type for nn -> table
        response = executeHttp(HttpRequest.get(serviceURI.resolve("type/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("table", response.getResponseBodyAsString());
        // update xx's properties -> should get not-found
        Map<String, String> nnProps = TableProperties.builder().setTTL(1000L).build().getProperties();
        response = executeHttp(HttpRequest.put(serviceURI.resolve("update/xx").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(404, response.getResponseCode());
        // update nn's properties
        response = executeHttp(HttpRequest.put(serviceURI.resolve("update/nn").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(200, response.getResponseCode());
        // get properties for xx -> not found
        response = executeHttp(HttpRequest.get(serviceURI.resolve("props/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get properties for nn and validate
        response = executeHttp(HttpRequest.get(serviceURI.resolve("props/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Map<String, String> returnedProps = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Map<String, String>>() {
        }.getType());
        Assert.assertEquals(nnProps, returnedProps);
        // write some data to the table
        DataSetManager<Table> nnManager = getDataset("nn");
        nnManager.get().put(new Put("x", "y", "z"));
        nnManager.flush();
        // in a new tx, validate that data is in table
        Assert.assertFalse(nnManager.get().get(new Get("x")).isEmpty());
        Assert.assertEquals("z", nnManager.get().get(new Get("x", "y")).getString("y"));
        nnManager.flush();
        // truncate xx -> not found
        response = executeHttp(HttpRequest.post(serviceURI.resolve("truncate/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // truncate nn
        response = executeHttp(HttpRequest.post(serviceURI.resolve("truncate/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // validate table is empty
        Assert.assertTrue(nnManager.get().get(new Get("x")).isEmpty());
        nnManager.flush();
        // delete nn
        response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // delete again -> not found
        response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // delete xx which never existed -> not found
        response = executeHttp(HttpRequest.delete(serviceURI.resolve("delete/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // exists should now return false for nn
        response = executeHttp(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        Assert.assertNull(getDataset("nn").get());
        // test Admin.namespaceExists()
        HttpRequest request = HttpRequest.get(serviceURI.resolve("namespaces/y").toURL()).build();
        response = executeHttp(request);
        Assert.assertEquals(404, response.getResponseCode());
        // test Admin.getNamespaceSummary()
        NamespaceMeta namespaceXMeta = new NamespaceMeta.Builder().setName(namespaceX).setGeneration(10L).build();
        getNamespaceAdmin().create(namespaceXMeta);
        request = HttpRequest.get(serviceURI.resolve("namespaces/" + namespaceX).toURL()).build();
        response = executeHttp(request);
        NamespaceSummary namespaceSummary = GSON.fromJson(response.getResponseBodyAsString(), NamespaceSummary.class);
        NamespaceSummary expectedX = new NamespaceSummary(namespaceXMeta.getName(), namespaceXMeta.getDescription(), namespaceXMeta.getGeneration());
        Assert.assertEquals(expectedX, namespaceSummary);
        // test ArtifactManager.listArtifacts()
        ArtifactId pluginArtifactId = new NamespaceId(namespaceX).artifact("r1", "1.0.0");
        // add a plugin artifact to namespace X
        addPluginArtifact(pluginArtifactId, ADMIN_APP_ARTIFACT, DummyPlugin.class);
        // no plugins should be listed in the default namespace, but the app artifact should
        request = HttpRequest.get(serviceURI.resolve("namespaces/default/plugins").toURL()).build();
        response = executeHttp(request);
        Assert.assertEquals(200, response.getResponseCode());
        Type setType = new TypeToken<Set<ArtifactSummary>>() {
        }.getType();
        Assert.assertEquals(Collections.singleton(ADMIN_ARTIFACT_SUMMARY), GSON.fromJson(response.getResponseBodyAsString(), setType));
        // the plugin should be listed in namespace X
        request = HttpRequest.get(serviceURI.resolve("namespaces/x/plugins").toURL()).build();
        response = executeHttp(request);
        Assert.assertEquals(200, response.getResponseCode());
        ArtifactSummary expected = new ArtifactSummary(pluginArtifactId.getArtifact(), pluginArtifactId.getVersion());
        Assert.assertEquals(Collections.singleton(expected), GSON.fromJson(response.getResponseBodyAsString(), setType));
    } finally {
        serviceManager.stop();
        if (getNamespaceAdmin().exists(new NamespaceId(namespaceX))) {
            getNamespaceAdmin().delete(new NamespaceId(namespaceX));
        }
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Table(io.cdap.cdap.api.dataset.table.Table) Set(java.util.Set) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) HttpResponse(io.cdap.common.http.HttpResponse) URI(java.net.URI) Put(io.cdap.cdap.api.dataset.table.Put) Type(java.lang.reflect.Type) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ServiceManager(io.cdap.cdap.test.ServiceManager) TypeToken(io.cdap.cdap.internal.guava.reflect.TypeToken) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) Get(io.cdap.cdap.api.dataset.table.Get) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 68 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class RemoteLogsFetcher method execute.

private void execute(String path, File file) throws IOException, UnauthorizedException {
    try (FileChannel channel = new FileOutputStream(file, false).getChannel()) {
        URL url = remoteClient.resolve(path);
        HttpRequest request = HttpRequest.get(url).withContentConsumer(new HttpContentConsumer() {

            @Override
            public boolean onReceived(ByteBuffer buffer) {
                try {
                    channel.write(buffer);
                } catch (IOException e) {
                    LOG.error("Failed write to file {}", file);
                    return false;
                }
                return true;
            }

            @Override
            public void onFinished() {
                Closeables.closeQuietly(channel);
            }
        }).build();
        remoteClient.executeStreamingRequest(request);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) HttpContentConsumer(io.cdap.common.http.HttpContentConsumer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL)

Example 69 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class RemoteUGIProvider method executeRequest.

private HttpResponse executeRequest(ImpersonationRequest impersonationRequest) throws IOException, UnauthorizedException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.POST, "impersonation/credentials").withBody(GSON.toJson(impersonationRequest)).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return response;
    }
    throw new IOException(String.format("%s Response: %s.", createErrorMessage(request.getURL()), response));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 70 with HttpRequest

use of io.cdap.common.http.HttpRequest in project cdap by caskdata.

the class RemoteAccessEnforcer method doEnforce.

private EnforcementResponse doEnforce(AuthorizationPrivilege authorizationPrivilege) throws IOException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.POST, "enforce").withBody(GSON.toJson(authorizationPrivilege)).build();
    LOG.trace("Remotely enforcing on authorization privilege {}", authorizationPrivilege);
    try {
        HttpResponse response = remoteClient.execute(request);
        if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
            return new EnforcementResponse(true, null);
        }
        return new EnforcementResponse(false, new IOException(String.format("Failed to enforce with code %d: %s", response.getResponseCode(), response.getResponseBodyAsString())));
    } catch (UnauthorizedException e) {
        return new EnforcementResponse(false, e);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(io.cdap.common.http.HttpResponse) AccessIOException(io.cdap.cdap.security.spi.AccessIOException) IOException(java.io.IOException)

Aggregations

HttpRequest (io.cdap.common.http.HttpRequest)124 HttpResponse (io.cdap.common.http.HttpResponse)92 URL (java.net.URL)81 Test (org.junit.Test)33 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 IOException (java.io.IOException)14 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)13 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)10 BadRequestException (io.cdap.cdap.common.BadRequestException)9 NotFoundException (io.cdap.cdap.common.NotFoundException)8 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)5 ServiceManager (io.cdap.cdap.test.ServiceManager)5 TypeToken (com.google.gson.reflect.TypeToken)4 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)4 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)4 TopicId (io.cdap.cdap.proto.id.TopicId)4 ApplicationManager (io.cdap.cdap.test.ApplicationManager)4 ByteBuffer (java.nio.ByteBuffer)4