use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.
the class ConversionHelpers method getInstanceConfiguration.
static DatasetInstanceConfiguration getInstanceConfiguration(HttpRequest request) throws BadRequestException {
Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8);
try {
DatasetInstanceConfiguration config = GSON.fromJson(reader, DatasetInstanceConfiguration.class);
Preconditions.checkNotNull(config.getTypeName(), "The typeName must be specified.");
return config;
} catch (JsonSyntaxException | NullPointerException e) {
throw new BadRequestException(e.getMessage());
}
}
use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method readScheduleDetailBody.
private ScheduleDetail readScheduleDetailBody(HttpRequest request, String scheduleName, boolean isUpdate, Function<JsonElement, ScheduleDetail> toScheduleDetail) throws BadRequestException, IOException {
// TODO: remove backward compatibility with ScheduleSpecification, use fromJson(ScheduleDetail.class)
JsonElement json;
try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
// The schedule spec in the request body does not contain the program information
json = 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;
if (((JsonObject) json).get("schedule") != null) {
// field only exists in legacy ScheduleSpec/UpdateDetail
try {
scheduleDetail = toScheduleDetail.apply(json);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Error parsing request body as a schedule " + (isUpdate ? "update details" : "specification") + " (in backward compatibility mode): " + e.getMessage());
} catch (IllegalArgumentException e) {
throw new BadRequestException(e);
}
} else {
try {
scheduleDetail = 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 org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.
the class ExploreExecutorHttpHandler method enableStream.
@POST
@Path("streams/{stream}/tables/{table}/enable")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void enableStream(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("stream") String streamName, @PathParam("table") final String tableName) throws Exception {
final StreamId streamId = new StreamId(namespace, streamName);
try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()))) {
final FormatSpecification format = GSON.fromJson(reader, FormatSpecification.class);
if (format == null) {
throw new BadRequestException("Expected format in the body");
}
QueryHandle handle = impersonator.doAs(streamId, new Callable<QueryHandle>() {
@Override
public QueryHandle call() throws Exception {
return exploreTableManager.enableStream(tableName, streamId, format);
}
});
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (UnsupportedTypeException e) {
LOG.error("Exception while generating create statement for stream {}", streamName, e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
}
}
use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.
the class ExploreExecutorHttpHandler method doDropPartition.
private void doDropPartition(HttpRequest request, HttpResponder responder, DatasetId datasetId) {
Dataset dataset;
try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
dataset = datasetInstantiator.getDataset(datasetId);
if (dataset == null) {
responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetId);
return;
}
} catch (IOException e) {
String classNotFoundMessage = isClassNotFoundException(e);
if (classNotFoundMessage != null) {
JsonObject json = new JsonObject();
json.addProperty("handle", QueryHandle.NO_OP.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
return;
}
LOG.error("Exception instantiating dataset {}.", datasetId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Exception instantiating dataset " + datasetId);
return;
}
try {
if (!(dataset instanceof PartitionedFileSet)) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "not a partitioned dataset.");
return;
}
Partitioning partitioning = ((PartitionedFileSet) dataset).getPartitioning();
Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()));
Map<String, String> properties = GSON.fromJson(reader, new TypeToken<Map<String, String>>() {
}.getType());
PartitionKey partitionKey;
try {
partitionKey = PartitionedFileSetArguments.getOutputPartitionKey(properties, partitioning);
} catch (Exception e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "invalid partition key: " + e.getMessage());
return;
}
if (partitionKey == null) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "no partition key was given.");
return;
}
QueryHandle handle = exploreTableManager.dropPartition(datasetId, properties, partitionKey);
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (Throwable e) {
LOG.error("Got exception:", e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
use of org.jboss.netty.buffer.ChannelBufferInputStream in project cdap by caskdata.
the class AppLifecycleHttpHandler method updateApp.
/**
* Updates an existing application.
*/
@POST
@Path("/apps/{app-id}/update")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void updateApp(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId, @PathParam("app-id") final String appName) throws NotFoundException, BadRequestException, UnauthorizedException, IOException {
ApplicationId appId = validateApplicationId(namespaceId, appName);
AppRequest appRequest;
try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
appRequest = GSON.fromJson(reader, AppRequest.class);
} catch (IOException e) {
LOG.error("Error reading request to update app {} in namespace {}.", appName, namespaceId, e);
throw new IOException("Error reading request body.");
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
}
try {
applicationLifecycleService.updateApp(appId, appRequest, createProgramTerminator());
responder.sendString(HttpResponseStatus.OK, "Update complete.");
} catch (InvalidArtifactException e) {
throw new BadRequestException(e.getMessage());
} catch (ConflictException e) {
responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
} catch (NotFoundException | UnauthorizedException e) {
throw e;
} catch (Exception e) {
// this is the same behavior as deploy app pipeline, but this is bad behavior. Error handling needs improvement.
LOG.error("Deploy failure", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
}
}
Aggregations