Search in sources :

Example 11 with RestServiceException

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

the class GetIcon method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String iconName = parameters.getStringValue(ICON_PARAMETER_ID);
    if (!IconManager.iconExists(iconName)) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No icon with name " + iconName);
    }
    final ConstellationIcon icon = IconManager.getIcon(iconName);
    out.write(icon.buildByteArray());
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ConstellationIcon(au.gov.asd.tac.constellation.utilities.icon.ConstellationIcon)

Example 12 with RestServiceException

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

the class GetPluginDescription method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String pluginName = parameters.getStringValue(PLUGIN_NAME_PARAMETER_ID);
    try {
        final Plugin plugin = PluginRegistry.get(pluginName);
        final PluginParameters pluginParams = plugin.createParameters();
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode root = mapper.createObjectNode();
        root.put("name", plugin.getName());
        root.put("id", plugin.getId());
        root.put("description", plugin.getDescription());
        final ArrayNode tags = root.putArray("tags");
        for (final String tag : plugin.getTags()) {
            tags.add(tag);
        }
        final ObjectNode params = root.putObject("parameters");
        if (pluginParams != null) {
            pluginParams.getParameters().entrySet().forEach(entry -> {
                final ObjectNode param = params.putObject(entry.getKey());
                final PluginParameter<?> pp = entry.getValue();
                param.put("name", pp.getName());
                param.put("type", pp.getType().getId());
                param.put("description", pp.getDescription());
            });
        }
        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) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

Example 13 with RestServiceException

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

the class GetRecordStore 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 selected = parameters.getBooleanValue(SELECTED_PARAMETER_ID);
    final boolean vx = parameters.getBooleanValue(VX_PARAMETER_ID);
    final boolean tx = parameters.getBooleanValue(TX_PARAMETER_ID);
    final String attrsParam = parameters.getStringValue(ATTRS_PARAMETER_ID);
    // Allow the user to specify a specific set of attributes,
    // cutting down data transfer and processing a lot,
    // particularly on the Python side.
    final String[] attrsArray = attrsParam != null ? attrsParam.split(",") : new String[0];
    // Maintain the order specified by the user.
    final Set<String> attrs = new LinkedHashSet<>();
    Collections.addAll(attrs, attrsArray);
    // Build the JSON in a form suitable for passing to pandas.DataFrame.from_items().
    // This includes the datatypes with the names, so the client can do transforms
    // where required (for example, converting strings to timestamps).
    // 
    final IoProgress ioph = new HandleIoProgress("External script: get RecordStore");
    ioph.start();
    ioph.progress("Building RecordStore...");
    final GraphRecordStore recordStore;
    final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        if ((vx && tx) || !(vx || tx)) {
            // We're getting the entire graph. We don't want all
            // of the vertices: since all of the vertices with
            // transactions are already included, we only want
            // the rest of the vertices, ie the singletons.
            recordStore = GraphRecordStoreUtilities.getAll(rg, true, selected, false);
        } else if (vx) {
            recordStore = GraphRecordStoreUtilities.getVertices(rg, false, selected, false);
        } else {
            recordStore = GraphRecordStoreUtilities.getTransactions(rg, selected, false);
        }
    } finally {
        rg.release();
    }
    // Create a mapping from "attrname" to "attrname<type>" for
    // all of the RecordStore attributes.
    final Map<String, String> attrToTypedAttr = new HashMap<>();
    for (final String kt : recordStore.keysWithType()) {
        final int ix = kt.lastIndexOf('<');
        final String key = kt.substring(0, ix);
        attrToTypedAttr.put(key, kt);
    }
    if (!attrs.isEmpty() && recordStore.size() > 0) {
        // Check that all of the user-specified attributes exist.
        final StringJoiner buf = new StringJoiner(",");
        for (final String key : attrs) {
            if (!attrToTypedAttr.containsKey(key)) {
                buf.add(key);
            }
        }
        if (buf.length() != 0) {
            throw new RestServiceException("The following attributes do not exist in the record store: " + buf.toString());
        }
    } else {
        // The user didn't specify any attributes, so use all of
        // the RecordStore keys.
        attrs.addAll(attrToTypedAttr.keySet());
    }
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
    mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, false);
    final ObjectNode root = mapper.createObjectNode();
    // We want to build a JSON document that looks like:
    // 
    // {"columns":["A","B"],"data":[[1,"a"],[2,"b"],[3,"c"]]}
    // 
    // which can be read by pandas.read_json(..., orient="split").
    // (It appears that the index parameter is not required.)
    final ArrayNode columns = root.putArray("columns");
    final ArrayNode data = root.putArray("data");
    if (recordStore.size() > 0) {
        ioph.progress("Building DataFrame...");
        for (final String attr : attrs) {
            final String kt = attrToTypedAttr.get(attr);
            columns.add(keyedName(kt));
        }
        recordStore.reset();
        while (recordStore.next()) {
            final ArrayNode row = data.addArray();
            for (final String attr : attrs) {
                final String kt = attrToTypedAttr.get(attr);
                final String type = kt.substring(kt.lastIndexOf('<') + 1, kt.length() - 1);
                final String value = recordStore.get(kt);
                RestUtilities.addData(row, type, value);
            }
        }
    }
    mapper.writeValue(out, root);
    ioph.finish();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) StringJoiner(java.util.StringJoiner) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) IoProgress(au.gov.asd.tac.constellation.utilities.gui.IoProgress)

Example 14 with RestServiceException

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

the class OpenGraph method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String filePath = parameters.getStringValue(FILE_PARAMETER_ID);
    final String existingId = RestServiceUtilities.activeGraphId();
    final File fnam = new File(filePath).getAbsoluteFile();
    String name = fnam.getName();
    if (StringUtils.endsWithIgnoreCase(name, GraphDataObject.FILE_EXTENSION)) {
        name = name.substring(0, name.length() - GraphDataObject.FILE_EXTENSION.length());
    }
    try {
        final Graph g = new GraphJsonReader().readGraphZip(fnam, new HandleIoProgress(String.format("Loading graph %s...", fnam)));
        GraphOpener.getDefault().openGraph(g, name, false);
        final String newId = RestServiceUtilities.waitForGraphChange(existingId);
        final Graph graph = GraphNode.getGraphNode(newId).getGraph();
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode root = mapper.createObjectNode();
        root.put("id", graph.getId());
        root.put("name", GraphNode.getGraphNode(graph.getId()).getDisplayName());
        root.put("schema", graph.getSchema().getFactory().getName());
        mapper.writeValue(out, root);
    } catch (final GraphParseException | FileNotFoundException ex) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, ex.getMessage());
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) Graph(au.gov.asd.tac.constellation.graph.Graph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) FileNotFoundException(java.io.FileNotFoundException) GraphParseException(au.gov.asd.tac.constellation.graph.file.io.GraphParseException) File(java.io.File) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with RestServiceException

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

the class RunPlugin method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String pluginName = parameters.getStringValue(PLUGIN_NAME_PARAMETER_ID);
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
    if (graph != null) {
        try {
            final ObjectMapper mapper = new ObjectMapper();
            final JsonNode json = mapper.readTree(in);
            if (json.size() > 0) {
                final Plugin plugin = PluginRegistry.get(pluginName);
                final PluginParameters pluginParameters = plugin.createParameters();
                RestServiceUtilities.parametersFromJson((ObjectNode) json, pluginParameters);
                PluginExecution.withPlugin(plugin).withParameters(pluginParameters).executeNow(graph);
            } else {
                PluginExecution.withPlugin(pluginName).executeNow(graph);
            }
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new RestServiceException(ex);
        } catch (final PluginException | IllegalArgumentException ex) {
            throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, ex.getMessage());
        }
    } else {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) JsonNode(com.fasterxml.jackson.databind.JsonNode) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

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