Search in sources :

Example 16 with ByteBufInputStream

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()]);
}
Also used : InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with ByteBufInputStream

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;
    }
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 18 with ByteBufInputStream

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));
}
Also used : InputStreamReader(java.io.InputStreamReader) ArtifactId(co.cask.cdap.proto.id.ArtifactId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) ArtifactPropertiesRequest(co.cask.cdap.proto.artifact.ArtifactPropertiesRequest) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ArtifactSummaryProperties(co.cask.cdap.proto.artifact.ArtifactSummaryProperties) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 19 with ByteBufInputStream

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;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail)

Example 20 with ByteBufInputStream

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) Nullable(javax.annotation.Nullable)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3