Search in sources :

Example 16 with ChannelBufferInputStream

use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.

the class PreviewHttpHandler method getTracersData.

@POST
@Path("/previews/{preview-id}/tracers")
public void getTracersData(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    ApplicationId application = namespace.app(previewId);
    Map<String, List<String>> previewRequest;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
        previewRequest = GSON.fromJson(reader, STRING_LIST_MAP_TYPE);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Request body is invalid json: " + e.getMessage());
    }
    if (previewRequest == null) {
        throw new BadRequestException("The body is not provided.");
    }
    List<String> tracerNames = previewRequest.get("tracers");
    if (tracerNames == null || tracerNames.isEmpty()) {
        throw new BadRequestException("Tracer names cannot be empty.");
    }
    Map<String, Map<String, List<JsonElement>>> result = new HashMap<>();
    for (String tracerName : tracerNames) {
        result.put(tracerName, previewManager.getRunner(application).getData(tracerName));
    }
    responder.sendString(HttpResponseStatus.OK, GSON.toJson(result));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) LogReader(co.cask.cdap.logging.read.LogReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BadRequestException(co.cask.cdap.common.BadRequestException) List(java.util.List) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashMap(java.util.HashMap) Map(java.util.Map) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 17 with ChannelBufferInputStream

use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.

the class PreviewHttpHandler method start.

@POST
@Path("/previews")
public void start(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    AppRequest appRequest;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
        appRequest = GSON.fromJson(reader, AppRequest.class);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Request body is invalid json: " + e.getMessage());
    }
    responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewManager.start(namespace, appRequest)));
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) LogReader(co.cask.cdap.logging.read.LogReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) AppRequest(co.cask.cdap.proto.artifact.AppRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 18 with ChannelBufferInputStream

use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.

the class AbstractAppFabricHttpHandler method parseBody.

@Nullable
protected <T> T parseBody(HttpRequest request, Type type) throws IllegalArgumentException, JsonSyntaxException {
    ChannelBuffer content = request.getContent();
    if (!content.readable()) {
        return null;
    }
    Reader reader = new InputStreamReader(new ChannelBufferInputStream(content), Charsets.UTF_8);
    try {
        return GSON.fromJson(reader, type);
    } catch (RuntimeException e) {
        LOG.info("Failed to parse body on {} as {}", request.getUri(), type, e);
        throw e;
    } finally {
        Closeables.closeQuietly(reader);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Nullable(javax.annotation.Nullable)

Example 19 with ChannelBufferInputStream

use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.

the class Event method fromJson.

public static Event fromJson(ByteBuffer input) {
    Event event = GSON.fromJson(new InputStreamReader(new ChannelBufferInputStream(ChannelBuffers.wrappedBuffer(input))), Event.class);
    Preconditions.checkNotNull(event.getTime(), "Time cannot be null.");
    Preconditions.checkNotNull(event.getUserId(), "User ID cannot be null.");
    Preconditions.checkNotNull(event.getUrl(), "URL cannot be null.");
    return event;
}
Also used : InputStreamReader(java.io.InputStreamReader) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream)

Example 20 with ChannelBufferInputStream

use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.

the class MetadataHttpHandler method readMetadata.

private Map<String, String> readMetadata(HttpRequest request) throws BadRequestException {
    ChannelBuffer content = request.getContent();
    if (!content.readable()) {
        throw new BadRequestException("Unable to read metadata properties from the request.");
    }
    Map<String, String> metadata;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(content), Charsets.UTF_8)) {
        metadata = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
    } catch (IOException e) {
        throw new BadRequestException("Unable to read metadata properties from the request.", e);
    }
    if (metadata == null) {
        throw new BadRequestException("Null metadata was read from the request");
    }
    return metadata;
}
Also used : InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)25 BadRequestException (co.cask.cdap.common.BadRequestException)15 InputStreamReader (java.io.InputStreamReader)15 Reader (java.io.Reader)14 IOException (java.io.IOException)12 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)9 Path (javax.ws.rs.Path)8 POST (javax.ws.rs.POST)6 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)4 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 NamespaceId (co.cask.cdap.proto.id.NamespaceId)4 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ExploreException (co.cask.cdap.explore.service.ExploreException)3 QueryHandle (co.cask.cdap.proto.QueryHandle)3 JsonObject (com.google.gson.JsonObject)3 InputStream (java.io.InputStream)3 SQLException (java.sql.SQLException)3 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)3 GenericRecord (org.apache.avro.generic.GenericRecord)3