Search in sources :

Example 46 with BadRequestException

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);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) FileReader(java.io.FileReader) File(java.io.File)

Example 47 with BadRequestException

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);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) FileReader(java.io.FileReader) File(java.io.File)

Example 48 with BadRequestException

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;
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) RawMessage(co.cask.cdap.messaging.data.RawMessage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 49 with BadRequestException

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);
}
Also used : RollbackDetail(co.cask.cdap.messaging.RollbackDetail) StoreRequest(co.cask.cdap.messaging.StoreRequest) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 50 with BadRequestException

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));
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BadRequestException(co.cask.cdap.common.BadRequestException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6