Example usage for com.mongodb.util JSON parse

List of usage examples for com.mongodb.util JSON parse

Introduction

In this page you can find the example usage for com.mongodb.util JSON parse.

Prototype

public static Object parse(final String jsonString) 

Source Link

Document

Parses a JSON string and returns a corresponding Java object.

Usage

From source file:eu.cassandra.training.entities.Appliance.java

License:Apache License

/**
 * Creating a JSON object out of the appliance's active and reactive active
 * consumption models./*from   w w w .ja v  a2 s . co m*/
 * 
 * @return the JSON object created from the active and reactive active
 *         consumption models.
 */
public DBObject powerConsumptionModelToJSON() {
    DBObject temp = new BasicDBObject();

    temp.put("name", name + " Consumption Model");
    temp.put("type", type);
    temp.put("description", "P and Q Consumption Model");
    temp.put("app_id", applianceID);
    temp.put("pmodel", JSON.parse(activeConsumptionModelString));
    temp.put("qmodel", JSON.parse(reactiveConsumptionModelString));
    temp.put("pvalues", new double[1]);
    temp.put("qvalues", new double[1]);
    return temp;

}

From source file:eu.cassandra.training.utils.APIUtilities.java

License:Apache License

/**
 * This function is used to send the entity models to the Cassandra Server,
 * specifically on the connected user's Library.
 * /* w w w .j  a va2  s. com*/
 * @param message
 *          The JSON schema of the entity.
 * @param suffix
 *          The library the model must be sent to.
 * @return the id of the entity model provided by the server.
 * @throws IOException
 * @throws AuthenticationException
 * @throws NoSuchAlgorithmException
 */
public static String sendEntity(String message, String suffix)
        throws IOException, AuthenticationException, NoSuchAlgorithmException {

    System.out.println(message);
    HttpPost httppost = new HttpPost(url + suffix);

    StringEntity entity = new StringEntity(message, "UTF-8");
    entity.setContentType("application/json");
    httppost.setEntity(entity);
    System.out.println("executing request: " + httppost.getRequestLine());

    HttpResponse response = httpclient.execute(httppost, localcontext);
    HttpEntity responseEntity = response.getEntity();
    String responseString = EntityUtils.toString(responseEntity, "UTF-8");
    System.out.println(responseString);

    DBObject dbo = (DBObject) JSON.parse(responseString);

    DBObject dataObj = (DBObject) dbo.get("data");

    return dataObj.get("_id").toString();

}

From source file:eu.cassandra.training.utils.APIUtilities.java

License:Apache License

/**
 * This function is used to send the user's credentials to the Cassandra
 * Server./*from  w  w w.  ja  v  a 2 s  .c om*/
 * 
 * @param username
 *          The username of the user in the server.
 * @param password
 *          The password of the user in the server.
 * @return true if connected, else false.
 * @throws Exception
 */
public static boolean sendUserCredentials(String username, char[] password) throws Exception {

    String pass = String.valueOf(password);

    try {
        UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username,
                pass);

        char SEP = File.separatorChar;
        File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security");
        File file = new File(dir, "jssecacerts");
        if (file.isFile() == false) {
            InstallCert.createCertificate("160.40.50.233", 8443);
            JFrame success = new JFrame();

            JOptionPane.showMessageDialog(success,
                    "Certificate was created for user " + username + ". Now the connection will start",
                    "Response Model Exported", JOptionPane.INFORMATION_MESSAGE);
        }

        try {
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        } catch (Exception e1) {
        }

        Scheme scheme = new Scheme("https", 8443, sf);
        httpclient.getConnectionManager().getSchemeRegistry().register(scheme);

        HttpGet httpget = new HttpGet(url + "/usr");
        httpget.addHeader(new BasicScheme().authenticate(usernamePasswordCredentials, httpget, localcontext));

        System.out.println("executing request: " + httpget.getRequestLine());

        HttpResponse response = httpclient.execute(httpget, localcontext);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);

        DBObject dbo = (DBObject) JSON.parse(responseString);

        if (dbo.get("success").toString().equalsIgnoreCase("true")) {

            BasicDBList dataObj = (BasicDBList) dbo.get("data");

            DBObject dbo2 = (DBObject) dataObj.get(0);

            userID = dbo2.get("usr_id").toString();

            System.out.println("userId: " + userID);

            return true;
        } else {
            System.out.println(false);
            return false;
        }

    } finally {
    }

}

From source file:eu.eubrazilcc.lvl.storage.dao.DatasetDAO.java

License:EUPL

private DBObject fromMetadata(final Metadata metadata) {
    DBObject obj = null;//ww w.j av a  2 s.  co m
    if (metadata != null) {
        try {
            obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(metadata));
        } catch (JsonProcessingException e) {
            LOGGER.error("Failed to write dataset to DB object", e);
        }
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.dao.LvlInstanceDAO.java

License:EUPL

private DBObject map(final SameTransientStore<LvlInstance> store) {
    DBObject obj = null;// w  ww .j ava2s  .co  m
    try {
        obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(new LvlInstanceEntity(store.purge())));
    } catch (JsonProcessingException e) {
        LOGGER.error("Failed to write instance to DB object", e);
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.dao.NotificationDAO.java

License:EUPL

private DBObject map(final NotificationTransientStore store) {
    DBObject obj = null;//  ww  w .j  a v  a 2 s.c o m
    try {
        obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(new NotificationEntity(store.purge())));
    } catch (JsonProcessingException e) {
        LOGGER.error("Failed to write notification to DB object", e);
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.dao.ReferenceDAO.java

License:EUPL

private DBObject map(final SameTransientStore<Reference> store) {
    DBObject obj = null;/*from w  w w  . j av a 2s . co  m*/
    try {
        obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(new ReferenceEntity(store.purge())));
    } catch (JsonProcessingException e) {
        LOGGER.error("Failed to write reference to DB object", e);
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.dao.SavedSearchDAO.java

License:EUPL

private DBObject map(final SameTransientStore<SavedSearch> store) {
    DBObject obj = null;//from  w ww.j  av a 2  s.com
    try {
        obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(new SavedSearchEntity(store.purge())));
    } catch (JsonProcessingException e) {
        LOGGER.error("Failed to write saved search to DB object", e);
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.dao.WorkflowRunDAO.java

License:EUPL

private DBObject map(final SameTransientStore<WorkflowRun> store) {
    DBObject obj = null;//from w w w .j ava2  s.  c  o m
    try {
        obj = (DBObject) JSON.parse(JSON_MAPPER.writeValueAsString(new WorkflowRunEntity(store.purge())));
    } catch (JsonProcessingException e) {
        LOGGER.error("Failed to write workflow run to DB object", e);
    }
    return obj;
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

public String createOpenAccessLink(final @Nullable String namespace, final String filename) {
    checkArgument(isNotBlank(filename), "Uninitialized or invalid filename");
    final String namespace2 = trimToEmpty(namespace);
    final String filename2 = filename.trim();
    final String secret = random(32, "abcdefghijklmnopqrstuvwxyz0123456789");
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace2) ? new GridFS(db, namespace2) : new GridFS(db);
    try {//from  w  w  w  .j a va 2  s .c  om
        final GridFSDBFile latestUploadedVersion = getLatestUploadedFile(gfsNs, filename2);
        checkState(latestUploadedVersion != null, "File not found");
        if (latestUploadedVersion.getMetaData() == null) {
            latestUploadedVersion.setMetaData(new BasicDBObject());
        }
        latestUploadedVersion.getMetaData().put(OPEN_ACCESS_LINK_ATTR, secret);
        latestUploadedVersion.getMetaData().put(OPEN_ACCESS_DATE_ATTR,
                JSON.parse(JSON_MAPPER.writeValueAsString(new Date())));
        latestUploadedVersion.save();
    } catch (IllegalStateException ise) {
        throw ise;
    } catch (Exception e) {
        LOGGER.error("Failed to create open access link in latest file version in namespace="
                + gfsNs.getBucketName() + ", filename=" + filename, e);
    }
    return secret;
}