Initial commit

This commit is contained in:
Mark Vainomaa 2017-01-24 15:12:49 +02:00
commit 7f2b6e2530
3 changed files with 116 additions and 0 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# ---> Java
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IntelliJ IDEA
*.iml
.idea/
# Maven
target/
dependency-reduced-pom.xml

33
pom.xml Normal file
View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.mikroskeem</groupId>
<artifactId>mongohackery</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,62 @@
package eu.mikroskeem.mongohackery;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import static com.mongodb.client.model.Filters.eq;
public class Main {
public static void main(String... args){
new Main();
}
public Main(){
MongoClient client = new MongoClient();
MongoDatabase leTest = client.getDatabase("leTest");
GridFSBucket gridfs = GridFSBuckets.create(leTest, "files");
/*
* Ransom data
* Should use file here actually
*/
InputStream is = new ByteArrayInputStream("topkek".getBytes());
/* Remove (if present) */
try {
gridfs.delete(gridfs.find(eq("filename", "topkek")).first().getId());
}
catch(NullPointerException ignored){}
/* Insert */
GridFSUploadOptions options = new GridFSUploadOptions()
.chunkSizeBytes(1024)
.metadata(new Document("contentType", "text/plain"));
/*
* NOTE: 'contentType' isn't standard! It is here, because I want to see
* It is there, because I tend to use it with GridFS
*/
ObjectId id = gridfs.uploadFromStream("topkek", is, options);
System.out.println(id);
/* Now read */
InputStream os = gridfs.openDownloadStream(gridfs.find(eq("filename", "topkek")).first().getId());
String res = new BufferedReader(new InputStreamReader(os))
.lines().collect(Collectors.joining("\n"));
System.out.println(res);
/* Remove */
gridfs.delete(gridfs.find(eq("filename", "topkek")).first().getId());
}
}