Search in sources :

Example 6 with HttpResponse

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

the class AbstractMetadataClient method makeRequest.

// makes a request and throws BadRequestException or NotFoundException, as appropriate
private HttpResponse makeRequest(Id.NamespacedId namespacedId, String path, HttpMethod httpMethod, @Nullable String body) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL url = resolve(namespacedId.getNamespace().toEntityId(), path);
    HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url);
    if (body != null) {
        builder.withBody(body);
    }
    HttpResponse response = execute(builder.build(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(namespacedId.toEntityId());
    }
    return response;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 7 with HttpResponse

use of co.cask.common.http.HttpResponse 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 8 with HttpResponse

use of co.cask.common.http.HttpResponse 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 9 with HttpResponse

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

the class RemoteNotificationFeedManager method listFeeds.

@Override
public List<NotificationFeedInfo> listFeeds(NamespaceId namespace) throws NotificationFeedException {
    String path = String.format("namespaces/%s/feeds", namespace.getNamespace());
    HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.GET, path).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        ObjectResponse<List<NotificationFeedInfo>> r = ObjectResponse.fromJsonBody(response, new TypeToken<List<NotificationFeedInfo>>() {
        }.getType());
        return r.getResponseObject();
    }
    throw new NotificationFeedException("Cannot list notification feeds. Reason: " + response);
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(co.cask.common.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) List(java.util.List)

Example 10 with HttpResponse

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

the class RemoteNotificationFeedManager method deleteFeed.

@Override
public void deleteFeed(NotificationFeedId feed) throws NotificationFeedNotFoundException, NotificationFeedException {
    String path = String.format("namespaces/%s/feeds/categories/%s/names/%s", feed.getNamespace(), feed.getCategory(), feed.getFeed());
    HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.DELETE, path).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotificationFeedNotFoundException(feed);
    } else if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new NotificationFeedException("Cannot delete notification feed. Reason: " + response);
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) HttpResponse(co.cask.common.http.HttpResponse)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)216 URL (java.net.URL)147 HttpRequest (co.cask.common.http.HttpRequest)80 NotFoundException (co.cask.cdap.common.NotFoundException)42 TypeToken (com.google.common.reflect.TypeToken)26 Test (org.junit.Test)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 IOException (java.io.IOException)16 ExploreException (co.cask.cdap.explore.service.ExploreException)13 ServiceManager (co.cask.cdap.test.ServiceManager)12 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 ApplicationManager (co.cask.cdap.test.ApplicationManager)10 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)8 HashMap (java.util.HashMap)7 List (java.util.List)7 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5