Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java

/**
 * Creates a filter.// ww w  .  jav  a2  s.c  om
 *
 * @param cepico
 * @param req
 * @return the filter id 
 * @throws java.io.IOException 
 */
@PUT
@Path("createcepico")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCEPICO(String cepico, @Context HttpServletRequest req) throws IOException {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    JSONObject credentials = new JSONObject();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    credentials.put("username", req.getHeader("name"));
    credentials.put("password", req.getHeader("password"));
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    JSONObject jo = new JSONObject(cepico);
    if (!jo.has("source")) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("cepicos");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        mongo.close();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    if (jo.has("dolceSpecification")) {

        //Filter oFilter = new Filter(filter);
        JSONObject dsjo = jo.getJSONObject("dolceSpecification");
        String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}";

        try {

            DolceSpecification ds = new DolceSpecification(str);

            if (ds instanceof DolceSpecification) {
                UUID uuid = UUID.randomUUID();
                String randomUUIDString = uuid.toString();

                String mqin = RandomStringUtils.randomAlphanumeric(8);
                String mqout = RandomStringUtils.randomAlphanumeric(8);

                Date NOW = new Date();

                JSONArray requestArray;

                try {
                    requestArray = createCEPICORequests(jo.getJSONArray("source"), ds.getEvents(),
                            getXSDDateTime(NOW));
                } catch (Exception e) {
                    mongo.close();
                    return Response.status(Response.Status.BAD_REQUEST)
                            .entity("not available getObservation Service for this sensor ").build();
                }

                CEP cepProcess = new CEP();

                if (!(cepProcess.CEPStart(CEP.CEPType.CEPICO, ds, mqin, mqout, confFile,
                        requestArray.toString(), credentials))) {
                    mongo.close();
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                if (cepProcess.PID < 1) {
                    mongo.close();
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                DBObject dbObject = createCEPSensor(cepico, randomUUIDString, dsjo, cepProcess.id);

                Document doc = new Document(dbObject.toMap());

                try {
                    db.getCollection("cepicos").insertOne(doc);

                    JSONObject opState = createOperationalStateObservation(randomUUIDString);

                    String sensorId = host + "/sensor/" + randomUUIDString;

                    MessageProcessor_publisher Publisher_MsgProcc = new MessageProcessor_publisher(this.dmsURL,
                            cookie, sensorId, "cepicosobservations", mongoURL, mongoDB);//555
                    MQTT_connector_subscriper publisher = new MQTT_connector_subscriper(mqout,
                            Publisher_MsgProcc);
                    MqttConnectorContainer.addConnector(publisher.getClientName(), publisher);

                    DBObject oPut = (DBObject) JSON.parse(opState.toString());
                    Document doc1 = new Document(oPut.toMap());

                    try {
                        db.getCollection("cepicosobservations").insertOne(doc1);
                        String id = doc1.get("_id").toString();

                    } catch (MongoException ex) {
                        mongo.close();
                        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                    }

                    JSONObject aOutput = new JSONObject();
                    aOutput.put("id", host + "/sensor/" + randomUUIDString);
                    return Response.status(Response.Status.OK).entity(aOutput.toString()).build();

                } catch (MongoException ex) {
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }

            } else {
                mongo.close();
                return Response.status(Response.Status.BAD_REQUEST).build();
            }
        } catch (MongoException ex) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }

    } else {
        mongo.close();
        return Response.status(Response.Status.BAD_REQUEST).build();
    }

}

From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java

/**
 * Gets a filter.//  w  w  w.  j  a va 2  s  .c  om
 *
 * @return the filter 
 */
@POST
@Path("getcepico")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getCEPICO(String id) {

    JSONObject jo = new JSONObject(id);
    String idjo = jo.getString("id");

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("cepicos");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    BasicDBObject searchById = new BasicDBObject("id", idjo);
    String found = null;
    BasicDBObject fields = new BasicDBObject().append("_id", false).append("cepinstance", false);

    FindIterable<Document> coll = db.getCollection("cepicos").find(searchById).projection(fields);

    try {
        found = coll.first().toJson();
    } catch (Exception e) {
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.NOT_FOUND).build();
    }

    if (found == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    } else {
        return Response.status(Response.Status.OK).entity(found).build();
    }

}

From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java

/**
 * Gets a filter.//from  w w  w  . j av a 2 s  .  c  o  m
 *
 * @param filterId
 * @param req
 * @return the filter 
 */
@DELETE
@Path("deletecepico")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteCEPICO(String filterId, @Context HttpServletRequest req) throws IOException {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    JSONObject jo = new JSONObject(filterId);
    String idjo = jo.getString("id");

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("cepicos");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        mongo.close();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

    MongoCollection<Document> coll = db.getCollection("cepicos");

    Bson filter = Filters.eq("id", idjo);

    FindIterable<Document> iterable = coll.find(filter);

    String cepInstance;

    CEP cepProcess = new CEP();

    if (iterable != null && iterable.first() != null) {
        Document doc = iterable.first();
        cepInstance = doc.getString("cepinstance");

        MongoCollection<Document> collInstances = db.getCollection("cepinstances");

        ObjectId ci = new ObjectId(cepInstance);
        Bson filterInstances = Filters.eq("_id", ci);

        FindIterable<Document> iterable2 = collInstances.find(filterInstances);

        if (iterable2 != null) {
            Document doc2 = iterable2.first();
            cepProcess.PID = doc2.getInteger("PID");
            cepProcess.fileName = doc2.getString("fileName");
            cepProcess.cepFolder = doc2.getString("cepFolder");
            cepProcess.type = CEP.CEPType.CEPICO.toString();
            CepProcess cp = new CepProcess(null, null, null, null);
            cp.PID = doc2.getInteger("PID");

            cepProcess.cp = cp;

            if (!cepProcess.cepDispose()) {
                java.util.logging.Logger.getLogger(CEPICO.class.getName()).log(Level.SEVERE,
                        "bcep Instance not terminated");
            } else {

                Bson filter1 = Filters.eq("_id", ci);
                Bson update = new Document("$set", new Document("status", "terminated"));
                UpdateOptions options = new UpdateOptions().upsert(false);
                UpdateResult updateDoc = db.getCollection("cepinstances").updateOne(filter1, update, options);

            }
            ;

            CepContainer.deleteCepProcess(cp.PID);

        }
    } else {
        return Response.status(Response.Status.NOT_FOUND).build();
    }

    DeleteResult deleteResult = coll.deleteOne(eq("id", idjo));

    if (deleteResult.getDeletedCount() < 1) {
        return Response.status(Response.Status.NOT_FOUND).build();
    } else {
        return Response.status(Response.Status.OK).build();
    }
}

From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java

/**
 * Gets the filters.//  w  w  w . j a va 2s .c  om
 *
 * @return the filters
 */
@GET
@Path("getcontinuousfilters")
@Produces(MediaType.APPLICATION_JSON)
public Response getFilterings(@Context HttpServletRequest req) {

    //        StringBuilder ck = new StringBuilder();
    //        Security slogin = new Security();
    //                  
    //        Boolean token = slogin.login(req.getHeader("name")
    //                ,req.getHeader("password"),false,ck);
    //        if (!token){
    //              return Response.status(Response.Status.UNAUTHORIZED).build();
    //        }
    //        this.cookie = ck.toString(); 

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("continuousfilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    // create an empty query
    BasicDBObject query = new BasicDBObject();
    BasicDBObject fields = new BasicDBObject().append("_id", false);
    fields.append("dolceSpecification", false);

    FindIterable<Document> coll = db.getCollection("continuousfilters").find(query).projection(fields);

    final JSONArray AllJson = new JSONArray();

    coll.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            String aux = document.toJson();
            JSONObject sensoraux = new JSONObject(aux);
            AllJson.put(sensoraux);
        }
    });
    db = null;
    if (mongo != null) {
        mongo.close();
        mongo = null;
    }
    return Response.status(Response.Status.OK).entity(AllJson.toString()).build();

}

From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java

/**
 * Creates a filter./*from   w  ww .  ja  v  a  2  s.co  m*/
 *
 * @param filter
 * @param req
 * @return the filter id 
 * @throws java.io.IOException 
 */
@PUT
@Path("createcontinuousfilter")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createcontinuousfilter(String filter, @Context HttpServletRequest req) throws IOException {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    JSONObject credentials = new JSONObject();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    credentials.put("username", req.getHeader("name"));
    credentials.put("password", req.getHeader("password"));
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    JSONObject jo = new JSONObject(filter);
    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("continuousfilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

    if (jo.has("dolceSpecification")) {

        JSONObject dsjo = jo.getJSONObject("dolceSpecification");
        String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}";

        try {

            DolceSpecification ds = new DolceSpecification(str);

            if (ds instanceof DolceSpecification) {

                UUID uuid = UUID.randomUUID();
                String randomUUIDString = uuid.toString();

                String mqin = RandomStringUtils.randomAlphanumeric(8);
                String mqout = RandomStringUtils.randomAlphanumeric(8);

                CEP cepProcess = new CEP();

                if (!(cepProcess.CEPStart(CEP.CEPType.CONTINUOUS, ds, mqin, mqout, confFile,
                        jo.getJSONArray("source").toString(), credentials))) {
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                if (cepProcess.PID < 1) {
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                DBObject dbObject = createCEPFilterSensor(filter, randomUUIDString, dsjo, cepProcess.id);

                Document doc = new Document(dbObject.toMap());

                try {
                    db.getCollection("continuousfilters").insertOne(doc);

                    JSONObject aOutput = new JSONObject();
                    String sensorId = host + "/sensor/" + randomUUIDString;
                    aOutput.put("id", sensorId);

                    //MIGUEL
                    MessageProcessor_publisher Publisher_MsgProcc = new MessageProcessor_publisher(this.dmsURL,
                            this.cookie, sensorId, "continuosfiltersobservations", this.mongoURL, this.mongoDB);//555
                    MQTT_connector_subscriper publisher = new MQTT_connector_subscriper(mqout,
                            Publisher_MsgProcc);
                    MqttConnectorContainer.addConnector(publisher.getClientName(), publisher);

                    //TODO --> DESTROY DEL CONNECTOR.

                    //                    MqttConnectorContainer.deleteConnector(publisher
                    //                            .getClientName());

                    JSONObject opState = createOperationalStateObservation(randomUUIDString);

                    DBObject oPut = (DBObject) JSON.parse(opState.toString());
                    Document doc1 = new Document(oPut.toMap());

                    try {
                        db.getCollection("continuosfiltersobservations").insertOne(doc1);
                        String id = doc1.get("_id").toString();

                        db = null;
                        if (mongo != null) {
                            mongo.close();
                            mongo = null;
                        }

                    } catch (MongoException ex) {
                        db = null;
                        if (mongo != null) {
                            mongo.close();
                            mongo = null;
                        }
                        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                    }
                    return Response.status(Response.Status.OK).entity(aOutput.toString()).build();

                } catch (MongoException ex) {
                    db = null;
                    if (mongo != null) {
                        mongo.close();
                        mongo = null;
                    }
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }

            } else {
                return Response.status(Response.Status.BAD_REQUEST).build();
            }
        } catch (JSONException | IOException e) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
    }

    return Response.status(Response.Status.BAD_REQUEST).build();

}

From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java

/**
 * Gets a filter.//from ww  w . ja v a  2  s. co  m
 *
 * @param filterId
 * @param req
 * @return the filter 
 */
@POST
@Path("getcontinuousfilter")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getcontinuousfilter(String filterId, @Context HttpServletRequest req) {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();
    JSONObject jo = new JSONObject(filterId);
    String idjo = jo.getString("id");

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("continuousfilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

    BasicDBObject searchById = new BasicDBObject("id", idjo);
    String found;
    BasicDBObject fields = new BasicDBObject().append("_id", false).append("cepinstance", false);

    FindIterable<Document> coll = db.getCollection("continuousfilters").find(searchById).projection(fields);

    try {
        found = coll.first().toJson();
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
    } catch (Exception e) {
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.NOT_FOUND).build();
    }

    if (found == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    } else {
        return Response.status(Response.Status.OK).entity(found).build();
    }

}

From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java

/**
 * Gets a filter./*from   w w  w. j  av a  2 s . c o m*/
 *
 * @param filterId
 * @param req
 * @return the filter 
 */
@DELETE
@Path("deletecontinuousfilter")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deletecontinuousfilter(String filterId, @Context HttpServletRequest req) throws IOException {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    JSONObject jo = new JSONObject(filterId);
    String idjo = jo.getString("id");

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("continuousfilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    MongoCollection<Document> coll = db.getCollection("continuousfilters");

    Bson filter = Filters.eq("id", idjo);

    FindIterable<Document> iterable = coll.find(filter);

    String cepInstance;

    CEP cepProcess = new CEP();

    if (iterable != null && iterable.first() != null) {
        Document doc = iterable.first();
        cepInstance = doc.getString("cepinstance");

        MongoCollection<Document> collInstances = db.getCollection("cepinstances");

        ObjectId ci = new ObjectId(cepInstance);
        Bson filterInstances = Filters.eq("_id", ci);

        FindIterable<Document> iterable2 = collInstances.find(filterInstances);

        if (iterable2 != null) {
            Document doc2 = iterable2.first();
            cepProcess.PID = doc2.getInteger("PID");
            cepProcess.fileName = doc2.getString("fileName");
            cepProcess.cepFolder = doc2.getString("cepFolder");
            cepProcess.type = CEP.CEPType.CONTINUOUS.toString();
            CepProcess cp = new CepProcess(null, null, null, null);
            cp.PID = doc2.getInteger("PID");

            cepProcess.cp = cp;

            if (!cepProcess.cepDispose()) {
                java.util.logging.Logger.getLogger(ContinuosFiltering.class.getName()).log(Level.SEVERE,
                        "bcep Instance not terminated");
            } else {

                Bson filter1 = Filters.eq("_id", ci);
                Bson update = new Document("$set", new Document("status", "terminated"));
                UpdateOptions options = new UpdateOptions().upsert(false);
                UpdateResult updateDoc = db.getCollection("cepinstances").updateOne(filter1, update, options);

            }
            ;
            CepContainer.deleteCepProcess(cp.PID);

        }
    }

    DeleteResult deleteResult = coll.deleteOne(eq("id", idjo));
    db = null;
    if (mongo != null) {
        mongo.close();
        mongo = null;
    }
    if (deleteResult.getDeletedCount() < 1) {
        return Response.status(Response.Status.NOT_FOUND).build();
    } else {

        return Response.status(Response.Status.OK).build();
    }
}

From source file:eu.vital.vitalcep.restApp.filteringApi.StaticFiltering.java

/**
 * Creates a filter./* w  ww .  j  a va2 s . c o  m*/
 *
 * @param info
 * @return the filter id 
 * @throws java.io.IOException 
 */
@POST
@Path("filterstaticdata")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response filterstaticdata(String info, @Context HttpServletRequest req)
        throws IOException, UnsupportedEncodingException, NoSuchAlgorithmException {

    JSONObject jo = new JSONObject(info);

    if (jo.has("dolceSpecification") && jo.has("data")) { // && jo.has("data") for demo

        MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
        MongoDatabase db = mongo.getDatabase(mongoDB);

        try {
            db.getCollection("staticdatafilters");
        } catch (Exception e) {
            //System.out.println("Mongo is down");
            db = null;
            if (mongo != null) {
                mongo.close();
                mongo = null;
            }
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

        }

        if (jo.has("dolceSpecification")) {

            //Filter oFilter = new Filter(filter);
            JSONObject dsjo = jo.getJSONObject("dolceSpecification");
            String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}";

            try {

                DolceSpecification ds = new DolceSpecification(str);

                if (!(ds instanceof DolceSpecification)) {
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }

                String mqin = RandomStringUtils.randomAlphanumeric(8);
                String mqout = RandomStringUtils.randomAlphanumeric(8);

                JSONArray aData = jo.getJSONArray("data");

                CEP cepProcess = new CEP();

                if (!(cepProcess.CEPStart(CEP.CEPType.DATA, ds, mqin, mqout, confFile, aData.toString(),
                        null))) {
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                String clientName = "collector_" + RandomStringUtils.randomAlphanumeric(4);

                if (cepProcess.PID < 1) {
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                UUID uuid = UUID.randomUUID();
                String randomUUIDString = uuid.toString();

                DBObject dbObject = createCEPFilterStaticSensorJsonld(info, randomUUIDString, jo, dsjo,
                        "vital:CEPFilterStaticDataSensor");
                Document doc = new Document(dbObject.toMap());

                try {
                    db.getCollection("staticdatafilters").insertOne(doc);
                    String id = doc.get("_id").toString();

                } catch (MongoException ex) {
                    db = null;
                    if (mongo != null) {
                        mongo.close();
                        mongo = null;
                    }
                    return Response.status(Response.Status.BAD_REQUEST).build();
                }

                JSONObject opState = createOperationalStateObservation(randomUUIDString);

                DBObject oPut = (DBObject) JSON.parse(opState.toString());
                Document doc1 = new Document(oPut.toMap());

                try {
                    db.getCollection("staticdatafiltersobservations").insertOne(doc1);
                    String id = doc1.get("_id").toString();

                } catch (MongoException ex) {
                    db = null;
                    if (mongo != null) {
                        mongo.close();
                        mongo = null;
                    }
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
                }

                /////////////////////////////////////////////////////
                // creates client and messages process
                //
                MqttAllInOne oMqtt = new MqttAllInOne();
                TMessageProc MsgProcc = new TMessageProc();

                /////////////////////////////////////////////////////////////////////////
                // PREPARING DOLCE INPUT
                Decoder decoder = new Decoder();
                ArrayList<String> simpleEventAL = decoder.JsonldArray2DolceInput(aData);

                String sal = simpleEventAL.toString();
                /////////////////////////////////////////////////////////////////////////////
                // SENDING TO MOSQUITTO
                oMqtt.sendMsg(MsgProcc, clientName, simpleEventAL, mqin, mqout, false);

                /////////////////////////////////////////////////////////////////////////////
                //RECEIVING FROM MOSQUITO               
                ArrayList<MqttMsg> mesagges = MsgProcc.getMsgs();

                ArrayList<Document> outputL;
                outputL = new ArrayList<>();

                Encoder encoder = new Encoder();

                outputL = encoder.dolceOutputList2ListDBObject(mesagges, host, randomUUIDString);

                String sOutput = "[";
                for (int i = 0; i < outputL.size(); i++) {
                    Document element = outputL.get(i);

                    if (i == 0) {
                        sOutput = sOutput + element.toJson();
                    }
                    sOutput = sOutput + "," + element.toJson();
                }

                sOutput = sOutput + "]";

                StringBuilder ck = new StringBuilder();

                try {
                    Security slogin = new Security();

                    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
                    if (!token) {
                        return Response.status(Response.Status.UNAUTHORIZED).build();
                    }
                    cookie = ck.toString();

                    DMSManager oDMS = new DMSManager(dmsURL, cookie);

                    MongoCollection<Document> collection = db.getCollection("staticdatafiltersobservations");

                    if (outputL.size() > 0) {
                        collection.insertMany(outputL);
                        if (!oDMS.pushObservations(sOutput)) {
                            java.util.logging.Logger.getLogger(StaticFiltering.class.getName())
                                    .log(Level.SEVERE, "couldn't save to the DMS");
                        }
                    }

                } catch (KeyManagementException | KeyStoreException ex) {
                    db = null;
                    if (mongo != null) {
                        mongo.close();
                        mongo = null;
                    }
                    java.util.logging.Logger.getLogger(MessageProcessor_publisher.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
                //cepProcess.
                try {
                    CepContainer.deleteCepProcess(cepProcess.PID);

                    if (!cepProcess.cepDispose()) {
                        java.util.logging.Logger.getLogger(StaticFiltering.class.getName()).log(Level.SEVERE,
                                "couldn't terminate ucep");
                    }
                } catch (Exception e) {
                    java.util.logging.Logger.getLogger(StaticFiltering.class.getName()).log(Level.SEVERE, null,
                            e);
                }
                db = null;
                if (mongo != null) {
                    mongo.close();
                    mongo = null;
                }
                return Response.status(Response.Status.OK).entity(sOutput).build();

            } catch (IOException | JSONException | java.text.ParseException e) {
                db = null;
                if (mongo != null) {
                    mongo.close();
                    mongo = null;
                }
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
            }
        }

        return Response.status(Response.Status.BAD_REQUEST).build();

    }

    return Response.status(Response.Status.BAD_REQUEST).build();

}

From source file:eu.vital.vitalcep.restApp.filteringApi.StaticFiltering.java

/**
  * Gets the filters./* w  ww  .  j  ava 2  s.co m*/
  *
  * @return the filters
  */
@GET
@Path("getstaticdatafilters")
@Produces(MediaType.APPLICATION_JSON)
public Response getStaticDataFilters(@Context HttpServletRequest req) {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("staticdatafilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    // create an empty query
    BasicDBObject query = new BasicDBObject();
    BasicDBObject fields = new BasicDBObject().append("_id", false);
    fields.append("dolceSpecification", false);

    FindIterable<Document> coll = db.getCollection("staticdatafilters").find(query).projection(fields);

    final JSONArray AllJson = new JSONArray();

    coll.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            String aux = document.toJson();
            JSONObject sensoraux = new JSONObject(aux);
            AllJson.put(sensoraux);
        }
    });
    db = null;
    if (mongo != null) {
        mongo.close();
        mongo = null;
    }
    return Response.status(Response.Status.OK).entity(AllJson.toString()).build();

}

From source file:eu.vital.vitalcep.restApp.filteringApi.StaticFiltering.java

/**
 * Gets the filters.//  w  w w .  j  a  va2 s .c  o m
 *
 * @return the filters
 */
@GET
@Path("getstaticqueryfilters")
@Produces(MediaType.APPLICATION_JSON)
public Response getStaticQueryFilters(@Context HttpServletRequest req) {

    StringBuilder ck = new StringBuilder();
    Security slogin = new Security();

    Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck);
    if (!token) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    this.cookie = ck.toString();

    MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase db = mongo.getDatabase(mongoDB);

    try {
        db.getCollection("staticqueryfilters");
    } catch (Exception e) {
        //System.out.println("Mongo is down");
        db = null;
        if (mongo != null) {
            mongo.close();
            mongo = null;
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    }

    // create an empty query
    BasicDBObject query = new BasicDBObject();
    BasicDBObject fields = new BasicDBObject().append("_id", false);
    fields.append("dolceSpecification", false);

    FindIterable<Document> coll = db.getCollection("staticqueryfilters").find(query).projection(fields);

    final JSONArray AllJson = new JSONArray();

    coll.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            String aux = document.toJson();
            JSONObject sensoraux = new JSONObject(aux);
            AllJson.put(sensoraux);
        }
    });
    db = null;
    if (mongo != null) {
        mongo.close();
        mongo = null;
    }
    return Response.status(Response.Status.OK).entity(AllJson.toString()).build();

}