Example usage for com.mongodb.gridfs GridFS findOne

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

Introduction

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

Prototype

public GridFSDBFile findOne(final DBObject query) 

Source Link

Document

Finds one file matching the given query.

Usage

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];//w  ww. j a  va  2 s  .c o m
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String op = req.getParameter("op");
    if (op == null)
        op = "get";

    StringBuilder buf = tl.get();
    // reset buf
    buf.setLength(0);

    // list
    if ("get".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("file does not exists"));
            return;
        }

        res.setContentLength((int) db_file.getLength());
        String ct = db_file.getContentType();
        if (ct != null)
            res.setContentType(ct);
        OutputStream os = res.getOutputStream();
        long l;
        while ((l = db_file.writeTo(os)) > 0)
            ;
        os.flush();
        os.close();

    }
    // list
    else if ("list".equals(op)) {

        DBCursor c = fs.getFileList();
        if (c == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        int no = 0;
        buf.append("[");
        while (c.hasNext()) {

            DBObject o = c.next();
            JSON.serialize(o, buf);
            buf.append(",");
            no++;

        }

        if (no > 0)
            buf.setCharAt(buf.length() - 1, ']');
        else
            buf.append(']');

        out_str(req, buf.toString(), "application/json");

    }
    // info
    else if ("info".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        buf.append("{");
        buf.append(String.format("\"ContentType\":%s,", db_file.getContentType()));
        buf.append(String.format("\"Length\":%d,", db_file.getLength()));
        buf.append(String.format("\"MD5\":%s", db_file.getMD5()));
        buf.append("}");

        out_str(req, buf.toString(), "application/json");

    } else
        res.sendError(SC_BAD_REQUEST);

}

From source file:org.apache.manifoldcf.crawler.connectors.gridfs.GridFSRepositoryConnector.java

License:Apache License

/** Process a set of documents.
* This is the method that should cause each document to be fetched, processed, and the results either added
* to the queue of documents for the current job, and/or entered into the incremental ingestion manager.
* The document specification allows this class to filter what is done based on the job.
* The connector will be connected before this method can be called.
*@param documentIdentifiers is the set of document identifiers to process.
*@param statuses are the currently-stored document versions for each document in the set of document identifiers
* passed in above./*from  www. j  av  a  2s  .  c  o m*/
*@param activities is the interface this method should use to queue up new document references
* and ingest documents.
*@param jobMode is an integer describing how the job is being run, whether continuous or once-only.
*@param usesDefaultAuthority will be true only if the authority in use for these documents is the default one.
*/
@Override
public void processDocuments(String[] documentIdentifiers, IExistingVersions statuses, Specification spec,
        IProcessActivity activities, int jobMode, boolean usesDefaultAuthority)
        throws ManifoldCFException, ServiceInterruption {

    for (String documentIdentifier : documentIdentifiers) {

        String versionString;
        GridFS gfs;
        GridFSDBFile document;

        getSession();
        String _id = documentIdentifier;
        gfs = new GridFS(session, bucket);
        document = gfs.findOne(new ObjectId(_id));
        if (document == null) {
            activities.deleteDocument(documentIdentifier);
            continue;
        } else {
            DBObject metadata = document.getMetaData();
            versionString = document.getMD5() + "+" + metadata != null ? Integer.toString(metadata.hashCode())
                    : StringUtils.EMPTY;
        }

        if (versionString.length() == 0
                || activities.checkDocumentNeedsReindexing(documentIdentifier, versionString)) {
            long startTime = System.currentTimeMillis();
            String errorCode = null;
            String errorDesc = null;
            String version = versionString;
            try {

                if (Logging.connectors.isDebugEnabled()) {
                    Logging.connectors.debug("GridFS: Processing document _id = " + _id);
                }

                DBObject metadata = document.getMetaData();
                if (metadata == null) {
                    errorCode = "NULLMETADATA";
                    errorDesc = "Excluded because document had a null Metadata";
                    Logging.connectors.warn("GridFS: Document " + _id + " has a null metadata - skipping.");
                    activities.noDocument(_id, version);
                    continue;
                }

                String urlValue = document.getMetaData().get(this.url) == null ? StringUtils.EMPTY
                        : document.getMetaData().get(this.url).toString();
                if (!StringUtils.isEmpty(urlValue)) {
                    boolean validURL;
                    try {
                        new java.net.URI(urlValue);
                        validURL = true;
                    } catch (java.net.URISyntaxException e) {
                        validURL = false;
                    }
                    if (validURL) {
                        long fileLenght = document.getLength();
                        Date createdDate = document.getUploadDate();
                        String fileName = document.getFilename();
                        String mimeType = document.getContentType();

                        if (!activities.checkURLIndexable(urlValue)) {
                            Logging.connectors.warn(
                                    "GridFS: Document " + _id + " has a URL excluded by the output connector ('"
                                            + urlValue + "') - skipping.");
                            errorCode = activities.EXCLUDED_URL;
                            errorDesc = "Excluded because of URL (" + urlValue + ")";
                            activities.noDocument(_id, version);
                            continue;
                        }

                        if (!activities.checkLengthIndexable(fileLenght)) {
                            Logging.connectors.warn("GridFS: Document " + _id
                                    + " has a length excluded by the output connector (" + fileLenght
                                    + ") - skipping.");
                            errorCode = activities.EXCLUDED_LENGTH;
                            errorDesc = "Excluded because of length (" + fileLenght + ")";
                            activities.noDocument(_id, version);
                            continue;
                        }

                        if (!activities.checkMimeTypeIndexable(mimeType)) {
                            Logging.connectors.warn("GridFS: Document " + _id
                                    + " has a mime type excluded by the output connector ('" + mimeType
                                    + "') - skipping.");
                            errorCode = activities.EXCLUDED_MIMETYPE;
                            errorDesc = "Excluded because of mime type (" + mimeType + ")";
                            activities.noDocument(_id, version);
                            continue;
                        }

                        if (!activities.checkDateIndexable(createdDate)) {
                            Logging.connectors.warn(
                                    "GridFS: Document " + _id + " has a date excluded by the output connector ("
                                            + createdDate + ") - skipping.");
                            errorCode = activities.EXCLUDED_DATE;
                            errorDesc = "Excluded because of date (" + createdDate + ")";
                            activities.noDocument(_id, version);
                            continue;
                        }

                        RepositoryDocument rd = new RepositoryDocument();
                        rd.setCreatedDate(createdDate);
                        rd.setModifiedDate(createdDate);
                        rd.setFileName(fileName);
                        rd.setMimeType(mimeType);
                        String[] aclsArray = null;
                        String[] denyAclsArray = null;
                        if (acl != null) {
                            try {
                                Object aclObject = document.getMetaData().get(acl);
                                if (aclObject != null) {
                                    List<String> acls = (List<String>) aclObject;
                                    aclsArray = (String[]) acls.toArray();
                                }
                            } catch (ClassCastException e) {
                                // This is bad because security will fail
                                Logging.connectors.warn("GridFS: Document " + _id
                                        + " metadata ACL field doesn't contain List<String> type.");
                                errorCode = "ACLTYPE";
                                errorDesc = "Allow ACL field doesn't contain List<String> type.";
                                throw new ManifoldCFException("Security decoding error: " + e.getMessage(), e);
                            }
                        }
                        if (denyAcl != null) {
                            try {
                                Object denyAclObject = document.getMetaData().get(denyAcl);
                                if (denyAclObject != null) {
                                    List<String> denyAcls = (List<String>) denyAclObject;
                                    denyAcls.add(GLOBAL_DENY_TOKEN);
                                    denyAclsArray = (String[]) denyAcls.toArray();
                                }
                            } catch (ClassCastException e) {
                                // This is bad because security will fail
                                Logging.connectors.warn("GridFS: Document " + _id
                                        + " metadata DenyACL field doesn't contain List<String> type.");
                                errorCode = "ACLTYPE";
                                errorDesc = "Deny ACL field doesn't contain List<String> type.";
                                throw new ManifoldCFException("Security decoding error: " + e.getMessage(), e);
                            }
                        }
                        rd.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT, aclsArray, denyAclsArray);

                        InputStream is = document.getInputStream();
                        try {
                            rd.setBinary(is, fileLenght);
                            try {
                                activities.ingestDocumentWithException(_id, version, urlValue, rd);
                            } catch (IOException e) {
                                handleIOException(e);
                            }
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                handleIOException(e);
                            }
                        }
                        gfs.getDB().getMongo().getConnector().close();
                        session = null;
                        errorCode = "OK";
                    } else {
                        Logging.connectors.warn(
                                "GridFS: Document " + _id + " has a invalid URL: " + urlValue + " - skipping.");
                        errorCode = activities.BAD_URL;
                        errorDesc = "Excluded because document had illegal URL ('" + urlValue + "')";
                        activities.noDocument(_id, version);
                    }
                } else {
                    Logging.connectors.warn("GridFS: Document " + _id + " has a null URL - skipping.");
                    errorCode = activities.NULL_URL;
                    errorDesc = "Excluded because document had a null URL.";
                    activities.noDocument(_id, version);
                }
            } finally {
                if (errorCode != null) {
                    activities.recordActivity(startTime, ACTIVITY_FETCH, document.getLength(), _id, errorCode,
                            errorDesc, null);
                }
            }
        }
    }
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Saves a file to the database by file name. This is used during a form upload. We use tika to
 * determine the content type./*from   ww w  .j  av  a 2 s  .co  m*/
 *
 * TODO: Refactor this mess
 *
 * @param databaseName the name of the database
 * @param bucketName the name of the bucket
 * @param fileName the name of the file
 * @param overwrite whether to overwrite an existing file with the same name
 * @param stream the file byte stream
 * @return the Mongo ID of the file
 * @throws DatasourceException
 * @throws ExistsException
 * @throws NotFoundException
 */
@Override
public String addByForm(String databaseName, String bucketName, String fileName, boolean overwrite,
        InputStream stream) throws DatasourceException, ExistsException, NotFoundException {
    String fileId = null;
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        GridFSDBFile gfsFile = gfsBucket.findOne(fileName);
        if (gfsFile == null) {
            // the file does not exist -- create
            GridFSInputFile dbFile = gfsBucket.createFile(stream, fileName);
            dbFile.setContentType(tika.detect(fileName));
            dbFile.save();
            fileId = dbFile.getId().toString();
        } else {
            // the file exists
            if (overwrite) {
                // overwrite the existing file
                gfsBucket.remove(gfsFile);
                GridFSInputFile inputFile = gfsBucket.createFile(stream, fileName);
                inputFile.setContentType(tika.detect(fileName));
                inputFile.save();
                fileId = inputFile.getId().toString();
            } else {
                throw new ExistsException("The file already exists in the bucket");
            }
        }
    } catch (MongoException ex) {
        logger.error("Could not persist entity to bucket", ex);
        throw new DatasourceException("Could not persist file to bucket");
    }
    if (fileId == null || fileId.isEmpty()) {
        throw new DatasourceException("Could not persist file to bucket");
    }
    return fileId;
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Saves a document to the database by file name. If the document already exists this request
 * will be dropped and the existing file will not be overwritten.
 *
 * @param databaseName the database/*ww w  .  j  a v a2 s  .  co m*/
 * @param bucketName the bucket
 * @param fileName the file name
 * @param inputStream the binary payload
 * @return the identifier of the file
 * @throws DatasourceException
 * @throws ExistsException
 * @throws NotFoundException
 */
@Override
public String addByFileName(String databaseName, String bucketName, String fileName, InputStream inputStream)
        throws DatasourceException, ExistsException, NotFoundException {
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        if (gfsBucket.findOne(fileName) != null) {
            throw new ExistsException("The file already exists");
        }
        GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName);
        inputFile.setContentType(tika.detect(fileName));
        inputFile.save();
        return inputFile.getId().toString();
    } catch (MongoException ex) {
        logger.error("An error occured while adding the file", ex);
        throw new DatasourceException("An error occured while adding the file");
    }
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Updates a file in the database. If the file exists in the database it will be updated. If the
 * file doesn't exist it will be created.
 *
 * @param databaseName the database/*from   w  ww.j ava  2 s. c  om*/
 * @param bucketName the bucket
 * @param fileName the file name
 * @param inputStream the binary payload
 * @return the identifier of the file
 * @throws DatasourceException
 * @throws NotFoundException
 */
@Override
public String updateByFileName(String databaseName, String bucketName, String fileName, InputStream inputStream)
        throws DatasourceException, NotFoundException {
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        GridFSDBFile gfsFile = gfsBucket.findOne(fileName);
        if (gfsFile == null) {
            GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName);
            inputFile.setContentType(tika.detect(fileName));
            inputFile.save();
            return inputFile.getId().toString();
        } else {
            gfsBucket.remove(gfsFile);
            GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName);
            inputFile.setContentType(tika.detect(fileName));
            inputFile.save();
            return inputFile.getId().toString();
        }
    } catch (MongoException ex) {
        logger.error("An error occured while updating the file", ex);
        throw new DatasourceException("An error occured while updating the file");
    }
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Returns the file with the given file name.
 *
 * @param databaseName the database//from  w  w  w  .ja v a 2s . c  o  m
 * @param bucketName the bucket
 * @param fileName the file name
 * @return the file in with the given file name
 * @throws DatasourceException
 * @throws IOException
 * @throws NotFoundException
 */
@Override
public FileEnvelope getByFileName(String databaseName, String bucketName, String fileName)
        throws DatasourceException, IOException, NotFoundException {
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        GridFSDBFile gfsFile = gfsBucket.findOne(fileName);
        if (gfsFile == null) {
            throw new NotFoundException("The file doesnt exist");
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        gfsFile.writeTo(baos);

        return new FileEnvelope(baos.toByteArray(), gfsFile.getContentType(), fileName);
    } catch (MongoException ex) {
        logger.error("An error occured while retrieving the file", ex);
        throw new DatasourceException("An error occured while retrieving the file");
    }
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Removes a file in the database./*w  w w.jav  a 2  s.  co m*/
 *
 * @param databaseName the database
 * @param bucketName the bucket
 * @param fileName the file to delete
 * @return a status message with the outcome of the operation
 * @throws DatasourceException
 * @throws NotFoundException
 */
@Override
public boolean removeByFileName(String databaseName, String bucketName, String fileName)
        throws DatasourceException, NotFoundException {
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        GridFSDBFile gfsFile = gfsBucket.findOne(fileName);
        if (gfsFile == null) {
            throw new NotFoundException("The file doesnt exist");
        }
        gfsBucket.remove(gfsFile);
        return true;
    } catch (MongoException ex) {
        logger.error("An error occured while removing the file", ex);
        throw new DatasourceException("An error occured while removing the file");
    }
}

From source file:org.exist.mongodb.xquery.gridfs.Get.java

License:Open Source License

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

    try {/*from  www . j  a va  2 s  . co 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 bucket = args[2].itemAt(0).getStringValue();
        String documentId = args[3].itemAt(0).getStringValue();
        boolean forceBinary = args[4].itemAt(0).toJavaObject(Boolean.class);

        // Get database
        DB db = client.getDB(dbname);

        // Creates a GridFS instance for the specified bucket
        GridFS gfs = new GridFS(db, bucket);

        // Find one document by id or by filename
        GridFSDBFile gfsFile = (isCalledAs(FIND_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId))
                : gfs.findOne(documentId); // TODO: find latest

        if (gfsFile == null) {
            throw new XPathException(this, GridfsModule.GRFS0004,
                    String.format("Document '%s' could not be found.", documentId));
        }

        Sequence retVal = get(gfsFile, forceBinary);

        return retVal;

    } 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, GridfsModule.GRFS0002, ex.getMessage());

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

}

From source file:org.exist.mongodb.xquery.gridfs.Properties.java

License:Open Source License

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

    try {/*from w  w w .jav  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 bucket = args[2].itemAt(0).getStringValue();
        String documentId = args[3].itemAt(0).getStringValue();

        // Get database
        DB db = client.getDB(dbname);

        // Creates a GridFS instance for the specified bucket
        GridFS gfs = new GridFS(db, bucket);

        // Find one document by id or by filename
        GridFSDBFile gfsFile = (isCalledAs(PROPS_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId))
                : gfs.findOne(documentId);

        if (gfsFile == null) {
            throw new XPathException(this, GridfsModule.GRFS0004,
                    String.format("Document '%s' could not be found.", documentId));
        }

        return ContentSerializer.getReport(gfsFile);

    } 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, GridfsModule.GRFS0002, ex.getMessage());

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

}

From source file:org.exist.mongodb.xquery.gridfs.Stream.java

License:Open Source License

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

    try {/*w  w  w. j  a va2  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 bucket = args[2].itemAt(0).getStringValue();
        String documentId = args[3].itemAt(0).getStringValue();
        Boolean setDisposition = args[4].itemAt(0).toJavaObject(Boolean.class);

        // Get database
        DB db = client.getDB(dbname);

        // Creates a GridFS instance for the specified bucket
        GridFS gfs = new GridFS(db, bucket);

        // Find one document by id or by filename
        GridFSDBFile gfsFile = (isCalledAs(FIND_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId))
                : gfs.findOne(documentId);

        stream(gfsFile, documentId, setDisposition);

    } 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, GridfsModule.GRFS0002, ex.getMessage());

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

    return Sequence.EMPTY_SEQUENCE;

}