Example usage for com.google.gson JsonObject addProperty

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

Introduction

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

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

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 {/*ww w  .  ja  v a 2 s. c o  m*/
        // 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.
 *//*from  ww  w.  j a  v a  2 s  .  c om*/
@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.
 *//*  ww  w  .j a  va 2 s .co 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  w  w  .  j a  v  a 2 s  . c  om*/
    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 w  w.  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 " + 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/*from  w ww. j  a v a2  s.  c  om*/
        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 w  w.  java 2s .c o m
public void status(@SuppressWarnings("UnusedParameters") HttpRequest request, HttpResponder responder) {
    JsonObject json = new JsonObject();
    json.addProperty("status", "OK");
    responder.sendJson(HttpResponseStatus.OK, json);
}

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

License:Apache License

@Path("explore/status")
@GET/* ww  w.  ja v  a 2  s  . c  om*/
public void status(HttpRequest request, HttpResponder responder) {
    JsonObject json = new JsonObject();
    json.addProperty("status", "OK");
    responder.sendJson(HttpResponseStatus.OK, json);
}

From source file:co.cask.cdap.gateway.handlers.AbstractMonitorHandler.java

License:Apache License

public void getServiceInstance(HttpRequest request, HttpResponder responder, String serviceName)
        throws Exception {
    JsonObject reply = new JsonObject();
    if (!serviceManagementMap.containsKey(serviceName)) {
        responder.sendString(HttpResponseStatus.NOT_FOUND,
                String.format("Invalid service name %s", serviceName));
        return;//from   w w  w.  j a v  a 2 s  .co m
    }
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    if (serviceManager.isServiceEnabled()) {
        int actualInstance = serviceManagementMap.get(serviceName).getInstances();
        reply.addProperty("provisioned", actualInstance);
        reply.addProperty("requested", getSystemServiceInstanceCount(serviceName));
        responder.sendJson(HttpResponseStatus.OK, reply);
    } else {
        responder.sendString(HttpResponseStatus.FORBIDDEN,
                String.format("Service %s is not enabled", serviceName));
    }
}

From source file:co.cask.cdap.gateway.handlers.AbstractMonitorHandler.java

License:Apache License

public void monitor(HttpRequest request, HttpResponder responder, String serviceName) {
    if (!serviceManagementMap.containsKey(serviceName)) {
        responder.sendString(HttpResponseStatus.NOT_FOUND,
                String.format("Invalid service name %s", serviceName));
        return;/*from w ww  .j  a v a 2 s.c  om*/
    }
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    if (!masterServiceManager.isServiceEnabled()) {
        responder.sendString(HttpResponseStatus.FORBIDDEN,
                String.format("Service %s is not enabled", serviceName));
        return;
    }
    if (masterServiceManager.canCheckStatus() && masterServiceManager.isServiceAvailable()) {
        JsonObject json = new JsonObject();
        json.addProperty("status", STATUSOK);
        responder.sendJson(HttpResponseStatus.OK, json);
    } else if (masterServiceManager.canCheckStatus()) {
        JsonObject json = new JsonObject();
        json.addProperty("status", STATUSNOTOK);
        responder.sendJson(HttpResponseStatus.OK, json);
    } else {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Operation not valid for this service");
    }
}