Example usage for com.mongodb.gridfs GridFS createFile

List of usage examples for com.mongodb.gridfs GridFS createFile

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS createFile.

Prototype

public GridFSInputFile createFile(final String filename) 

Source Link

Document

Creates a file entry.

Usage

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

private void saveMessage(ShsMessageEntry entry, ShsMessage message, DB db) {
    GridFS gridFs = new GridFS(db);
    GridFSInputFile inputFile = gridFs.createFile(entry.getId());

    OutputStream out = null;//from www . j  av a 2s.com
    try {
        out = inputFile.getOutputStream();

        new ShsMessageMarshaller().marshal(message, out);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:xbdd.webapp.resource.feature.Report.java

License:Apache License

/**
 * go through all the embedded content, store it to GridFS, replace the doc embeddings with a hyperlink to the saved content.
 *//*from  w ww  .java2 s. c o m*/
protected void embedSteps(final DBObject feature, final GridFS gridFS, final Coordinates coordinates) {
    final BasicDBList elements = (BasicDBList) feature.get("elements");
    final String featureId = (String) feature.get("_id");
    if (elements != null) {
        for (int j = 0; j < elements.size(); j++) {
            final DBObject scenario = (DBObject) elements.get(j);
            final String scenarioId = (String) scenario.get("_id");
            final BasicDBList steps = (BasicDBList) scenario.get("steps");
            if (steps != null) {
                for (int k = 0; k < steps.size(); k++) {
                    final DBObject step = (DBObject) steps.get(k);
                    final BasicDBList embeddings = (BasicDBList) step.get("embeddings");
                    if (embeddings != null) {
                        for (int l = 0; l < embeddings.size(); l++) {
                            final DBObject embedding = (DBObject) embeddings.get(l);
                            final GridFSInputFile image = gridFS.createFile(
                                    Base64.decodeBase64(((String) embedding.get("data")).getBytes()));
                            image.setFilename(guid());
                            final BasicDBObject metadata = new BasicDBObject()
                                    .append("product", coordinates.getProduct())
                                    .append("major", coordinates.getMajor())
                                    .append("minor", coordinates.getMinor())
                                    .append("servicePack", coordinates.getServicePack())
                                    .append("build", coordinates.getBuild()).append("feature", featureId)
                                    .append("scenario", scenarioId);
                            image.setMetaData(metadata);
                            image.setContentType((String) embedding.get("mime_type"));
                            image.save();
                            embeddings.put(l, image.getFilename());
                        }
                    }
                }
            }
        }
    }
}

From source file:xbdd.webapp.resource.feature.UploadAttachment.java

License:Apache License

@SuppressWarnings("unchecked")
@POST/* w w w. j a  v  a 2s  .  c om*/
@Path("/{elementId}/{report}/{version}/{build}/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String setAttachment(@PathParam("report") final String report,
        @PathParam("version") final String version, @PathParam("build") final String build,
        @PathParam("id") final String id, @PathParam("elementId") final String elementId,
        @FormDataParam("attachmentfile") final File file,
        @FormDataParam("attachmentfile") final FormDataBodyPart body, @Context final HttpServletRequest req)
        throws IOException {
    try (final InputStream is = new FileInputStream(file.getAbsolutePath())) {
        final String elementIdMod = elementId.replace("&2F", "/");
        final DB gridDB = this.client.getDB("grid");
        final GridFS gridFS = new GridFS(gridDB);
        final GridFSInputFile gridFile = gridFS.createFile(is);
        gridFile.setFilename(
                body.getMediaType().toString().split("/")[0] + ".x1.mu." + UUID.randomUUID().toString());
        gridFile.setContentType(body.getMediaType().toString());
        gridFile.save();
        final DB bddDB = this.client.getDB("bdd");
        final DBCollection collection = bddDB.getCollection("features");
        // // get object
        final String featureId = report + "/" + version + "/" + build + "/" + id;
        final DBObject feature = collection.findOne(new BasicDBObject("_id", featureId));
        final BasicDBList elements = (BasicDBList) feature.get("elements");
        String scenarioName = "";
        if (elements != null) {
            for (int i = 0; i < elements.size(); i++) {
                final DBObject scenario = (DBObject) elements.get(i);
                if (scenario.get("id").equals(id + ";" + elementIdMod)) {
                    scenarioName = (String) scenario.get("name");
                    // get steps
                    final BasicDBList steps = (BasicDBList) scenario.get("steps");
                    final DBObject laststep = (DBObject) steps.get(steps.size() - 1);
                    List<String> embeddings = new ArrayList<String>();
                    if (laststep.containsField("embeddings")) {
                        embeddings = (ArrayList<String>) laststep.get("embeddings");
                    }
                    embeddings.add(gridFile.getFilename()); // get existng then add to them
                    laststep.put("embeddings", embeddings);
                    steps.set(steps.size() - 1, laststep);
                    scenario.put("steps", steps);
                    elements.set(i, scenario);
                }
            }
        }
        feature.put("elements", elements);
        feature.put("statusLastEditedBy", req.getRemoteUser());
        feature.put("lastEditOn", new Date());

        // add edit update
        BasicDBList edits = (BasicDBList) feature.get("edits");
        if (edits == null) {
            edits = new BasicDBList();
        }
        final BasicDBList temp = new BasicDBList();
        temp.add(new BasicDBObject().append("id", "embeddings").append("added", gridFile.getFilename())
                .append("removed", null));

        final BasicDBList masks = new BasicDBList();
        masks.add(new BasicDBObject().append("scenario", scenarioName).append("changes", temp));

        final BasicDBObject edit = new BasicDBObject().append("name", feature.get("statusLastEditedBy"))
                .append("date", feature.get("lastEditOn")).append("prev", feature.get("calculatedStatus"))
                .append("curr", feature.get("calculatedStatus")).append("stepChanges", masks);

        final BasicDBList newEdits = new BasicDBList();
        newEdits.add(edit);
        newEdits.addAll(edits);
        feature.put("edits", newEdits);

        collection.save(feature);
        return gridFile.getFilename();
    }
}