Example usage for javax.json Json createObjectBuilder

List of usage examples for javax.json Json createObjectBuilder

Introduction

In this page you can find the example usage for javax.json Json createObjectBuilder.

Prototype

public static JsonObjectBuilder createObjectBuilder(Map<String, Object> map) 

Source Link

Document

Creates a JSON object builder, initialized with the data from specified map .

Usage

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleValidate() throws Exception {
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "validate");
    Map<String, Object> body = new HashMap<>();
    Map<String, String> image = new HashMap<>();
    image.put("value", "ubuntu:latest");
    body.put("IMAGE", image);
    request.setRequestBody(Json.createObjectBuilder(body).build().toString());

    GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected successful response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());/*from  w w w.ja va  2  s . c o m*/
    JsonObject errors = Json.createReader(new StringReader(response.responseBody())).readObject()
            .getJsonObject("errors");
    assertEquals("Expected no errors", 0, errors.size());
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPluginTest.java

@Test
public void handleValidateError() throws Exception {
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest(null, null, "validate");
    Map<String, Object> body = new HashMap<>();
    Map<String, String> image = new HashMap<>();
    image.put("value", "ubuntu:");
    body.put("IMAGE", image);
    request.setRequestBody(Json.createObjectBuilder(body).build().toString());

    GoPluginApiResponse response = new DockerExecPlugin().handle(request);

    assertEquals("Expected successful response", DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE,
            response.responseCode());// www .  j av a2 s .  com
    JsonObject errors = Json.createReader(new StringReader(response.responseBody())).readObject()
            .getJsonObject("errors");
    assertEquals("Expected 1 error", 1, errors.size());
    assertEquals("Wrong message", "'ubuntu:' is not a valid image identifier", errors.getString("IMAGE"));
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPlugin.java

private GoPluginApiResponse handleExecuteRequest(JsonObject requestBody) {
    final String image = requestBody.getJsonObject(CONFIG).getJsonObject(IMAGE).getString(VALUE);
    final String command = requestBody.getJsonObject(CONFIG).getJsonObject("COMMAND").getString(VALUE);
    final JsonString argumentsJson = requestBody.getJsonObject(CONFIG).getJsonObject("ARGUMENTS")
            .getJsonString(VALUE);//from   w ww  .  ja va 2 s. c  o  m
    final String[] arguments;
    if (argumentsJson != null) {
        arguments = argumentsJson.getString().split("\\r?\\n");
    } else {
        arguments = new String[0];
    }
    Map<String, String> envVars = requestBody.getJsonObject("context").getJsonObject("environmentVariables")
            .entrySet().stream()
            .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), ((JsonString) e.getValue()).getString()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    final String workingDir = requestBody.getJsonObject("context").getString("workingDirectory");
    final String pwd = Paths.get(System.getProperty("user.dir"), workingDir).toAbsolutePath().toString();

    final Map<String, Object> responseBody = new HashMap<>();
    try {
        final int exitCode = executeBuild(image, pwd, envVars, command, arguments);

        responseBody.put(MESSAGE,
                (new StringBuilder()).append("Command ")
                        .append(DockerUtils.getCommandString(command, arguments))
                        .append(" completed with status ").append(exitCode).toString());
        if (exitCode == 0) {
            responseBody.put(SUCCESS, Boolean.TRUE);
        } else {
            responseBody.put(SUCCESS, Boolean.FALSE);
        }
    } catch (DockerCleanupException dce) {
        responseBody.clear();
        responseBody.put(SUCCESS, Boolean.FALSE);
        if (dce.getNested() == null) {
            responseBody.put(MESSAGE, dce.getCause().getMessage());
        } else {
            responseBody.put(MESSAGE, dce.getNested().getMessage());
        }
    } catch (ImageNotFoundException infe) {
        responseBody.put(SUCCESS, Boolean.FALSE);
        responseBody.put(MESSAGE,
                (new StringBuilder()).append("Image '").append(image).append("' not found").toString());
    } catch (Exception e) {
        responseBody.clear();
        responseBody.put(SUCCESS, Boolean.FALSE);
        responseBody.put(MESSAGE, e.getMessage());
    }

    return DefaultGoPluginApiResponse.success(Json.createObjectBuilder(responseBody).build().toString());
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPlugin.java

private GoPluginApiResponse handleValidateRequest(JsonObject requestBody) {
    final Map<String, Object> responseBody = new HashMap<>();
    final Map<String, String> errors = new HashMap<>();

    final String image = requestBody.getJsonObject(IMAGE).getString(VALUE);
    if (!imageValid(image)) {
        errors.put(IMAGE, (new StringBuilder()).append("'").append(image)
                .append("' is not a valid image identifier").toString());
    }/*from   www. j a va2s.co  m*/

    responseBody.put("errors", errors);
    return DefaultGoPluginApiResponse.success(Json.createObjectBuilder(responseBody).build().toString());
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPlugin.java

private GoPluginApiResponse handleViewRequest() {
    try {/*w ww . jav  a  2 s .co m*/
        final String template = IOUtils.toString(
                getClass().getResourceAsStream("/templates/task.template.html"), StandardCharsets.UTF_8);

        final Map<String, Object> body = new HashMap<>();
        body.put("displayValue", "Docker Exec");
        body.put("template", template);

        return DefaultGoPluginApiResponse.success(Json.createObjectBuilder(body).build().toString());
    } catch (Exception e) {
        Logger.getLoggerFor(this.getClass()).error("Error retrieving template", e);
        final String body = Json.createObjectBuilder().add("exception", e.getMessage()).build().toString();
        return DefaultGoPluginApiResponse.error(body);
    }
}

From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPlugin.java

private GoPluginApiResponse handleConfigRequest() {
    final Map<String, Object> body = new HashMap<>();
    final Map<String, Object> image = new HashMap<>();
    image.put(DISPLAY_NAME, "Image");
    image.put(DISPLAY_ORDER, "0");
    body.put(IMAGE, image);/*from  ww  w .j  a va  2s .  c  om*/
    final Map<String, Object> command = new HashMap<>();
    command.put(DISPLAY_NAME, "Command");
    command.put(DISPLAY_ORDER, "1");
    body.put("COMMAND", command);
    final Map<String, Object> arguments = new HashMap<>();
    arguments.put(DISPLAY_NAME, "Arguments");
    arguments.put(DISPLAY_ORDER, "2");
    arguments.put("required", false);
    body.put("ARGUMENTS", arguments);

    return DefaultGoPluginApiResponse.success(Json.createObjectBuilder(body).build().toString());
}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

/**
 * Creates a new NetworkConfig instance configured with details supplied in YAML format
 *
 * @param configStream A stream opened on a YAML document containing network configuration details
 * @return A new NetworkConfig instance/*from  ww w .  j  a va  2s  .  co  m*/
 * @throws InvalidArgumentException
 */
public static NetworkConfig fromYamlStream(InputStream configStream)
        throws InvalidArgumentException, NetworkConfigurationException {

    logger.trace("NetworkConfig.fromYamlStream...");

    // Sanity check
    if (configStream == null) {
        throw new InvalidArgumentException("configStream must be specified");
    }

    Yaml yaml = new Yaml();

    @SuppressWarnings("unchecked")
    Map<String, Object> map = yaml.load(configStream);

    JsonObjectBuilder builder = Json.createObjectBuilder(map);

    JsonObject jsonConfig = builder.build();
    return fromJsonObject(jsonConfig);
}