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();
}
}
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();
}
}
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));
}
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));
}
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;
}
Aggregations