Search in sources :

Example 31 with BasicBSONObject

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);
}
Also used : ObjectId(org.bson.types.ObjectId) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) BasicBSONObject(org.bson.BasicBSONObject) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) BasicDBObject(com.mongodb.BasicDBObject) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) FileUploadException(org.apache.commons.fileupload.FileUploadException) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 32 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 33 with BasicBSONObject

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;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject)

Example 34 with BasicBSONObject

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());
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) ObjectId(org.bson.types.ObjectId) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Date(java.util.Date)

Example 35 with BasicBSONObject

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();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Mongo(com.mongodb.Mongo) ContentStorage(v7db.files.spi.ContentStorage) Content(v7db.files.spi.Content)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4