无法从MongoDB

时间:2017-07-17 10:35:04

标签: java mongodb jcr jackrabbit

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException;

import org.apache.jackrabbit.JcrConstants; import org.apache.jackrabbit.oak.Oak; import org.apache.jackrabbit.oak.jcr.Jcr; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;

import com.mongodb.DB; import com.mongodb.MongoClient;

import javax.jcr.Binary; import javax.jcr.LoginException; import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.version.Version; import javax.jcr.version.VersionHistory; import javax.jcr.version.VersionIterator; import javax.jcr.version.VersionManager;

public class Versioning {

public static void main(String[] args) throws LoginException,
        RepositoryException, FileNotFoundException {
    // TODO Auto-generated method stub

    DB db = new MongoClient("127.0.0.1", 27017).getDB("mydb");
    DocumentNodeStore ns = new DocumentMK.Builder().setMongoDB(db)
            .getNodeStore();
    System.out.println("start");
    Repository repo = new Jcr(new Oak(ns)).with(new OpenSecurityProvider())
            .createRepository();
    Session session = repo.login();

    VersionManager vm = session.getWorkspace().getVersionManager();
    Node parentNode = session.getRootNode();
    String multiPartFileName = "pdf";
    FileInputStream stream;
    Binary binary;
    Node node;
    Node content;
    Node fileNode;
    if (parentNode.hasNode("pdfFileNode")) {
        node = parentNode.getNode("pdfFileNode");
    } else {
        node = parentNode.addNode("pdfFileNode", "nt:unstructured");
    }

    if (node.hasNode(multiPartFileName)) {
        fileNode = node.getNode(multiPartFileName);
        System.out.println("filenode available");
    } else {
        fileNode = node.addNode(multiPartFileName, "nt:file");
        System.out.println("creating the filenode");
    }
    stream = new FileInputStream(new File("D:/pdf-sample.pdf"));

    if (fileNode.hasNode("jcr:content")) {
        content = fileNode.getNode("jcr:content");
    } else {
        content = fileNode.addNode("jcr:content", "nt:resource");
    }
    System.out.println("crossed");
    binary = session.getValueFactory().createBinary(stream);

    node.addMixin(JcrConstants.MIX_VERSIONABLE);
    content.setProperty("jcr:data", binary);
    session.save();

    System.out.println("node.getPath() :" + node.getPath());

    Version firstVersion = vm.checkin(node.getPath());

    System.out.println("firstVersion :" + firstVersion);
    Node child = parentNode.getNode("pdfFileNode");

    System.out.println("child.getPath() :" + child.getPath());

    vm.checkout(child.getPath());

    System.out.println("checkout child.getPath() :" + child.getPath());

    stream = new FileInputStream(new File("D:/pdf-sample.pdf"));

    //content = fileNode.addNode("jcr:content", "nt:resource");
    binary = session.getValueFactory().createBinary(stream);

    content.setProperty("jcr:data", binary);
    session.save();
    vm.checkin(child.getPath());

    System.out.println("checkin child.getPath() :" + child.getPath());

    VersionHistory history = vm.getVersionHistory(child.getPath());
    for (VersionIterator it = history.getAllVersions(); it.hasNext();) {
        Version version = (Version) it.next();
        System.out.println("Version: " + version.getName() + " created on "
                + version.getCreated().getTime());
    }

    vm.checkout(child.getPath());
    System.out.println("restoring checkout child.getPath() :"
            + child.getPath());
    session.logout();
    ns.dispose();

    // vm.restore(firstVersion, true);
}
}

通过上面的代码,我正在对文件进行更改并反复保存该文件,以便按如下方式创建其版本。

  

版本:1.0创建于2017年7月17日星期一15:00:00 IST 2017

     

版本:1.1于2017年7月17日星期一15:00:00创建

     

版本:1.2创建于2017年7月17日星期一15:07:12 IST 2017

     

...

我不确定我的代码是否实际保存了mongo DB中的每个版本的文件数据,或者只保存了最后一个(覆盖)。

现在,通过下面的代码我试图根据他们的版本获取文件内容(数据),但我无法做到。

import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream;

import javax.jcr.LoginException; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.version.Version; import javax.jcr.version.VersionHistory; import javax.jcr.version.VersionIterator; import javax.jcr.version.VersionManager;

import org.apache.jackrabbit.oak.Oak; import org.apache.jackrabbit.oak.commons.IOUtils; import org.apache.jackrabbit.oak.jcr.Jcr; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;

import com.mongodb.DB; import com.mongodb.MongoClient;

public class Retrive {

public static void main(String[] args) throws LoginException,
        RepositoryException, IOException {
    // TODO Auto-generated method stub

    Node childNode;

    DB db = new MongoClient("127.0.0.1", 27017).getDB("mydb");
    DocumentNodeStore ns = new DocumentMK.Builder().setMongoDB(db)
            .getNodeStore();
    System.out.println("start");
    Repository repo = new Jcr(new Oak(ns)).with(new OpenSecurityProvider())
            .createRepository();
    String multiPartFileName = "pdf";
    Node document = null;
    Node fileNode;
    Session session = repo.login();
    VersionManager vm = session.getWorkspace().getVersionManager();
    Node root = session.getRootNode();
    if (root.hasNode("pdfFileNode")) {
        document = root.getNode("pdfFileNode");
        System.out.println("pdfFileNode available");
    }

    NodeIterator nodeIterator = document.getNodes();
    System.out.println("Size of nodeIterator :" + nodeIterator.getSize());
    Node nameFile;

    VersionHistory history = vm.getVersionHistory(document.getPath());
    for (VersionIterator it = history.getAllVersions(); it.hasNext();) {
        Version version = (Version) it.next();
        System.out.println("Version: " + version.getName() + " created on "
                + version.getCreated().getTime());

        if (version.getName().equals("1.1")) {
            while (nodeIterator.hasNext()) {
                nameFile = nodeIterator.nextNode();
                String filename = nameFile.getName();
                System.out.println("filename :" + filename);
                if (filename.equals("pdf")) {
                    Node jcrContent = nameFile.getNode("jcr:content");
                    InputStream is = jcrContent.getProperty("jcr:data")
                            .getBinary().getStream();

                    BufferedInputStream bis = new BufferedInputStream(is);
                    ByteArrayOutputStream buf = new ByteArrayOutputStream();
                    int result = bis.read();
                    while (result != -1) {
                        byte b = (byte) result;
                        buf.write(b);
                        result = bis.read();
                    }

                    System.out.println("plain text: " + buf.toString());

                    break;
                } else {
                    System.out.println("not found");
                }
            }
        }
    }
    System.out.println("end");
}
}

任何人都可以让我知道缺少什么,或者根据版本检索所有文件内容(数据)的最佳方法是什么?

0 个答案:

没有答案