Search in sources :

Example 1 with RestServiceException

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceException in project constellation by constellation-app.

the class GetServiceDescription method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String serviceName = parameters.getStringValue(SERVICE_NAME_PARAMETER_ID);
    final HttpMethod httpMethod = HttpMethod.getValue(parameters.getStringValue(METHOD_NAME_PARAMETER_ID));
    try {
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode root = mapper.createObjectNode();
        final RestService rs = RestServiceRegistry.get(serviceName, httpMethod);
        root.put("name", rs.getName());
        root.put("http_method", httpMethod.name());
        root.put("description", rs.getDescription());
        root.put("mimetype", rs.getMimeType());
        final ArrayNode tags = root.putArray("tags");
        for (final String tag : rs.getTags()) {
            tags.add(tag);
        }
        final ObjectNode params = root.putObject("parameters");
        rs.createParameters().getParameters().entrySet().forEach(entry -> {
            final PluginParameter<?> pp = entry.getValue();
            final ObjectNode param = params.putObject(entry.getKey());
            param.put("name", pp.getName());
            param.put("type", pp.getType().getId());
            param.put("description", pp.getDescription());
            if (pp.getObjectValue() != null) {
                param.put("value", pp.getObjectValue().toString());
            }
        });
        mapper.writeValue(out, root);
    } catch (final IllegalArgumentException ex) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, ex.getMessage());
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RestService(au.gov.asd.tac.constellation.webserver.restapi.RestService) HttpMethod(au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RestServiceException

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceException in project constellation by constellation-app.

the class AddRecordStore method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    final boolean completeWithSchema = parameters.getBooleanValue(COMPLETE_PARAMETER_ID);
    final String arrange = parameters.getStringValue(ARRANGE_PARAMETER_ID);
    final boolean resetView = parameters.getBooleanValue(RESET_PARAMETER_ID);
    final RecordStore rs = new GraphRecordStore();
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode json = mapper.readTree(in);
    final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
    // (We ignore the index array.)
    if (!json.hasNonNull(COLUMNS) || !json.get(COLUMNS).isArray()) {
        throw new RestServiceException("Could not find columns object containing column names");
    }
    if (!json.hasNonNull("data") || !json.get("data").isArray()) {
        throw new RestServiceException("Could not find data object containing data rows");
    }
    final ArrayNode columns = (ArrayNode) json.get(COLUMNS);
    final String[] headers = new String[columns.size()];
    for (int i = 0; i < headers.length; i++) {
        headers[i] = columns.get(i).asText();
    }
    final ArrayNode data = (ArrayNode) json.get("data");
    for (final Iterator<JsonNode> i = data.elements(); i.hasNext(); ) {
        final ArrayNode jrow = (ArrayNode) i.next();
        rs.add();
        boolean txFound = false;
        boolean txSourceFound = false;
        for (int ix = 0; ix < headers.length; ix++) {
            final String h = headers[ix];
            final JsonNode jn = jrow.get(ix);
            if (!jn.isNull()) {
                if (jn.getNodeType() == JsonNodeType.ARRAY) {
                    rs.set(h, RestServiceUtilities.toList((ArrayNode) jn));
                } else {
                    rs.set(h, jn.asText());
                }
            }
            txFound |= h.startsWith(GraphRecordStoreUtilities.TRANSACTION);
            txSourceFound |= TX_SOURCE.equals(h);
        }
        if (txFound && !txSourceFound) {
            rs.set(TX_SOURCE, API_SOURCE);
        }
    }
    addToGraph(graph, rs, completeWithSchema, arrange, resetView);
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) Graph(au.gov.asd.tac.constellation.graph.Graph) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with RestServiceException

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceException in project constellation by constellation-app.

the class AddRecordStore method addToGraph.

private static void addToGraph(final Graph graph, final RecordStore recordStore, final boolean completeWithSchema, final String arrange, final boolean resetView) {
    final Plugin p = new ImportFromRestApiPlugin(recordStore, completeWithSchema, arrange);
    PluginExecutor pe = PluginExecutor.startWith(p);
    if (resetView) {
        pe = pe.followedBy(InteractiveGraphPluginRegistry.RESET_VIEW);
    }
    try {
        pe.executeNow(graph);
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new RestServiceException(ex);
    } catch (final PluginException ex) {
        throw new RestServiceException(ex);
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) PluginExecutor(au.gov.asd.tac.constellation.plugins.PluginExecutor) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

Example 4 with RestServiceException

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceException in project constellation by constellation-app.

the class GetGraphImage method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final Graph graph = GraphManager.getDefault().getActiveGraph();
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph is opened in Constellation");
    }
    // This is asynchronous, so we need a Semaphore.
    // 
    final GraphNode graphNode = GraphNode.getGraphNode(graph);
    final VisualManager visualManager = graphNode.getVisualManager();
    final BufferedImage[] img1 = new BufferedImage[1];
    if (visualManager != null) {
        final Semaphore waiter = new Semaphore(0);
        visualManager.exportToBufferedImage(img1, waiter);
        waiter.acquireUninterruptibly();
        ImageIO.write(img1[0], "png", out);
    } else {
        throw new IOException("Graph image unavailable");
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) VisualManager(au.gov.asd.tac.constellation.utilities.visual.VisualManager) Graph(au.gov.asd.tac.constellation.graph.Graph) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 5 with RestServiceException

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceException in project constellation by constellation-app.

the class NewGraph method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String schemaParam = parameters.getStringValue(SCHEMA_PARAMETER_ID);
    String schemaName = null;
    for (final SchemaFactory schemaFactory : SchemaFactoryUtilities.getSchemaFactories().values()) {
        if (schemaFactory.isPrimarySchema() && (schemaParam == null || schemaParam.equals(schemaFactory.getName()))) {
            schemaName = schemaFactory.getName();
            break;
        }
    }
    if (schemaName == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, String.format("Unknown schema %s", schemaParam));
    }
    // Creating a new graph is asynchronous; we want to hide this from the client.
    // 
    final Graph existingGraph = GraphManager.getDefault().getActiveGraph();
    final String existingId = existingGraph != null ? existingGraph.getId() : null;
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(schemaName).createSchema();
    final StoreGraph sg = new StoreGraph(schema);
    schema.newGraph(sg);
    final Graph dualGraph = new DualGraph(sg, false);
    final String graphName = SchemaFactoryUtilities.getSchemaFactory(schemaName).getLabel().trim().toLowerCase();
    GraphOpener.getDefault().openGraph(dualGraph, graphName);
    final String newId = RestServiceUtilities.waitForGraphChange(existingId);
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode root = mapper.createObjectNode();
    root.put("id", newId);
    root.put("name", GraphNode.getGraphNode(newId).getDisplayName());
    root.put("schema", schemaName);
    mapper.writeValue(out, root);
}
Also used : SchemaFactory(au.gov.asd.tac.constellation.graph.schema.SchemaFactory) RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Aggregations

RestServiceException (au.gov.asd.tac.constellation.webserver.restapi.RestServiceException)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Graph (au.gov.asd.tac.constellation.graph.Graph)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)8 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)7 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)5 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 IOException (java.io.IOException)4 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)3 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)3 RestService (au.gov.asd.tac.constellation.webserver.restapi.RestService)3 GraphNode (au.gov.asd.tac.constellation.graph.node.GraphNode)2 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)2 SimpleEditPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin)2 HandleIoProgress (au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress)2 HttpMethod (au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod)2 FileNotFoundException (java.io.FileNotFoundException)2 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)1 GraphJsonReader (au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader)1