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