use of co.cask.cdap.common.StreamNotFoundException in project cdap by caskdata.
the class StreamClient method delete.
/**
* Deletes a stream.
*
* @param stream ID of the stream to truncate
* @throws IOException if a network error occurred
* @throws StreamNotFoundException if the stream with the specified name was not found
*/
public void delete(StreamId stream) throws IOException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s", stream.getStream()));
HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
}
use of co.cask.cdap.common.StreamNotFoundException in project cdap by caskdata.
the class StreamClient method getConfig.
/**
* Gets the configuration of a stream.
*
* @param stream ID of the stream
* @throws IOException if a network error occurred
* @throws StreamNotFoundException if the stream was not found
*/
public StreamProperties getConfig(StreamId stream) throws IOException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s", stream.getStream()));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
return GSON.fromJson(response.getResponseBodyAsString(Charsets.UTF_8), StreamProperties.class);
}
use of co.cask.cdap.common.StreamNotFoundException in project cdap by caskdata.
the class StreamClientTestRun method testDelete.
@Test
public void testDelete() throws Exception {
StreamId streamId = namespaceId.stream("testDelete");
streamClient.create(streamId);
// Send an event and get it back
String msg = "Test Delete";
streamClient.sendEvent(streamId, msg);
List<StreamEvent> events = Lists.newArrayList();
streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
Assert.assertEquals(1, events.size());
Assert.assertEquals(msg, Charsets.UTF_8.decode(events.get(0).getBody()).toString());
// Delete the stream
streamClient.delete(streamId);
// Try to get info, it should throw a StreamNotFoundException
try {
streamClient.getConfig(streamId);
Assert.fail();
} catch (StreamNotFoundException e) {
// Expected
}
// Try to get events, it should throw a StreamNotFoundException
try {
streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
Assert.fail();
} catch (StreamNotFoundException e) {
// Expected
}
// Create the stream again, it should returns empty events
streamClient.create(streamId);
events.clear();
streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
Assert.assertTrue(events.isEmpty());
}
use of co.cask.cdap.common.StreamNotFoundException in project cdap by caskdata.
the class StreamClient method sendBatch.
/**
* Sends a batch request to a stream batch endpoint.
*
* @param stream ID of the stream
* @param contentType content type of the data
* @param inputSupplier provides content for the batch request
* @throws IOException if a network error occurred
* @throws StreamNotFoundException if the stream with the specified ID was not found
*/
public void sendBatch(StreamId stream, String contentType, InputSupplier<? extends InputStream> inputSupplier) throws IOException, StreamNotFoundException, UnauthenticatedException {
URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s/batch", stream.getStream()));
Map<String, String> headers = ImmutableMap.of("Content-type", contentType);
HttpRequest request = HttpRequest.post(url).addHeaders(headers).withBody(inputSupplier).build();
HttpResponse response = restClient.upload(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
}
use of co.cask.cdap.common.StreamNotFoundException in project cdap by caskdata.
the class StreamClient method writeEvent.
/**
* Writes stream event using the given URL. The write maybe sync or async, depending on the URL.
*/
private void writeEvent(URL url, StreamId stream, String event) throws IOException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
HttpRequest request = HttpRequest.post(url).withBody(event).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
}
Aggregations