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());
}
}
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);
}
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);
}
}
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");
}
}
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);
}
Aggregations