Example usage for javax.json JsonObjectBuilder build

List of usage examples for javax.json JsonObjectBuilder build

Introduction

In this page you can find the example usage for javax.json JsonObjectBuilder build.

Prototype

JsonObject build();

Source Link

Document

Returns the JSON object associated with this object builder.

Usage

From source file:org.trellisldp.http.MultipartUploader.java

/**
 * Get a list of the uploads//from   ww w.j a v  a  2 s  . c  om
 * @param partition the partition
 * @param id the upload id
 * @return a response
 *
 * <p>Note: the response structure will be like this:</p>
 * <pre>{
 *   "1": "somehash",
 *   "2": "otherhash",
 *   "3": "anotherhash"
 * }</pre>
 */
@GET
@Timed
@Produces("application/json")
public String listUploads(@PathParam("partition") final String partition, @PathParam("id") final String id) {
    final JsonObjectBuilder builder = Json.createObjectBuilder();

    binaryService.getResolverForPartition(partition).filter(BinaryService.Resolver::supportsMultipartUpload)
            .filter(res -> res.uploadSessionExists(id)).map(res -> res.listParts(id))
            .orElseThrow(NotFoundException::new).forEach(x -> builder.add(x.getKey().toString(), x.getValue()));

    return builder.build().toString();
}

From source file:org.gameontext.mediator.models.Exits.java

@JsonIgnore
public JsonObject toSimpleJsonList() {

    JsonObjectBuilder content = Json.createObjectBuilder();
    if (n != null) {
        content.add("N", n.getDoor());
    }//from  w w  w .  j a va 2s. c o  m

    if (s != null) {
        content.add("S", s.getDoor());
    }

    if (e != null) {
        content.add("E", e.getDoor());
    }

    if (w != null) {
        content.add("W", w.getDoor());
    }

    if (u != null) {
        content.add("U", u.getDoor());
    }

    if (d != null) {
        content.add("D", d.getDoor());
    }

    return content.build();
}

From source file:httputils.RavelloHttpClient.java

public JsonObject publishBlueprint(String applicationName, int blueprintId, int stopTime, int startupDelay,
        String preferredCloud, String preferredRegion, boolean startAllVms, boolean costOptimized)
        throws RavelloException, InterruptedException {
    JsonObject value = null;//from w w w.jav  a 2s  .  c o  m
    HttpResponse response = null;

    try {
        response = this.getBlueprint(blueprintId);
        if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new RavelloException("Failed to get blueprint number " + blueprintId + " error: "
                    + response.getStatusLine().toString());
        }
        JsonObject vmTemp = HttpUtil.convertResponseToJson(response);
        EntityUtils.consume(response.getEntity());

        JsonBuilderFactory factory = Json.createBuilderFactory(null);
        Iterator<Map.Entry<String, JsonValue>> it = vmTemp.entrySet().iterator();
        JsonObjectBuilder builder = factory.createObjectBuilder();
        Map.Entry<String, JsonValue> ent;
        while (it.hasNext()) {
            ent = it.next();
            if (!ent.getKey().equals("id") && !ent.getKey().equals("owner")) {
                builder.add(ent.getKey(), ent.getValue());
            }
        }
        builder.add("name", applicationName);
        value = builder.build();
        vmTemp = null;

        response = this.createApplication(value);
        this.verifyResponseAndConsume(response, "Failed to create application - error: ");

        value = HttpUtil.convertResponseToJson(response);
        EntityUtils.consumeQuietly(response.getEntity());

        int appId = value.getInt("id");

        if (costOptimized) {
            value = factory.createObjectBuilder().add("startAllVms", startAllVms).build();
        } else {
            value = factory.createObjectBuilder().add("startAllVms", startAllVms)
                    .add("preferredCloud", preferredCloud).add("preferredRegion", preferredRegion)
                    .add("optimizationLevel", "PERFORMANCE_OPTIMIZED").build();
        }
        response = this.post("/applications/" + appId + "/publish", value);
        this.verifyResponseAndConsume(response, "Failed to publish application - error: ");

        value = factory.createObjectBuilder().add("expirationFromNowSeconds", stopTime).build();
        response = this.post("/applications/" + appId + "/setExpiration", value);
        if (!HttpUtil.verifyResponseAndConsume(response)) {
            throw new RavelloException("Failed to set expiration time for application - error: "
                    + response.getStatusLine().toString() + "\n"
                    + "THIS ERROR MAY CAUSE APPLICATION TO RUN INDEFINITELY - MAKE SURE TO CHECK IT STOPPED");
        }

        if (!startAllVms) {
            response = this.getApplication(appId);
            if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
                EntityUtils.consumeQuietly(response.getEntity());
                throw new RavelloException(
                        "Failed to get application status - error: " + response.getStatusLine().toString());
            }
            value = HttpUtil.convertResponseToJson(response);

            return value;
        }

        String state;
        JsonArray jArr;
        boolean allStarted;
        while (true) {
            allStarted = true;
            response = this.getApplication(appId);
            if (!HttpUtil.verifyResponseWithoutConsuming(response)) {
                EntityUtils.consumeQuietly(response.getEntity());
                throw new RavelloException(
                        "Failed to get application status - error: " + response.getStatusLine().toString());
            }
            value = HttpUtil.convertResponseToJson(response);

            jArr = value.getJsonObject("deployment").getJsonArray("vms");
            for (int jt = 0; jt < jArr.size(); jt++) {
                state = jArr.getJsonObject(jt).getString("state");
                allStarted = state.equals("STARTED");
                if (state.equals("ERROR")) {
                    throw new RavelloException(
                            "vm" + jArr.getJsonObject(jt).getString("name") + " failed to start");
                }
                if (!allStarted) {
                    break;
                }
            }

            if (allStarted) {
                break;
            } else {
                EntityUtils.consumeQuietly(response.getEntity());
                Thread.sleep(20000);
            }
        }

    } catch (ClientProtocolException e) {
        throw new RavelloException("ClientProtocolException - " + e.getMessage());
    } catch (IOException e) {
        throw new RavelloException("IOException - " + e.getMessage());
    } catch (NullPointerException e) {
        throw new RavelloException("NullPointerException - " + e.getMessage());
    }

    Thread.sleep(startupDelay * 1000);

    return value;
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkStringField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");

    Settings settings = new Settings();
    settings.setEntityClass(Person.class);
    Person person = (Person) aggregateService.create(jsonBuilder.build(), settings);
    assert (person.getId() != null);
    assert (person.getName().equals("DILIP_DALTON"));

    Object jsonObject = aggregateService.read(person, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));

}

From source file:no.sintef.jarfter.PostgresqlInteractor.java

public JsonObject selectAllTransfomations(String filter_uri) throws JarfterException {
    checkConnection();//  w w w. j  a va 2 s  .  c o  m

    String table = "transformations";
    String uri = "uri";
    String name = "name";
    String metadata = "metadata";
    String clojure = "clojure";
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    try {
        PreparedStatement st = conn.prepareStatement("SELECT " + uri + ", " + name + ", " + metadata + ", "
                + clojure + " FROM transformations WHERE uri ~ ?;");
        if (filter_uri == null) {
            st.setString(1, ".*");
        } else {
            st.setString(1, filter_uri);
        }
        log("selectAllTransfomations - calling executeQuery");
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            JsonObjectBuilder job = Json.createObjectBuilder();
            job.add(uri, rs.getString(uri));
            job.add(name, rs.getString(name));
            job.add(metadata, rs.getString(metadata));
            job.add(clojure, rs.getString(clojure));
            jsonArrayBuilder.add(job.build());
        }

        rs.close();
        st.close();
    } catch (SQLException sqle) {
        log("selectAllTransformations - got SQLException");
        error(sqle);
        throw new JarfterException(JarfterException.Error.SQL_UNKNOWN_ERROR, sqle.getLocalizedMessage());
    }
    return Json.createObjectBuilder().add(table, jsonArrayBuilder.build()).build();
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkEmptyLongField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getSalary() == null);
    assert (!employee.getIsCriticalSystemObject());

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (!json.containsKey("salary"));
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkLongField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("salary", 100000);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getSalary() == 100000);

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (((JsonNumber) json.get("salary")).intValue() == 100000);
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkIntField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("employeeNo", 235);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getEmployeeNo() == 235);

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (((JsonNumber) json.get("employeeNo")).intValue() == 235);
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkBooleanField() throws JSONException {
    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("isCriticalSystemObject", true);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getIsCriticalSystemObject());

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (json.getBoolean("isCriticalSystemObject"));
}

From source file:tools.xor.logic.DefaultJson.java

protected void checkBigDecimalField() throws JSONException {
    final BigDecimal largeDecimal = new BigDecimal("12345678998765432100000.123456789987654321");

    // create person
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
    jsonBuilder.add("name", "DILIP_DALTON");
    jsonBuilder.add("displayName", "Dilip Dalton");
    jsonBuilder.add("description", "Software engineer in the bay area");
    jsonBuilder.add("userName", "daltond");
    jsonBuilder.add("largeDecimal", largeDecimal);

    Settings settings = new Settings();
    settings.setEntityClass(Employee.class);
    Employee employee = (Employee) aggregateService.create(jsonBuilder.build(), settings);
    assert (employee.getId() != null);
    assert (employee.getName().equals("DILIP_DALTON"));
    assert (employee.getLargeDecimal().equals(largeDecimal));

    Object jsonObject = aggregateService.read(employee, settings);
    JsonObject json = (JsonObject) jsonObject;
    System.out.println("JSON string: " + json.toString());
    assert (((JsonString) json.get("name")).getString().equals("DILIP_DALTON"));
    assert (((JsonNumber) json.get("largeDecimal")).bigDecimalValue().equals(largeDecimal));
}