use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class LoadPreferencesCommand method perform.
@SuppressWarnings("unchecked")
@Override
public void perform(Arguments arguments, PrintStream printStream) throws Exception {
String contentType = arguments.getOptional(ArgumentName.CONTENT_TYPE.toString(), "");
File file = new File(arguments.get(ArgumentName.LOCAL_FILE_PATH.toString()));
if (!file.isFile()) {
throw new IllegalArgumentException("Not a file: " + file);
}
Map<String, String> args = Maps.newHashMap();
try (FileReader reader = new FileReader(file)) {
if (contentType.equals("json")) {
args = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
} else {
throw new IllegalArgumentException("Unsupported file format. Only JSON format is supported");
}
} catch (JsonSyntaxException e) {
throw new BadRequestException(String.format("JSON syntax in file is invalid. Support only for string-to-string map. %s", e.getMessage()));
}
setPreferences(arguments, printStream, args);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class LoadPreferencesDeprecatedCommand method perform.
@SuppressWarnings("unchecked")
@Override
public void perform(Arguments arguments, PrintStream printStream) throws Exception {
String contentType = arguments.getOptional(ArgumentName.CONTENT_TYPE.toString(), "");
File file = new File(arguments.get(ArgumentName.LOCAL_FILE_PATH.toString()));
if (!file.isFile()) {
throw new IllegalArgumentException("Not a file: " + file);
}
Map<String, String> args = Maps.newHashMap();
try (FileReader reader = new FileReader(file)) {
if (contentType.equals("json")) {
args = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
} else {
throw new IllegalArgumentException("Unsupported file format. Only JSON format is supported");
}
} catch (JsonSyntaxException e) {
throw new BadRequestException(String.format("JSON syntax in file is invalid. Support only for string-to-string map. %s", e.getMessage()));
}
setPreferences(arguments, printStream, args);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class FetchHandler method poll.
@POST
@Path("poll")
public void poll(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
// Currently only support avro
if (!"avro/binary".equals(request.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
throw new BadRequestException("Only avro/binary content type is supported.");
}
// Decode the poll request
Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA);
// Fetch the messages
CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId);
try {
responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), ImmutableMultimap.of(HttpHeaders.Names.CONTENT_TYPE, "avro/binary"));
} catch (Throwable t) {
iterator.close();
throw t;
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class StoreHandler method publish.
@POST
@Path("/publish")
public void publish(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
StoreRequest storeRequest = createStoreRequest(topicId, request);
// Empty payload is only allowed for transactional publish
if (!storeRequest.isTransactional() && !storeRequest.hasNext()) {
throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId);
}
// Publish the message and response with the rollback information
RollbackDetail rollbackInfo = messagingService.publish(storeRequest);
if (rollbackInfo == null) {
// Non-tx publish doesn't have rollback info.
responder.sendStatus(HttpResponseStatus.OK);
return;
}
ChannelBuffer response = encodeRollbackDetail(rollbackInfo);
responder.sendContent(HttpResponseStatus.OK, response, "avro/binary", null);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class StoreHandler method createStoreRequest.
/**
* Creates a {@link StoreRequest} instance based on the given {@link HttpRequest}.
*/
private StoreRequest createStoreRequest(TopicId topicId, HttpRequest request) throws Exception {
// Currently only support avro
if (!"avro/binary".equals(request.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
throw new BadRequestException("Only avro/binary content type is supported.");
}
Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishRequest.SCHEMA);
return new GenericRecordStoreRequest(topicId, datumReader.read(null, decoder));
}
Aggregations