Example usage for com.mongodb.util JSONParseException getMessage

List of usage examples for com.mongodb.util JSONParseException getMessage

Introduction

In this page you can find the example usage for com.mongodb.util JSONParseException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:eu.cassandra.server.api.Projects.java

License:Apache License

/**
 * Create a project//  w ww .  j av  a 2s  .co m
 */
@POST
public Response createProject(String message, @Context HttpHeaders httpHeaders) {
    String usr_id = Utils.userChecked(httpHeaders);
    if (usr_id != null) {
        String patchedMessage = message;
        try {
            patchedMessage = Utils.inject(message, "usr_id", usr_id);
        } catch (com.mongodb.util.JSONParseException e) {
            //JSONtoReturn jSON2Rrn = new JSONtoReturn();
            //PrettyJSONPrinter.prettyPrint(jSON2Rrn.createJSONError("Error parsing JSON input", e.getMessage()));
            Response.status(Response.Status.BAD_REQUEST).entity("Error parsing JSON input - " + e.getMessage())
                    .build();
        } catch (Exception e) {
            //JSONtoReturn jSON2Rrn = new JSONtoReturn();
            //PrettyJSONPrinter.prettyPrint(jSON2Rrn.createJSONError(patchedMessage, e));
            Response.status(Response.Status.BAD_REQUEST).entity(patchedMessage + " - " + e.getMessage())
                    .build();
        }
        String json = PrettyJSONPrinter.prettyPrint(new MongoProjects().createProject(patchedMessage));
        return Response.ok(json, MediaType.APPLICATION_JSON).build();
    } else {
        return Response.status(Response.Status.UNAUTHORIZED).entity("User and or password do not match")
                .build();
    }
}

From source file:eu.cassandra.server.mongo.csn.MongoCluster.java

License:Apache License

/**
 * // w  w  w  . j  a  va2  s.co  m
 * @param graph_id
 * @param method
 * @param clusters
 * @param httpHeaders
 * @return
 */
private DBObject saveClusters(String graph_id, String run_id, String method,
        HashMap<Integer, Vector<String>> clusters, List<CEdge> edgesRemoved, String name,
        String clusterbasedon) {
    JSONtoReturn jSON2Rrn = new JSONtoReturn();
    DBObject clusterObject = new BasicDBObject("clustermethod", method);
    try {
        ObjectId objectId = new ObjectId();
        clusterObject.put("_id", objectId);
        clusterObject.put("graph_id", graph_id);
        clusterObject.put("run_id", run_id);
        clusterObject.put("name", name);
        clusterObject.put("clusterbasedon", clusterbasedon);

        Vector<DBObject> nodes = new Vector<DBObject>();
        HashMap<String, String> map = new HashMap<String, String>();
        for (Vector<String> cluster : clusters.values()) {
            for (int i = 0; i < cluster.size(); i++) {
                String id = cluster.get(i);
                DBObject node = new BasicDBObject("_id", new ObjectId());
                map.put(id, node.get("_id").toString());
                nodes.add(node);
            }
        }
        Vector<DBObject> edges = new Vector<DBObject>();
        for (Vector<String> cluster : clusters.values()) {
            for (int i = 0; i < cluster.size(); i++) {
                for (int j = i + 1; j < cluster.size(); j++) {
                    String id = cluster.get(i);
                    String id2 = cluster.get(j);
                    DBObject edge = new BasicDBObject("_id", new ObjectId());
                    edge.put("node_id2", map.get(id2));
                    edge.put("node_id1", map.get(id));
                    edges.add(edge);
                }
            }
        }
        String img = new SaveGraphImg().saveImg(nodes, edges);
        clusterObject.put("img", img);

        Vector<DBObject> allClusters = new Vector<DBObject>();

        clusterObject.put("n", clusters.keySet().size());
        for (int j = 0; j < clusters.keySet().size(); j++) {
            DBObject cl = new BasicDBObject();
            cl.put("name", "cluster" + j);
            cl.put("pricing_id", "");
            cl.put("installations", clusters.get(j));

            allClusters.add(cl);
            //clusterObject.put("cluster_" + j, clusters.get(j));
        }
        clusterObject.put("clusters", allClusters);
        DBConn.getConn().getCollection(MongoGraphs.COL_CSN_CLUSTERS).insert(clusterObject);

        //Inverse (node to cluster) 
        for (int j = 0; j < clusters.keySet().size(); j++) {
            Vector<String> nodeIDs = clusters.get(j);
            for (String nodeID : nodeIDs) {
                DBObject ob = new BasicDBObject("node_id", nodeID);
                ob.put("cluster", "cluster_" + j);
                ob.put("graph_id", graph_id);
                ob.put("clustersid", objectId.toString());
                if (run_id != null)
                    ob.put("run_id", run_id);
                DBConn.getConn().getCollection(MongoGraphs.COL_CSN_NODES2CLUSTERS).insert(ob);
            }
        }

        //Edges Removed
        if (edgesRemoved != null) {
            for (int i = 0; i < edgesRemoved.size(); i++) {
                String edgeID = edgesRemoved.get(i).getEdgeId();
                DBObject ob = new BasicDBObject("edge_id", edgeID);
                ob.put("graph_id", graph_id);
                ob.put("clustersid", objectId.toString());
                if (run_id != null)
                    ob.put("run_id", run_id);
                DBConn.getConn().getCollection(MongoGraphs.COL_CSN_EDGES_REMOVED).insert(ob);
            }
        }
        return jSON2Rrn.createJSONInsertPostMessage("Clusters Created", clusterObject);
    } catch (com.mongodb.util.JSONParseException e) {
        return jSON2Rrn.createJSONError("Error parsing JSON input", e.getMessage());
    } catch (Exception e) {
        return jSON2Rrn.createJSONError(clusterObject.toString(), e);
    }
}

From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java

License:Apache License

/**
 * //from ww  w . j  av  a 2 s  . c om
 * @param coll
 * @param dataToInsert
 * @param successMessage
 * @param refColl
 * @param refKeyName
 * @param canBeNull
 * @param schemaType
 * @param httpHeaders
 * @return
 */
public DBObject insertData(String coll, String dataToInsert, String successMessage, String[] refColl,
        String[] refKeyName, boolean[] canBeNull, int schemaType, HttpHeaders httpHeaders) {
    DBObject data;
    try {
        data = (DBObject) JSON.parse(dataToInsert);
        if (data.containsField("_id")) {
            data.removeField("_id");
            dataToInsert = data.toString();
        }
        new JSONValidator().isValid(dataToInsert, schemaType);
        if (refColl != null && refKeyName != null) {
            for (int i = 0; i < refColl.length; i++) {
                ensureThatRefKeyExists(data, refColl[i], refKeyName[i], canBeNull[i]);
            }
        }
        if (httpHeaders == null)
            DBConn.getConn().getCollection(coll).insert(data);
        else
            DBConn.getConn(MongoDBQueries.getDbNameFromHTTPHeader(httpHeaders)).getCollection(coll)
                    .insert(data);
    } catch (com.mongodb.util.JSONParseException e) {
        return jSON2Rrn.createJSONError("Error parsing JSON input", e.getMessage());
    } catch (Exception e) {
        return jSON2Rrn.createJSONError(dataToInsert, e);
    }
    return jSON2Rrn.createJSONInsertPostMessage(successMessage, data);
}

From source file:org.exist.mongodb.xquery.mongodb.collection.Aggregate.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//from   ww  w  .  j av  a 2  s.  c  o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();
        List<DBObject> pipeline = convertPipeline(args[3]);

        // Get collection in database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Execute query      
        AggregationOutput aggrOutput = dbcol.aggregate(pipeline);

        // Parse results
        Sequence retVal = new ValueSequence();

        for (DBObject result : aggrOutput.results()) {
            retVal.add(new StringValue(result.toString()));
        }

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Count.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/* w  w w.j  a va2 s.c  om*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get query when available
        String query = (args.length == 4) ? args[3].itemAt(0).getStringValue() : null;
        BasicDBObject mongoQuery = (query == null) ? null : (BasicDBObject) JSON.parse(query);

        // Get database and collection
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Count documents
        Long nrOfDocuments = (mongoQuery == null) ? dbcol.count() : dbcol.count(mongoQuery);

        return new IntegerValue(nrOfDocuments);

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Drop.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//from  w  w  w.  j  a v a  2 s .  c  o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        // Get database and collection
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        dbcol.drop();

        return Sequence.EMPTY_SEQUENCE;

    } catch (JSONParseException ex) {
        String msg = "Invalid JSON data: " + ex.getMessage();
        LOG.error(msg);
        throw new XPathException(this, MongodbModule.MONG0004, msg);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw new XPathException(this, MongodbModule.MONG0003, t.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.Find.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//from  w ww  .java  2  s.  c  o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        String query = (args.length >= 4) ? args[3].itemAt(0).getStringValue() : null;

        String keys = (args.length >= 5) ? args[4].itemAt(0).getStringValue() : null;

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // PLace holder result cursor
        DBCursor cursor;

        if (query == null) {
            // No query
            cursor = dbcol.find();

        } else {
            // Parse query
            BasicDBObject mongoQuery = (BasicDBObject) JSON.parse(query);

            // Parse keys when available
            BasicDBObject mongoKeys = (keys == null) ? null : (BasicDBObject) JSON.parse(keys);

            // Call correct method
            cursor = (mongoKeys == null) ? dbcol.find(mongoQuery) : dbcol.find(mongoQuery, mongoKeys);
        }

        // Execute query
        Sequence retVal = new ValueSequence();

        // Harvest results
        try {
            while (cursor.hasNext()) {
                retVal.add(new StringValue(cursor.next().toString()));
            }
        } finally {
            cursor.close();
        }

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.FindAndModify.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//from ww w  . j a  v a2s  .c  o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        BasicDBObject query = (args.length >= 4)
                ? (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue())
                : null;

        BasicDBObject update = (args.length >= 5)
                ? (BasicDBObject) JSON.parse(args[4].itemAt(0).getStringValue())
                : null;

        BasicDBObject sort = (args.length >= 6) ? (BasicDBObject) JSON.parse(args[5].itemAt(0).getStringValue())
                : null;

        //             Map<String,Boolean> options = (args.length >= 7)
        //                    ? convertOptions((AbstractMapType) args[6].itemAt(0))
        //                    : null;

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        //query update sort

        DBObject result;
        if (sort == null /* && options==null */) {
            result = dbcol.findAndModify(query, update);

        } else /* if (options==null) */ {
            result = dbcol.findAndModify(query, sort, update);
        }

        //            else {
        //               options.putIfAbsent("remove", unordered);
        //               options.putIfAbsent("returnNew", unordered);
        //               options.putIfAbsent("update", unordered);
        //               options.putIfAbsent("upsert", unordered);
        //                
        //               dbcol.findAndModify(query, sort, update, unordered, result, unordered, unordered);
        //            }

        // Execute query
        Sequence retVal = (result == null) ? Sequence.EMPTY_SEQUENCE : new StringValue(result.toString());

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.FindAndRemove.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from   w ww. j  av  a2  s  .com*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        BasicDBObject query = (args.length >= 4)
                ? (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue())
                : null;

        // Get collection in database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Execute query      
        DBObject result = dbcol.findAndRemove(query);

        // Parse results
        Sequence retVal = (result == null) ? Sequence.EMPTY_SEQUENCE : new StringValue(result.toString());

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}

From source file:org.exist.mongodb.xquery.mongodb.collection.FindOne.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*w  ww .j ava  2  s  .  c o m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        BasicDBObject query = (args.length >= 4)
                ? (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue())
                : null;

        BasicDBObject fields = (args.length >= 5)
                ? (BasicDBObject) JSON.parse(args[4].itemAt(0).getStringValue())
                : null;

        BasicDBObject orderBy = (args.length >= 6)
                ? (BasicDBObject) JSON.parse(args[5].itemAt(0).getStringValue())
                : null;

        // Check id
        MongodbClientStore.getInstance().validate(mongodbClientId);

        // Get database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        DBObject result;
        if (fields == null && orderBy == null && query == null) {
            result = dbcol.findOne();

        } else if (fields == null && orderBy == null) {
            result = dbcol.findOne(query);

        } else if (orderBy == null) {
            result = dbcol.findOne(query, fields);

        } else {
            result = dbcol.findOne(query, fields, orderBy);
        }

        // Execute query
        Sequence retVal = (result == null) ? Sequence.EMPTY_SEQUENCE : new StringValue(result.toString());

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}