Search in sources :

Example 6 with StreamNotFoundException

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);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) URL(java.net.URL)

Example 7 with StreamNotFoundException

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);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) URL(java.net.URL)

Example 8 with StreamNotFoundException

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());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) Test(org.junit.Test)

Example 9 with StreamNotFoundException

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);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) URL(java.net.URL)

Example 10 with StreamNotFoundException

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);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException)

Aggregations

StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)11 HttpResponse (co.cask.common.http.HttpResponse)8 URL (java.net.URL)8 HttpRequest (co.cask.common.http.HttpRequest)5 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)2 BadRequestException (co.cask.cdap.common.BadRequestException)2 StreamId (co.cask.cdap.proto.id.StreamId)2 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)2 IOException (java.io.IOException)2 NotFoundException (co.cask.cdap.common.NotFoundException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)1 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)1 JsonReader (com.google.gson.stream.JsonReader)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 Test (org.junit.Test)1