Search in sources :

Example 16 with RestServiceException

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

the class GetAttributes method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    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 ObjectMapper mapper = new ObjectMapper();
    final ObjectNode root = mapper.createObjectNode();
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        final int gCount = rg.getAttributeCount(GraphElementType.GRAPH);
        for (int i = 0; i < gCount; i++) {
            final int attrId = rg.getAttribute(GraphElementType.GRAPH, i);
            final String type = rg.getAttributeType(attrId);
            final String label = rg.getAttributeName(attrId);
            root.put(String.format("graph.%s", label), type);
        }
        final int vCount = rg.getAttributeCount(GraphElementType.VERTEX);
        for (int i = 0; i < vCount; i++) {
            final int attrId = rg.getAttribute(GraphElementType.VERTEX, i);
            final String type = rg.getAttributeType(attrId);
            final String label = rg.getAttributeName(attrId);
            root.put(String.format("source.%s", label), type);
        }
        final int tCount = rg.getAttributeCount(GraphElementType.TRANSACTION);
        for (int i = 0; i < tCount; i++) {
            final int attrId = rg.getAttribute(GraphElementType.TRANSACTION, i);
            final String type = rg.getAttributeType(attrId);
            final String label = rg.getAttributeName(attrId);
            root.put(String.format("transaction.%s", label), type);
        }
    } finally {
        rg.release();
    }
    mapper.writeValue(out, root);
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with RestServiceException

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

the class GetGraphValues method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    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 ObjectMapper mapper = new ObjectMapper();
    final ObjectNode root = mapper.createObjectNode();
    final ArrayNode columns = root.putArray("columns");
    final ArrayNode data = root.putArray("data");
    final ArrayNode row = data.addArray();
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        final int gCount = rg.getAttributeCount(GraphElementType.GRAPH);
        for (int i = 0; i < gCount; i++) {
            final int attrId = rg.getAttribute(GraphElementType.GRAPH, i);
            final String type = rg.getAttributeType(attrId);
            final String label = rg.getAttributeName(attrId);
            String value = rg.getStringValue(attrId, 0);
            columns.add(String.format("%s|%s", label, type));
            RestUtilities.addData(row, type, value);
        }
    } finally {
        rg.release();
    }
    mapper.writeValue(out, root);
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 18 with RestServiceException

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

the class SetGraph method callService.

@Override
public void callService(final PluginParameters parameters, InputStream in, OutputStream out) throws IOException {
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    final GraphNode graphNode = GraphNode.getGraphNode(graphId);
    if (graphNode != null) {
        try {
            SwingUtilities.invokeAndWait(() -> graphNode.getTopComponent().requestActive());
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new RestServiceException(ex);
        } catch (final InvocationTargetException ex) {
            throw new RestServiceException(ex);
        }
    } else {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 19 with RestServiceException

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

the class FileListener method parseAndExecute.

/**
 * Execute a REST endpoint.
 *
 * @param node A JSON node representing the input parameters.
 *
 * @throws Exception because of AutoCloseable
 */
private void parseAndExecute(final String verb, final String endpoint, final String path, final JsonNode args) throws Exception {
    if ("/v2/service".equals(endpoint)) {
        final HttpMethod httpMethod = HttpMethod.getValue(verb);
        // Get an instance of the service (if it exists).
        // 
        final RestService rs = RestServiceRegistry.get(path, httpMethod);
        // Convert the arguments to PluginParameters.
        // 
        final PluginParameters parameters = rs.createParameters();
        RestServiceUtilities.parametersFromJson((ObjectNode) args, parameters);
        try (final InStream ins = new InStream(restPath, CONTENT_IN, true);
            final OutputStream out = outStream(restPath, CONTENT_OUT)) {
            rs.callService(parameters, ins.in, out);
        } catch (final IOException | RuntimeException ex) {
            throw new RestServiceException(ex);
        }
    } else {
        unrec(ENDPOINT, endpoint);
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) IOException(java.io.IOException) RestService(au.gov.asd.tac.constellation.webserver.restapi.RestService) HttpMethod(au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod)

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