Example usage for com.google.gson JsonObject JsonObject

List of usage examples for com.google.gson JsonObject JsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject JsonObject.

Prototype

JsonObject

Source Link

Usage

From source file:co.cask.cdap.etl.spark.batch.OutputFormatProviderTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(OutputFormatProvider src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("outputFormatClass", src.getOutputFormatClassName());
    jsonObj.add("outputFormatConfig", context.serialize(src.getOutputFormatConfiguration()));
    return jsonObj;
}

From source file:co.cask.cdap.explore.executor.AbstractExploreMetadataHttpHandler.java

License:Apache License

protected void handleResponseEndpointExecution(HttpRequest request, HttpResponder responder,
        final EndpointCoreExecution<QueryHandle> execution) throws ExploreException, IOException {
    genericEndpointExecution(request, responder, new EndpointCoreExecution<Void>() {
        @Override//from w w w . j a v a 2  s  .  co m
        public Void execute(HttpRequest request, HttpResponder responder)
                throws IllegalArgumentException, SQLException, ExploreException, IOException {
            QueryHandle handle = execution.execute(request, responder);
            JsonObject json = new JsonObject();
            json.addProperty("handle", handle.getHandle());
            responder.sendJson(HttpResponseStatus.OK, json);
            return null;
        }
    });
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

@POST
@Path("streams/{stream}/tables/{table}/enable")
public void enableStream(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("stream") String streamName,
        @PathParam("table") String tableName) throws Exception {
    Id.Stream streamId = Id.Stream.from(namespaceId, streamName);
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()))) {
        FormatSpecification format = GSON.fromJson(reader, FormatSpecification.class);
        if (format == null) {
            throw new BadRequestException("Expected format in the body");
        }/*  w  w w.ja v  a2  s  .  co  m*/
        QueryHandle handle = 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());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

@POST
@Path("streams/{stream}/tables/{table}/disable")
public void disableStream(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("stream") String streamName,
        @PathParam("table") String tableName) {

    Id.Stream streamId = Id.Stream.from(namespaceId, streamName);
    try {/*from w  w w  .j ava2s .  com*/
        // throws io exception if there is no stream
        streamAdmin.getConfig(streamId);
    } catch (IOException e) {
        LOG.debug("Could not find stream {} to disable explore on.", streamName, e);
        responder.sendString(HttpResponseStatus.NOT_FOUND, "Could not find stream " + streamName);
        return;
    }

    try {
        QueryHandle handle = exploreTableManager.disableStream(tableName, streamId);
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json);
    } catch (Throwable t) {
        LOG.error("Got exception disabling exploration for stream {}", streamId, t);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

/**
 * Enable ad-hoc exploration of a dataset instance.
 *//*  w  ww  .jav  a2 s  .c o m*/
@POST
@Path("datasets/{dataset}/enable")
public void enableDataset(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("dataset") String datasetName) {
    Id.DatasetInstance datasetID = Id.DatasetInstance.from(namespaceId, datasetName);
    DatasetSpecification datasetSpec;
    try {
        datasetSpec = datasetFramework.getDatasetSpec(datasetID);
        if (datasetSpec == null) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetID);
            return;
        }
    } catch (DatasetManagementException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Error getting spec for dataset " + datasetID);
        return;
    }

    try {
        QueryHandle handle = exploreTableManager.enableDataset(datasetID, datasetSpec);
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json);
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (ExploreException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Error enabling explore on dataset " + datasetID);
    } catch (SQLException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST,
                "SQL exception while trying to enable explore on dataset " + datasetID);
    } catch (UnsupportedTypeException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST,
                "Schema for dataset " + datasetID + " is not supported for exploration: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Got exception:", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

/**
 * Disable ad-hoc exploration of a dataset instance.
 *///from   w w  w. j av a  2s. c o m
@POST
@Path("datasets/{dataset}/disable")
public void disableDataset(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("dataset") String datasetName) {

    LOG.debug("Disabling explore for dataset instance {}", datasetName);
    Id.DatasetInstance datasetID = Id.DatasetInstance.from(namespaceId, datasetName);
    DatasetSpecification spec;
    try {
        spec = datasetFramework.getDatasetSpec(datasetID);
        if (spec == null) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetID);
            return;
        }
    } catch (DatasetManagementException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Error getting spec for dataset " + datasetID);
        return;
    }

    try {
        QueryHandle handle = exploreTableManager.disableDataset(datasetID, spec);
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json);
    } catch (Throwable e) {
        LOG.error("Got exception while trying to disable explore on dataset {}", datasetID, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

@POST
@Path("datasets/{dataset}/partitions")
public void addPartition(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("dataset") String datasetName) {
    Id.DatasetInstance datasetInstanceId = Id.DatasetInstance.from(namespaceId, datasetName);
    Dataset dataset;/*from w ww  .  j a v a 2  s  .c o m*/
    try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
        dataset = datasetInstantiator.getDataset(datasetInstanceId);

        if (dataset == null) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetInstanceId);
            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 {}.", datasetInstanceId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Exception instantiating dataset " + datasetName);
        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());
        String fsPath = properties.get("path");
        if (fsPath == null) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, "path was not specified.");
            return;
        }

        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.addPartition(datasetInstanceId, partitionKey, fsPath);
        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());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreExecutorHttpHandler.java

License:Apache License

@POST
@Path("datasets/{dataset}/deletePartition")
public void dropPartition(HttpRequest request, HttpResponder responder,
        @PathParam("namespace-id") String namespaceId, @PathParam("dataset") String datasetName) {
    Id.DatasetInstance datasetInstanceId = Id.DatasetInstance.from(namespaceId, datasetName);
    Dataset dataset;/*  w  ww .j  av a 2s  .  c o  m*/
    try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {

        dataset = datasetInstantiator.getDataset(datasetInstanceId);
        if (dataset == null) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetInstanceId);
            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 {}.", datasetInstanceId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Exception instantiating dataset " + datasetInstanceId);
        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(datasetInstanceId, 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());
    }
}

From source file:co.cask.cdap.explore.executor.ExploreMetadataHttpHandler.java

License:Apache License

private void handleResponseEndpointExecution(HttpRequest request, HttpResponder responder,
        final EndpointCoreExecution<QueryHandle> execution) {
    genericEndpointExecution(request, responder, new EndpointCoreExecution<Void>() {
        @Override/* w  w  w .j  a  va  2  s .co m*/
        public Void execute(HttpRequest request, HttpResponder responder)
                throws IllegalArgumentException, SQLException, ExploreException, IOException {
            QueryHandle handle = execution.execute(request, responder);
            JsonObject json = new JsonObject();
            json.addProperty("handle", handle.getHandle());
            responder.sendJson(HttpResponseStatus.OK, json);
            return null;
        }
    });
}

From source file:co.cask.cdap.explore.executor.ExplorePingHandler.java

License:Apache License

@Path("/explore/status")
@GET//from w ww .jav a2 s.  co  m
public void status(@SuppressWarnings("UnusedParameters") HttpRequest request, HttpResponder responder) {
    JsonObject json = new JsonObject();
    json.addProperty("status", "OK");
    responder.sendJson(HttpResponseStatus.OK, json);
}