Search in sources :

Example 1 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ArtifactHttpHandlerTest method callPluginMethod.

private co.cask.common.http.HttpResponse callPluginMethod(Id.Artifact plugins3Id, String pluginType, String pluginName, String pluginMethod, String body, ArtifactScope scope, int expectedResponseCode) throws URISyntaxException, IOException {
    URL endpoint = getEndPoint(String.format("%s/namespaces/%s/artifacts/%s/versions/%s/plugintypes/%s/plugins/%s/methods/%s?scope=%s", Constants.Gateway.API_VERSION_3, plugins3Id.getNamespace().getId(), plugins3Id.getName(), plugins3Id.getVersion().getVersion(), pluginType, pluginName, pluginMethod, scope.name())).toURL();
    HttpRequest request = HttpRequest.post(endpoint).withBody(body).build();
    co.cask.common.http.HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(expectedResponseCode, response.getResponseCode());
    return response;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) URL(java.net.URL)

Example 2 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class AuthorizationTest method testAddDropPartitions.

@Test
public void testAddDropPartitions() throws Exception {
    createAuthNamespace();
    ApplicationId appId = AUTH_NAMESPACE.app(PartitionTestApp.class.getSimpleName());
    DatasetId datasetId = AUTH_NAMESPACE.dataset(PartitionTestApp.PFS_NAME);
    Map<EntityId, Set<Action>> neededPrivileges = ImmutableMap.<EntityId, Set<Action>>builder().put(appId, EnumSet.of(Action.ADMIN)).put(AUTH_NAMESPACE.artifact(PartitionTestApp.class.getSimpleName(), "1.0-SNAPSHOT"), EnumSet.of(Action.ADMIN)).put(datasetId, EnumSet.of(Action.ADMIN)).put(AUTH_NAMESPACE.datasetType(PartitionedFileSet.class.getName()), EnumSet.of(Action.ADMIN)).build();
    setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
    ProgramId programId = appId.program(ProgramType.SERVICE, PartitionTestApp.PFS_SERVICE_NAME);
    grantAndAssertSuccess(programId, BOB, EnumSet.of(Action.EXECUTE));
    cleanUpEntities.add(programId);
    grantAndAssertSuccess(datasetId, BOB, EnumSet.of(Action.READ));
    cleanUpEntities.add(datasetId);
    ApplicationManager appMgr = deployApplication(AUTH_NAMESPACE, PartitionTestApp.class);
    SecurityRequestContext.setUserId(BOB.getName());
    String partition = "p1";
    String subPartition = "1";
    String text = "some random text for pfs";
    ServiceManager pfsService = appMgr.getServiceManager(PartitionTestApp.PFS_SERVICE_NAME);
    pfsService.start();
    pfsService.waitForRun(ProgramRunStatus.RUNNING, 1, TimeUnit.MINUTES);
    URL pfsURL = pfsService.getServiceURL();
    String apiPath = String.format("partitions/%s/subpartitions/%s", partition, subPartition);
    URL url = new URL(pfsURL, apiPath);
    HttpRequest request;
    HttpResponse response;
    try {
        request = HttpRequest.post(url).withBody(text).build();
        response = HttpRequests.execute(request);
        // should fail because bob does not have write privileges on the dataset
        Assert.assertEquals(500, response.getResponseCode());
    } finally {
        pfsService.stop();
        pfsService.waitForRun(ProgramRunStatus.KILLED, 1, TimeUnit.MINUTES);
    }
    // grant read and write on dataset and restart
    grantAndAssertSuccess(datasetId, BOB, EnumSet.of(Action.WRITE, Action.READ));
    pfsService.start();
    pfsService.waitForRun(ProgramRunStatus.RUNNING, 1, TimeUnit.MINUTES);
    pfsURL = pfsService.getServiceURL();
    url = new URL(pfsURL, apiPath);
    try {
        request = HttpRequest.post(url).withBody(text).build();
        response = HttpRequests.execute(request);
        // should succeed now because bob was granted write privileges on the dataset
        Assert.assertEquals(200, response.getResponseCode());
        // make sure that the partition was added
        request = HttpRequest.get(url).build();
        response = HttpRequests.execute(request);
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals(text, response.getResponseBodyAsString());
        // drop the partition
        request = HttpRequest.delete(url).build();
        response = HttpRequests.execute(request);
        Assert.assertEquals(200, response.getResponseCode());
    } finally {
        pfsService.stop();
        pfsService.waitForRuns(ProgramRunStatus.KILLED, 2, 1, TimeUnit.MINUTES);
        SecurityRequestContext.setUserId(ALICE.getName());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) PrivilegedAction(java.security.PrivilegedAction) Action(co.cask.cdap.proto.security.Action) ApplicationManager(co.cask.cdap.test.ApplicationManager) EnumSet(java.util.EnumSet) Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) PartitionedFileSet(co.cask.cdap.api.dataset.lib.PartitionedFileSet) HttpResponse(co.cask.common.http.HttpResponse) PartitionedFileSet(co.cask.cdap.api.dataset.lib.PartitionedFileSet) ProgramId(co.cask.cdap.proto.id.ProgramId) URL(java.net.URL) DatasetId(co.cask.cdap.proto.id.DatasetId) EntityId(co.cask.cdap.proto.id.EntityId) ServiceManager(co.cask.cdap.test.ServiceManager) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 3 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class RemoteNotificationFeedManager method createFeed.

@Override
public boolean createFeed(NotificationFeedInfo feed) throws NotificationFeedException {
    String path = String.format("namespaces/%s/feeds/categories/%s/names/%s", feed.getNamespace(), feed.getCategory(), feed.getFeed());
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, path).withBody(GSON.toJson(feed)).build();
    HttpResponse response = execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return true;
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        return false;
    }
    throw new NotificationFeedException("Cannot create notification feed. Reason: " + response);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) HttpResponse(co.cask.common.http.HttpResponse)

Example 4 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class RemoteUGIProvider method executeRequest.

private HttpResponse executeRequest(ImpersonationRequest impersonationRequest) throws IOException {
    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(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException)

Example 5 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class StreamHandlerTest method getNumProcessed.

private long getNumProcessed(StreamId streamId) throws Exception {
    String path = String.format("/v3/metrics/query?metric=system.collect.events&tag=namespace:%s&tag=stream:%s&aggregate=true", streamId.getNamespace(), streamId.getEntityName());
    HttpRequest request = HttpRequest.post(getEndPoint(path).toURL()).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());
    return getNumEventsFromResponse(response.getResponseBodyAsString());
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse)

Aggregations

HttpRequest (co.cask.common.http.HttpRequest)100 URL (java.net.URL)78 HttpResponse (co.cask.common.http.HttpResponse)71 Test (org.junit.Test)23 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)13 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 BadRequestException (co.cask.cdap.common.BadRequestException)10 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 ServiceManager (co.cask.cdap.test.ServiceManager)9 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 TypeToken (com.google.gson.reflect.TypeToken)6 IOException (java.io.IOException)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)5 TypeToken (com.google.common.reflect.TypeToken)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)4 SparkManager (co.cask.cdap.test.SparkManager)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)3