Example usage for javax.json JsonString toString

List of usage examples for javax.json JsonString toString

Introduction

In this page you can find the example usage for javax.json JsonString toString.

Prototype

@Override
String toString();

Source Link

Document

Returns JSON text for this JSON value.

Usage

From source file:com.dhenton9000.jersey.client.BasicTests.java

@Test
public void testGetRestaurantWithReadOnlyJsonApi() {
    ClientConfig config = new ClientConfig();

    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getBaseURI());

    JsonObject restaurantObject = this.getSingleRestaurant(4, target);
    JsonString restaurantName = restaurantObject.getJsonString("name");
    LOG.debug(restaurantName.getString());
    assertTrue(restaurantName.toString().toUpperCase().contains("ARBY"));
}

From source file:de.tu_dortmund.ub.data.dswarm.Task.java

@Override
public String call() {

    // init logger
    PropertyConfigurator.configure(config.getProperty("service.log4j-conf"));

    logger.info("[" + config.getProperty("service.name") + "] " + "Starting 'Task' ...");

    // init IDs of the prototype project
    String dataModelID = config.getProperty("prototype.dataModelID");
    String projectID = config.getProperty("prototype.projectID");
    String outputDataModelID = config.getProperty("prototype.outputDataModelID");

    // init process values
    String inputResourceID = null;
    String message = null;// w w  w. j  a v a 2s. c  o m

    try {

        // get the resource id of the current data model >> updateResourceID replaces resourceID
        String updateResourceID = null;
        try {
            updateResourceID = getProjectResourceID(dataModelID);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        logger.info("[" + config.getProperty("service.name") + "] updateResourceID = " + updateResourceID);

        // upload resource and update a InputDataModel
        String inputResourceJson = uploadFileAndUpdateResource(updateResourceID, resource,
                "resource for project '" + resource, config.getProperty("project.name") + "' - case " + cnt);
        JsonReader jsonReader = Json.createReader(IOUtils.toInputStream(inputResourceJson, "UTF-8"));
        inputResourceID = jsonReader.readObject().getString("uuid");
        logger.info("[" + config.getProperty("service.name") + "] inputResourceID = " + inputResourceID);

        if (updateResourceID != null) {

            // update the datamodel (will use it's (update) resource)
            updateDataModel(dataModelID);

            // configuration and processing of the task
            String jsonResponse = executeTask(dataModelID, projectID, outputDataModelID);

            if (jsonResponse != null) {

                if (Boolean.parseBoolean(config.getProperty("results.persistInFolder"))) {

                    if (Boolean.parseBoolean(config.getProperty("results.writeDMPJson"))) {
                        // save DMP results in files
                        FileUtils.writeStringToFile(new File(config.getProperty("results.folder")
                                + File.separatorChar + dataModelID + "." + cnt + ".json"), jsonResponse);
                    }

                    // build rdf graph
                    ValueFactory factory = ValueFactoryImpl.getInstance();

                    Graph graph = new LinkedHashModel();

                    URI graphUri = factory.createURI(config.getProperty("results.rdf.graph"));

                    URI subject = null;
                    URI predicate = null;
                    URI object = null;
                    Literal literal = null;
                    Statement statement = null;

                    JsonReader dmpJsonResult = Json.createReader(IOUtils.toInputStream(jsonResponse, "UTF-8"));
                    JsonArray records = dmpJsonResult.readArray();

                    for (JsonObject record : records.getValuesAs(JsonObject.class)) {

                        subject = factory
                                .createURI(record.getJsonString("__record_id").toString().replaceAll("\"", ""));

                        for (JsonObject triple : record.getJsonArray("__record_data")
                                .getValuesAs(JsonObject.class)) {

                            for (String key : triple.keySet()) {

                                if (key.endsWith("rdf-syntax-ns#type")) {
                                    predicate = RDF.TYPE;
                                    object = factory.createURI(
                                            triple.getJsonString(key).toString().replaceAll("\"", ""));
                                    statement = factory.createStatement(subject, predicate, object, graphUri);
                                    graph.add(statement);
                                } else {

                                    predicate = factory.createURI(key);

                                    switch (triple.get(key).getValueType().toString()) {

                                    case "STRING": {

                                        try {
                                            object = factory.createURI(
                                                    triple.getJsonString(key).toString().replaceAll("\"", ""));
                                            statement = factory.createStatement(subject, predicate, object,
                                                    graphUri);
                                            graph.add(statement);
                                        } catch (Exception e) {
                                            literal = factory.createLiteral(
                                                    triple.getJsonString(key).toString().replaceAll("\"", ""));
                                            statement = factory.createStatement(subject, predicate, literal,
                                                    graphUri);
                                            graph.add(statement);
                                        }
                                        break;
                                    }
                                    case "ARRAY": {

                                        for (JsonString value : triple.getJsonArray(key)
                                                .getValuesAs(JsonString.class)) {

                                            try {
                                                object = factory
                                                        .createURI(value.toString().replaceAll("\"", ""));
                                                statement = factory.createStatement(subject, predicate, object,
                                                        graphUri);
                                                graph.add(statement);
                                            } catch (Exception e) {
                                                literal = factory
                                                        .createLiteral(value.toString().replaceAll("\"", ""));
                                                statement = factory.createStatement(subject, predicate, literal,
                                                        graphUri);
                                                graph.add(statement);
                                            }
                                        }
                                        break;
                                    }
                                    default: {

                                        logger.info("Unhandled ValueType: " + triple.get(key).getValueType());
                                    }
                                    }
                                }
                            }
                        }
                    }

                    if (graph.size() > 0) {
                        // save rdf data as 'results.rdf.format' in 'results.folder'
                        RDFFormat format = null;
                        switch (config.getProperty("results.rdf.format")) {

                        case "xml": {

                            format = RDFFormat.RDFXML;
                            break;
                        }
                        case "nquads": {

                            format = RDFFormat.NQUADS;
                            break;
                        }
                        case "jsonld": {

                            format = RDFFormat.JSONLD;
                            break;
                        }
                        case "ttl": {

                            format = RDFFormat.TURTLE;
                            break;
                        }
                        default: {

                            format = RDFFormat.RDFXML;
                        }
                        }

                        try {
                            FileOutputStream out = new FileOutputStream(
                                    config.getProperty("results.folder") + File.separatorChar + dataModelID
                                            + "." + cnt + ".rdf." + config.getProperty("results.rdf.format"));
                            RDFWriter writer = Rio.createWriter(format, out);

                            writer.startRDF();
                            for (Statement st : graph) {
                                writer.handleStatement(st);
                            }
                            writer.endRDF();

                            out.close();

                        } catch (RDFHandlerException | IOException e) {
                            e.printStackTrace();
                        }

                        message = "'" + resource + "' transformed. results in '"
                                + config.getProperty("results.folder") + File.separatorChar + dataModelID + "."
                                + cnt + ".rdf." + config.getProperty("results.rdf.format") + "'";
                    } else {

                        message = "'" + resource + "' transformed but result is empty.";
                    }
                }
            } else {

                message = "'" + resource + "' not transformed: error in task execution.";
            }
        }
    } catch (Exception e) {

        logger.error("[" + config.getProperty("service.name") + "] Processing resource '" + resource
                + "' failed with a " + e.getClass().getSimpleName());
        e.printStackTrace();
    }

    return message;
}