Search in sources :

Example 1 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project camel by apache.

the class GridFsConsumer method run.

@Override
public void run() {
    DBCursor c = null;
    java.util.Date fromDate = null;
    QueryStrategy s = endpoint.getQueryStrategy();
    boolean usesTimestamp = s != QueryStrategy.FileAttribute;
    boolean persistsTimestamp = s == QueryStrategy.PersistentTimestamp || s == QueryStrategy.PersistentTimestampAndFileAttribute;
    boolean usesAttribute = s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute || s == QueryStrategy.PersistentTimestampAndFileAttribute;
    DBCollection ptsCollection = null;
    DBObject persistentTimestamp = null;
    if (persistsTimestamp) {
        ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
        // ensure standard indexes as long as collections are small
        try {
            if (ptsCollection.count() < 1000) {
                ptsCollection.createIndex(new BasicDBObject("id", 1));
            }
        } catch (MongoException e) {
        //TODO: Logging
        }
        persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
        if (persistentTimestamp == null) {
            persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
            fromDate = new java.util.Date();
            persistentTimestamp.put("timestamp", fromDate);
            ptsCollection.save(persistentTimestamp);
        }
        fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
    } else if (usesTimestamp) {
        fromDate = new java.util.Date();
    }
    try {
        Thread.sleep(endpoint.getInitialDelay());
        while (isStarted()) {
            if (c == null || c.getCursorId() == 0) {
                if (c != null) {
                    c.close();
                }
                String queryString = endpoint.getQuery();
                DBObject query;
                if (queryString == null) {
                    query = new BasicDBObject();
                } else {
                    query = (DBObject) JSON.parse(queryString);
                }
                if (usesTimestamp) {
                    query.put("uploadDate", new BasicDBObject("$gt", fromDate));
                }
                if (usesAttribute) {
                    query.put(endpoint.getFileAttributeName(), null);
                }
                c = endpoint.getFilesCollection().find(query);
            }
            boolean dateModified = false;
            while (c.hasNext() && isStarted()) {
                GridFSDBFile file = (GridFSDBFile) c.next();
                GridFSDBFile forig = file;
                if (usesAttribute) {
                    file.put(endpoint.getFileAttributeName(), "processing");
                    DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null).get();
                    forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false, file, true, false);
                }
                if (forig != null) {
                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));
                    Exchange exchange = endpoint.createExchange();
                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
                    try {
                        getProcessor().process(exchange);
                        //System.out.println("Processing " + file.getFilename());
                        if (usesAttribute) {
                            forig.put(endpoint.getFileAttributeName(), "done");
                            endpoint.getFilesCollection().save(forig);
                        }
                        if (usesTimestamp) {
                            if (file.getUploadDate().compareTo(fromDate) > 0) {
                                fromDate = file.getUploadDate();
                                dateModified = true;
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (persistsTimestamp && dateModified) {
                persistentTimestamp.put("timestamp", fromDate);
                ptsCollection.save(persistentTimestamp);
            }
            Thread.sleep(endpoint.getDelay());
        }
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (c != null) {
        c.close();
    }
}
Also used : MongoException(com.mongodb.MongoException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) MongoException(com.mongodb.MongoException) Exchange(org.apache.camel.Exchange) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile)

Example 2 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project nanopub-server by tkuhn.

the class NanopubDb method writePackageToStream.

public void writePackageToStream(long pageNo, boolean gzipped, OutputStream out) throws IOException {
    if (pageNo < 1 || pageNo >= journal.getCurrentPageNo()) {
        throw new IllegalArgumentException("Not a complete page: " + pageNo);
    }
    GridFSDBFile f = packageGridFs.findOne(pageNo + "");
    OutputStream packageOut = null;
    InputStream packageAsStream = null;
    try {
        if (f == null) {
            if (gzipped) {
                out = new GZIPOutputStream(out);
            }
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            packageOut = new GZIPOutputStream(bOut);
            String pageContent = journal.getPageContent(pageNo);
            for (String uri : pageContent.split("\\n")) {
                Nanopub np = getNanopub(TrustyUriUtils.getArtifactCode(uri));
                String s;
                try {
                    s = NanopubUtils.writeToString(np, RDFFormat.TRIG);
                } catch (RDFHandlerException ex) {
                    throw new RuntimeException("Unexpected RDF handler exception", ex);
                }
                byte[] bytes = (s + "\n").getBytes();
                out.write(bytes);
                packageOut.write(bytes);
            }
            packageOut.close();
            packageAsStream = new ByteArrayInputStream(bOut.toByteArray());
            GridFSInputFile i = packageGridFs.createFile(packageAsStream);
            i.setFilename(pageNo + "");
            i.save();
        } else {
            if (gzipped) {
                f.writeTo(out);
            } else {
                GZIPInputStream in = new GZIPInputStream(f.getInputStream());
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                in.close();
            }
        }
    } finally {
        if (out != null)
            out.close();
        if (packageOut != null)
            packageOut.close();
        if (packageAsStream != null)
            packageAsStream.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Nanopub(org.nanopub.Nanopub) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) RDFHandlerException(org.openrdf.rio.RDFHandlerException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile)

Example 3 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project tutorials by eugenp.

the class GridFSLiveTest method givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound.

@Test
public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound() {
    DBObject metaDataUser1 = new BasicDBObject();
    metaDataUser1.put("user", "alex");
    DBObject metaDataUser2 = new BasicDBObject();
    metaDataUser2.put("user", "david");
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream("src/main/resources/test.png");
        gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser1);
        gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser2);
    } catch (FileNotFoundException ex) {
        logger.error("File not found", ex);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                logger.error("Failed to close", ex);
            }
        }
    }
    List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null);
    assertNotNull(gridFSDBFiles);
    assertThat(gridFSDBFiles.size(), is(2));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project tutorials by eugenp.

the class GridFSLiveTest method givenMetadataAndFilesExist_whenFindingAllFilesOnQuery_thenFilesWithMetadataAreFoundOnQuery.

@Test
public void givenMetadataAndFilesExist_whenFindingAllFilesOnQuery_thenFilesWithMetadataAreFoundOnQuery() {
    DBObject metaDataUser1 = new BasicDBObject();
    metaDataUser1.put("user", "alex");
    DBObject metaDataUser2 = new BasicDBObject();
    metaDataUser2.put("user", "david");
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream("src/main/resources/test.png");
        gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser1);
        gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser2);
    } catch (FileNotFoundException ex) {
        logger.error("File not found", ex);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                logger.error("Failed to close", ex);
            }
        }
    }
    List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex")));
    assertNotNull(gridFSDBFiles);
    assertThat(gridFSDBFiles.size(), is(1));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Query(org.springframework.data.mongodb.core.query.Query) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project teiid by teiid.

the class MongoDBExecutionFactory method retrieveValue.

/**
 * @param field
 * @param expectedClass
 * @return
 * @throws TranslatorException
 */
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (value.getClass().equals(expectedClass)) {
        return value;
    }
    if (value instanceof DBRef) {
        Object obj = ((DBRef) value).getId();
        if (obj instanceof BasicDBObject) {
            BasicDBObject bdb = (BasicDBObject) obj;
            return bdb.get(colName);
        }
        return obj;
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
        return new java.sql.Date(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
        return new java.sql.Time(((java.util.Date) value).getTime());
    } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
        return new BigDecimal((String) value);
    } else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
        return new BigInteger((String) value);
    } else if (value instanceof String && expectedClass.equals(Character.class)) {
        return new Character(((String) value).charAt(0));
    } else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
        return new BinaryType(((String) value).getBytes());
    } else if (value instanceof String && expectedClass.equals(Blob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new BlobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof String && expectedClass.equals(Clob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new ClobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        }, -1);
    } else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof BasicDBList) {
        BasicDBList arrayValues = (BasicDBList) value;
        // array
        if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
            Class arrayType = expectedClass.getComponentType();
            Object array = Array.newInstance(arrayType, arrayValues.size());
            for (int i = 0; i < arrayValues.size(); i++) {
                Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
                Array.set(array, i, arrayItem);
            }
            value = array;
        }
    } else if (value instanceof org.bson.types.ObjectId) {
        org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
        value = id.toHexString();
    } else {
        Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
        if (transform != null) {
            try {
                value = transform.transform(value, expectedClass);
            } catch (TransformationException e) {
                throw new TranslatorException(e);
            }
        }
    }
    return value;
}
Also used : InputStreamFactory(org.teiid.core.types.InputStreamFactory) BasicDBObject(com.mongodb.BasicDBObject) BlobImpl(org.teiid.core.types.BlobImpl) ClobImpl(org.teiid.core.types.ClobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) InputStream(java.io.InputStream) DBRef(com.mongodb.DBRef) IOException(java.io.IOException) GridFS(com.mongodb.gridfs.GridFS) BigDecimal(java.math.BigDecimal) BasicDBList(com.mongodb.BasicDBList) SQLXML(java.sql.SQLXML) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) Transform(org.teiid.core.types.Transform)

Aggregations

GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)20 BasicDBObject (com.mongodb.BasicDBObject)8 InputStream (java.io.InputStream)8 DBObject (com.mongodb.DBObject)6 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 Test (org.junit.Test)4 GridFS (com.mongodb.gridfs.GridFS)3 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)3 FileInputStream (java.io.FileInputStream)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LogDetail (com.bc.pmpheep.annotation.LogDetail)2 ResponseBean (com.bc.pmpheep.controller.bean.ResponseBean)2 Content (com.bc.pmpheep.general.po.Content)2 DBCollection (com.mongodb.DBCollection)2 DBCursor (com.mongodb.DBCursor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2