use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class DatasetClient method update.
/**
* Updates the properties of a dataset.
*
* @param instance the dataset to update
* @param properties properties to set
* @throws NotFoundException if the dataset is not found
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void update(DatasetId instance, Map<String, String> properties) throws NotFoundException, IOException, UnauthenticatedException, ConflictException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(instance);
} else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new ConflictException(response.getResponseBodyAsString());
}
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class DatasetClient method getProperties.
/**
* Retrieve the properties with which a dataset was created or updated.
* @param instance the dataset instance
* @return the properties as a map
*/
public Map<String, String> getProperties(DatasetId instance) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = restClient.execute(request, config.getAccessToken());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(instance);
}
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
}).getResponseObject();
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class FileFetcherHttpHandlerInternalTest method download.
private HttpResponse download(File src, Location dst) throws IOException {
// Make a request to download the source file.
URL url = new URL(String.format("%s/v3Internal/location/%s", baseURL, src.toURI().getPath()));
OutputStream outputStream = dst.getOutputStream();
HttpRequest request = HttpRequest.builder(HttpMethod.GET, url).withContentConsumer(new HttpContentConsumer() {
@Override
public boolean onReceived(ByteBuffer chunk) {
try {
byte[] bytes = new byte[chunk.remaining()];
chunk.get(bytes, 0, bytes.length);
outputStream.write(bytes);
} catch (IOException e) {
LOG.error("Failed to write to {}", dst.toURI());
return false;
}
return true;
}
@Override
public void onFinished() {
try {
outputStream.close();
} catch (Exception e) {
LOG.error("Failed to close {}", dst.toURI());
}
}
}).build();
HttpResponse httpResponse = HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
httpResponse.consumeContent();
return httpResponse;
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ScheduleClient method doUpdate.
private void doUpdate(ScheduleId scheduleId, String json) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException, AlreadyExistsException {
String path = String.format("apps/%s/versions/%s/schedules/%s/update", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
URL url = config.resolveNamespacedURLV3(scheduleId.getNamespaceId(), path);
HttpRequest request = HttpRequest.post(url).withBody(json).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
throw new NotFoundException(scheduleId);
}
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class TestFrameworkTestRun method testByteCodeClassLoader.
@Category(XSlowTests.class)
@Test
public void testByteCodeClassLoader() throws Exception {
// This test verify bytecode generated classes ClassLoading
ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
ServiceManager serviceManager = appManager.getServiceManager("RecordHandler").start();
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Increment record
URL url = new URL(serviceURL, "increment/public");
for (int i = 0; i < 10; i++) {
HttpResponse response = executeHttp(HttpRequest.post(url).build());
Assert.assertEquals(200, response.getResponseCode());
}
// Query record
url = new URL(serviceURL, "query?type=public");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
long count = Long.parseLong(response.getResponseBodyAsString());
serviceManager.stop();
// Verify the record count with dataset
DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace.dataset("records"));
KeyValueTable records = recordsManager.get();
Assert.assertEquals(count, Bytes.toLong(records.read("PUBLIC")));
}
Aggregations