Example usage for com.mongodb.gridfs GridFS getDB

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

Introduction

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

Prototype

public DB getDB() 

Source Link

Document

Gets the database used.

Usage

From source file:com.ikanow.utility.GridFSRandomAccessFile.java

License:Open Source License

/** Returns a random access file accessor from a GridFS and fileId
 * @param gridFS - com.mongodb.gridfs.GridFS - the MongoDB gridFS "collection"
 * @param fileId - org.bson.ObjectId, the _id of the file
 * @throws IOException/*  ww  w .  j a  v  a2  s  . com*/
 */
public GridFSRandomAccessFile(GridFS gridFS, ObjectId fileId) throws IOException {
    this(gridFS.getDB(), gridFS.getBucketName(), fileId);
}

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 ww  w. j  ava2  s.com*/
*@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:rapture.blob.mongodb.GridFSBlobHandler.java

License:Open Source License

private String createLockKey(GridFS gridFS, String docPath) {
    return gridFS.getDB().getName() + "." + gridFS.getBucketName() + "." + docPath;
}