use of org.bson.BasicBSONObject in project v7files by thiloplanz.
the class BucketsServlet method doFormPost.
private void doFormPost(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException {
ObjectId uploadId = new ObjectId();
BSONObject parameters = new BasicBSONObject();
List<FileItem> files = new ArrayList<FileItem>();
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
for (Object _file : upload.parseRequest(request)) {
FileItem file = (FileItem) _file;
if (file.isFormField()) {
String v = file.getString();
parameters.put(file.getFieldName(), v);
} else {
files.add(file);
}
}
} catch (FileUploadException e) {
throw new IOException(e);
}
} else {
for (Entry<String, String[]> param : request.getParameterMap().entrySet()) {
String[] v = param.getValue();
if (v.length == 1)
parameters.put(param.getKey(), v[0]);
else
parameters.put(param.getKey(), v);
}
}
BSONObject result = new BasicBSONObject("_id", uploadId);
BSONObject uploads = new BasicBSONObject();
for (FileItem file : files) {
DiskFileItem f = (DiskFileItem) file;
// inline until 10KB
if (f.isInMemory()) {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.get(), uploadId, f.getName(), f.getContentType()));
} else {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.getStoreLocation(), uploadId, f.getName(), f.getContentType()));
}
file.delete();
}
result.put("files", uploads);
result.put("parameters", parameters);
bucketCollection.update(new BasicDBObject("_id", bucket.get("_id")), new BasicDBObject("$push", new BasicDBObject("FormPost.data", result)));
String redirect = BSONUtils.getString(bucket, "FormPost.redirect");
// redirect mode
if (StringUtils.isNotBlank(redirect)) {
response.sendRedirect(redirect + "?v7_formpost_id=" + uploadId);
return;
}
// echo mode
// JSON does not work, see https://jira.mongodb.org/browse/JAVA-332
// response.setContentType("application/json");
// response.getWriter().write(JSON.serialize(result));
byte[] bson = BSON.encode(result);
response.getOutputStream().write(bson);
}
use of org.bson.BasicBSONObject in project v7files by thiloplanz.
the class BucketsServlet method doEchoPutGet.
private void doEchoPutGet(HttpServletRequest request, HttpServletResponse response, BSONObject bucket, byte[] sha) throws IOException {
Content content = storage.getContent(sha);
if (content == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Bucket '" + bucket.get("_id") + "' does not have a file matching digest '" + Hex.encodeHexString(sha) + "'");
return;
}
BSONObject file = new BasicBSONObject("sha", sha);
String customFilename = request.getParameter("filename");
if (StringUtils.isNotBlank(customFilename))
file.put("filename", customFilename);
sendFile(request, response, sha, file, content);
}
use of org.bson.BasicBSONObject in project v7files by thiloplanz.
the class BucketsServletTest method prepareBucket.
private BasicBSONObject prepareBucket(String bucketId, String GET, String POST, String PUT) {
BasicBSONObject o = new BasicBSONObject("_id", bucketId).append("GET", GET).append("POST", POST).append("PUT", PUT);
prepareMockData("test.v7files.buckets", o);
return o;
}
use of org.bson.BasicBSONObject in project v7files by thiloplanz.
the class MiltonServletTest method testGET.
public void testGET() throws IOException, SAXException {
ServletUnitClient sc = sr.newClient();
sc.setAuthentication("V7Files", "admin", "admin");
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/a.txt");
try {
sc.getResponse(request);
fail("file not found => 404");
} catch (HttpNotFoundException e) {
}
}
prepareMockData("test.v7files.files", new BasicBSONObject("_id", new ObjectId()).append("filename", "a.txt").append("parent", "webdav").append("created_at", new Date()).append("in", "abcd".getBytes()));
assertGET(sc, "http://test/myServlet/a.txt", "abcd");
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/a.txt");
WebResponse resp = sc.getResponse(request);
String eTag = resp.getHeaderField("Etag");
request.setHeaderField("If-None-Match", eTag);
resp = sc.getResponse(request);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, resp.getResponseCode());
assertEquals("", resp.getText());
}
}
use of org.bson.BasicBSONObject in project v7files by thiloplanz.
the class MongoContentStorageTest method testReadChunkedData.
public void testReadChunkedData() throws MongoException, IOException {
byte[] data1 = "first chunk".getBytes();
byte[] sha1 = DigestUtils.sha(data1);
byte[] data2 = " second chunk".getBytes();
byte[] sha2 = DigestUtils.sha(data2);
byte[] data = "first chunk second chunk".getBytes();
byte[] sha = DigestUtils.sha(data);
prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha1).append("in", data1));
prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha2).append("in", data2));
prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha).append("store", "cat").append("base", Arrays.asList(new BasicBSONObject("sha", sha1).append("length", data1.length), new BasicBSONObject("sha", sha2).append("length", data2.length))));
Mongo mongo = getMongo();
ContentStorage storage = new MongoContentStorage(mongo.getDB("test").getCollection("v7files.content"));
Content check = storage.getContent(sha);
assertEquals(new String(data), IOUtils.toString(check.getInputStream()));
assertEquals(data.length, check.getLength());
mongo.close();
}
Aggregations