use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class MetadataHttpHandler method readArray.
private String[] readArray(FullHttpRequest request) throws BadRequestException {
ByteBuf content = request.content();
if (!content.isReadable()) {
throw new BadRequestException("Unable to read a list of tags from the request.");
}
List<String> toReturn;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(content), StandardCharsets.UTF_8)) {
toReturn = GSON.fromJson(reader, LIST_STRING_TYPE);
} catch (IOException e) {
throw new BadRequestException("Unable to read a list of tags from the request.", e);
}
if (toReturn == null) {
throw new BadRequestException("Null tags were read from the request.");
}
return toReturn.toArray(new String[toReturn.size()]);
}
use of io.netty.buffer.ByteBufInputStream in project alien4cloud by alien4cloud.
the class StompClientHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
StompFrame frame = (StompFrame) msg;
String destination = null;
if (frame.headers().get(StompHeaders.DESTINATION) != null) {
destination = frame.headers().get(StompHeaders.DESTINATION).toString();
}
if (log.isDebugEnabled()) {
log.debug("Received frame {} from topic {}", toString(frame), destination);
}
IStompCallback callback = null;
if (destination != null) {
callback = handlers.get(destination);
if (callback == null) {
throw new IllegalStateException("Received message for a topic that was never registered before");
}
}
switch(frame.command()) {
case CONNECTED:
connectFuture.setSuccess();
break;
case MESSAGE:
if (String.class == callback.getExpectedDataType()) {
callback.onData(frame.headers().get(StompHeaders.DESTINATION).toString(), frame.content().toString(Charset.forName("UTF-8")));
} else {
callback.onData(frame.headers().get(StompHeaders.DESTINATION).toString(), JsonUtil.readObject(new ByteBufInputStream(frame.content()), callback.getExpectedDataType()));
}
break;
case ERROR:
String frameText = toString(frame);
log.error("Received stomp error {} for topic {}", frameText, destination);
callback.onError(new StompErrorException("Stomp error for destination " + destination + " :\n" + frameText));
break;
default:
frameText = toString(frame);
log.error("Received unknown frame {} for topic {}", frameText, destination);
callback.onError(new StompUnknownCommandException("Unknown stomp command " + frame.command() + " from frame :\n" + frameText));
break;
}
}
use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactProperties.
@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = validateAndGetNamespace(namespaceId);
List<ArtifactPropertiesRequest> propertyRequests;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
}
List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
ArtifactId artifactId = validateAndGetArtifactId(requestNamespace, propertiesRequest.getName(), propertiesRequest.getVersion());
ArtifactDetail artifactDetail;
try {
artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
} catch (ArtifactNotFoundException e) {
continue;
}
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
for (String propertyKey : propertiesRequest.getProperties()) {
if (properties.containsKey(propertyKey)) {
filteredProperties.put(propertyKey, properties.get(propertyKey));
}
}
result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), propertiesRequest.getVersion(), propertiesRequest.getScope(), filteredProperties));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method readScheduleDetailBody.
private ScheduleDetail readScheduleDetailBody(FullHttpRequest request, String scheduleName) throws BadRequestException, IOException {
JsonElement json;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), Charsets.UTF_8)) {
// The schedule spec in the request body does not contain the program information
json = DECODE_GSON.fromJson(reader, JsonElement.class);
} catch (IOException e) {
throw new IOException("Error reading request body", e);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
}
if (!json.isJsonObject()) {
throw new BadRequestException("Expected a json object in the request body but received " + GSON.toJson(json));
}
ScheduleDetail scheduleDetail;
try {
scheduleDetail = DECODE_GSON.fromJson(json, ScheduleDetail.class);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Error parsing request body as a schedule specification: " + e.getMessage());
}
// If the schedule name is present in the request body, it should match the name in path params
if (scheduleDetail.getName() != null && !scheduleName.equals(scheduleDetail.getName())) {
throw new BadRequestException(String.format("Schedule name in the body of the request (%s) does not match the schedule name in the path parameter (%s)", scheduleDetail.getName(), scheduleName));
}
return scheduleDetail;
}
use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class AbstractAppFabricHttpHandler method parseBody.
@Nullable
protected <T> T parseBody(FullHttpRequest request, Type type) throws IllegalArgumentException, JsonSyntaxException {
ByteBuf content = request.content();
if (!content.isReadable()) {
return null;
}
Reader reader = new InputStreamReader(new ByteBufInputStream(content), StandardCharsets.UTF_8);
try {
return GSON.fromJson(reader, type);
} catch (RuntimeException e) {
LOG.info("Failed to parse body on {} as {}", request.uri(), type, e);
throw e;
} finally {
Closeables.closeQuietly(reader);
}
}
Aggregations