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));
}
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)));
}
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);
}
}
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;
}
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;
}
Aggregations