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;
}
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());
}
}
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);
}
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);
}
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);
}
}
Aggregations