jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Stolwijk <nick_stolw...@planet.nl>
Subject Problem with saving binary node data
Date Fri, 10 Aug 2007 13:32:10 GMT
We have a problem with Jackrabbit 1.3.1. All the directory and file 
nodes get created, only my files stay empty. Can somebody help me with 
this problem? My code:

    public static final String NODE_PROP_TYPE_FOLDER   = "nt:folder";
    public static final String NODE_PROP_TYPE_FILE     = "nt:file";
    public static final String NODE_PROP_TYPE_DATA     = "jcr:data";
    public static final String NODE_PROP_MIME_TYPE     = "jcr:mimeType";
    public static final String NODE_PROP_TYPE_CONTENT  = "jcr:content";
    public static final String NODE_PROP_TYPE_RESOURCE = "nt:resource";
    public static final String NODE_PROP_LAST_MODIFIED = "jcr:lastModified";
    public static final String NODE_PROP_CREATED       = "jcr:created";
    public static final String NODE_PROP_ENCODING      = "jcr:encoding";

    /**
     * Method importFile imports the specified file to the location 
specified by the node path into the JCR.
     *
     * @param file of type File the file to import into the JCR
     * @param relativeSubNode of type String a nodePath relative to the 
configured rootName which should not start or end with a "/"
     * @param relativeNodePath of type String a nodePath relative to the 
specified sub node which should not start or end with a "/"
     * @throws JackrabbitMediaImportException when the specified file is 
invalid or the specified path is invalid
     */
    public void importFile(final String relativeSubNode, final String 
relativeNodePath, final File file)
            throws JackrabbitMediaImportException {
        checkInitialized();

        try {
            if (null == file) {
                throw new JackrabbitMediaImportException("Failed to 
import file <null>.");
            } else if (!file.exists()) {
                throw new JackrabbitMediaImportException("Failed to import "
                        + file.getPath() + ", the file does not exist.");
            }

            log.debug("Import " + rootName + "/" + relativeSubNode + "/" 
+ relativeNodePath + "/" + file.getName());
            Node root = getSession().getRootNode();
            // Navigate to (optionally create on the fly) the location 
specified by the node path
            Node currentFolderNode = getOrCreateNodePath(root,
                    rootName + "/" + relativeSubNode + "/" + 
getRelativeNodePathCasing(relativeNodePath));

            log.debug("File will be imported as " + 
currentFolderNode.getPath() + "/" + file.getName());
            // Store the specified file into the specified node path
            storeFile(currentFolderNode, file);
            root.save();
            root.getSession().save();
        } catch (InvalidNodePathException e) {
            throw new JackrabbitMediaImportException("Failed to import "
                    + file.getPath() + " to " + relativeNodePath + " of 
the JCR", e);
        } catch (RepositoryException e) {
            throw new JackrabbitMediaImportException("Failed to import " 
+ file.getPath() + " into JCR", e);
        }
    }


    /**
     * Method storeFile does the actual streaming of the file to the JCR.
     *
     * @param folder of type Node which will contain the actual file
     * @param file of type File the file to store
     * @throws JackrabbitMediaImportException when
     */
    private void storeFile(final Node folder, final File file) throws 
JackrabbitMediaImportException {
        synchronized (mutex) {
            try {
                ValueFactory valueFactory = 
folder.getSession().getValueFactory();

                String filename = 
getFilenamePlusExtensionCasing(file.getName());
                Node fileNode = folder.addNode(filename, 
NODE_PROP_TYPE_FILE);
                Node contentNode = 
fileNode.addNode(NODE_PROP_TYPE_CONTENT, NODE_PROP_TYPE_RESOURCE);
                contentNode.setProperty(NODE_PROP_TYPE_DATA, new 
FileInputStream(file));

                MimetypesFileTypeMap mimetypesFileTypeMap = new 
MimetypesFileTypeMap();
                // It does not know PDF by default (...)
                mimetypesFileTypeMap.addMimeTypes("application/pdf pdf 
PDF");

                contentNode.setProperty(NODE_PROP_MIME_TYPE, 
mimetypesFileTypeMap.getContentType(file));
                contentNode.setProperty(NODE_PROP_LAST_MODIFIED, 
valueFactory.createValue(Calendar.getInstance()));
                contentNode.setProperty(NODE_PROP_TYPE_DATA, "");
                contentNode.setProperty(NODE_PROP_ENCODING, "");

                // Save the newly added information
                folder.save();
            } catch (RepositoryException e) {
                throw new JackrabbitMediaImportException("Unable to read 
from / write to repository.", e);
            } catch (FileNotFoundException e) {
                throw new JackrabbitMediaImportException("Failed to 
import " + file.getPath() + " into JCR", e);
            }
        }
    }

With regards,

Nick Stolwijk

Mime
View raw message