Search in sources :

Example 1 with HttpMethod

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod 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 HttpMethod

use of au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod 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

RestService (au.gov.asd.tac.constellation.webserver.restapi.RestService)2 RestServiceException (au.gov.asd.tac.constellation.webserver.restapi.RestServiceException)2 HttpMethod (au.gov.asd.tac.constellation.webserver.restapi.RestServiceUtilities.HttpMethod)2 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1