Search in sources :

Example 1 with MockResponder

use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.

the class DefaultStreamManager method send.

@Override
public void send(File file, String contentType) throws Exception {
    String path = String.format("/v3/namespaces/%s/streams/%s/batch", streamId.getNamespaceId(), streamId.getId());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
    request.setHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
    final MockResponder responder = new MockResponder();
    final BodyConsumer bodyConsumer = streamHandler.batch(request, responder, streamId.getNamespaceId(), streamId.getId());
    Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from stream batch load call should not be null");
    ByteStreams.readBytes(Files.newInputStreamSupplier(file), new ByteProcessor<BodyConsumer>() {

        @Override
        public boolean processBytes(byte[] buf, int off, int len) throws IOException {
            bodyConsumer.chunk(ChannelBuffers.wrappedBuffer(buf, off, len), responder);
            return true;
        }

        @Override
        public BodyConsumer getResult() {
            bodyConsumer.finished(responder);
            return bodyConsumer;
        }
    });
    Preconditions.checkState(HttpResponseStatus.OK.equals(responder.getStatus()), "Failed to load events to stream %s in batch", streamId);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) MockResponder(co.cask.cdap.internal.MockResponder) BodyConsumer(co.cask.http.BodyConsumer) IOException(java.io.IOException)

Example 2 with MockResponder

use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.

the class MapReduceRunnerTestBase method writeToStream.

protected void writeToStream(Id.Stream streamId, String body) throws IOException {
    String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
    ChannelBuffer content = ChannelBuffers.wrappedBuffer(ByteBuffer.wrap(Bytes.toBytes(body)));
    httpRequest.setContent(content);
    httpRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    MockResponder responder = new MockResponder();
    try {
        streamHandler.enqueue(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
    } catch (Exception e) {
        Throwables.propagateIfPossible(e, IOException.class);
        throw Throwables.propagate(e);
    }
    if (responder.getStatus() != HttpResponseStatus.OK) {
        throw new IOException("Failed to write to stream. Status = " + responder.getStatus());
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) MockResponder(co.cask.cdap.internal.MockResponder) IOException(java.io.IOException) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with MockResponder

use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.

the class DefaultStreamManager method getEvents.

private List<StreamEvent> getEvents(Id.Stream streamId, String startTime, String endTime, int limit) throws IOException {
    long start = TimeMathParser.parseTime(startTime, TimeUnit.MILLISECONDS);
    long end = TimeMathParser.parseTime(endTime, TimeUnit.MILLISECONDS);
    String path = String.format("/v3/namespaces/%s/streams/%s/events?start=%d&end=%d&limit=%d", streamId.getNamespaceId(), streamId.getId(), start, end, limit);
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
    MockResponder responder = new MockResponder();
    try {
        streamFetchHandler.fetch(httpRequest, responder, streamId.getNamespaceId(), streamId.getId(), startTime, endTime, limit);
    } catch (Exception e) {
        Throwables.propagateIfPossible(e, IOException.class);
        throw Throwables.propagate(e);
    }
    if (responder.getStatus() != HttpResponseStatus.OK) {
        throw new IOException("Failed to read from stream. Status = " + responder.getStatus());
    }
    return responder.decodeResponseContent(STREAM_EVENT_LIST_TYPE, GSON);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) MockResponder(co.cask.cdap.internal.MockResponder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with MockResponder

use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.

the class DefaultStreamManager method send.

@Override
public void send(Map<String, String> headers, ByteBuffer buffer) throws IOException {
    String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpRequest.setHeader(streamId.getId() + "." + entry.getKey(), entry.getValue());
    }
    ChannelBuffer content = ChannelBuffers.wrappedBuffer(buffer);
    httpRequest.setContent(content);
    httpRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    MockResponder responder = new MockResponder();
    try {
        streamHandler.enqueue(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
    } catch (Exception e) {
        Throwables.propagateIfPossible(e, IOException.class);
        throw Throwables.propagate(e);
    }
    if (responder.getStatus() != HttpResponseStatus.OK) {
        throw new IOException("Failed to write to stream. Status = " + responder.getStatus());
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) MockResponder(co.cask.cdap.internal.MockResponder) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 5 with MockResponder

use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.

the class DefaultStreamManager method createStream.

@Override
public void createStream() throws IOException {
    String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
    MockResponder responder = new MockResponder();
    try {
        streamHandler.create(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
    } catch (Exception e) {
        Throwables.propagateIfPossible(e, IOException.class);
        throw Throwables.propagate(e);
    }
    if (responder.getStatus() != HttpResponseStatus.OK) {
        throw new IOException("Failed to create stream. Status = " + responder.getStatus());
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) MockResponder(co.cask.cdap.internal.MockResponder) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

MockResponder (co.cask.cdap.internal.MockResponder)5 IOException (java.io.IOException)5 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)5 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)5 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 BodyConsumer (co.cask.http.BodyConsumer)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1