+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.argeo.jcr;
-import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
-import javax.activation.MimetypesFileTypeMap;
+import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.argeo.ArgeoException;
-import org.springframework.core.io.Resource;
+/**
+ * Bridge Spring resources and JCR folder / files semantics (nt:folder /
+ * nt:file), supporting versioning as well.
+ */
public class JcrResourceAdapter {
private final static Log log = LogFactory.getLog(JcrResourceAdapter.class);
// private String restoreBase = "/.restore";
- public void mkdirs(String path) {
- JcrUtils.mkdirs(session(), path, "nt:folder", versioning);
+ public JcrResourceAdapter() {
}
- public void create(String path, Resource file, String mimeType) {
- try {
- create(path, file.getInputStream(), mimeType);
- } catch (IOException e) {
- throw new ArgeoException("Cannot read " + file, e);
- }
+ public JcrResourceAdapter(Session session) {
+ this.session = session;
+ }
+
+ public void mkdirs(String path) {
+ JcrUtils.mkdirs(session(), path, NodeType.NT_FOLDER,
+ NodeType.NT_FOLDER, versioning);
}
public void create(String path, InputStream in, String mimeType) {
Node folderNode = (Node) session().getItem(parentPath);
Node fileNode = folderNode.addNode(fileName, "nt:file");
- Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
+ Node contentNode = fileNode.addNode(Property.JCR_CONTENT,
+ "nt:resource");
if (mimeType != null)
- contentNode.setProperty("jcr:mimeType", mimeType);
- contentNode.setProperty("jcr:encoding", defaultEncoding);
- contentNode.setProperty("jcr:data", in);
+ contentNode.setProperty(Property.JCR_MIMETYPE, mimeType);
+ contentNode.setProperty(Property.JCR_ENCODING, defaultEncoding);
+ Binary binary = session().getValueFactory().createBinary(in);
+ contentNode.setProperty(Property.JCR_DATA, binary);
+ JcrUtils.closeQuietly(binary);
Calendar lastModified = Calendar.getInstance();
// lastModified.setTimeInMillis(file.lastModified());
- contentNode.setProperty("jcr:lastModified", lastModified);
+ contentNode.setProperty(Property.JCR_LAST_MODIFIED, lastModified);
// resNode.addMixin("mix:referenceable");
if (versioning)
session().save();
if (versioning)
- fileNode.checkin();
+ session().getWorkspace().getVersionManager()
+ .checkin(fileNode.getPath());
if (log.isDebugEnabled())
log.debug("Created " + path);
}
- public void update(String path, Resource file) {
- try {
- update(path, file.getInputStream());
- } catch (IOException e) {
- throw new ArgeoException("Cannot read " + file, e);
- }
- }
-
public void update(String path, InputStream in) {
try {
if (!session().itemExists(path)) {
- String type = new MimetypesFileTypeMap()
- .getContentType(FilenameUtils.getName(path));
+ String type = null;
+ // FIXME: using javax.activation leads to conflict between Java
+ // 1.5 and 1.6 (since javax.activation was included in Java 1.6)
+ // String type = new MimetypesFileTypeMap()
+ // .getContentType(FilenameUtils.getName(path));
create(path, in, type);
return;
}
Node fileNode = (Node) session().getItem(path);
- Node contentNode = fileNode.getNode("jcr:content");
- fileNode.checkout();
- contentNode.setProperty("jcr:data", in);
+ Node contentNode = fileNode.getNode(Property.JCR_CONTENT);
+ if (versioning)
+ session().getWorkspace().getVersionManager()
+ .checkout(fileNode.getPath());
+ Binary binary = session().getValueFactory().createBinary(in);
+ contentNode.setProperty(Property.JCR_DATA, binary);
+ JcrUtils.closeQuietly(binary);
Calendar lastModified = Calendar.getInstance();
// lastModified.setTimeInMillis(file.lastModified());
- contentNode.setProperty("jcr:lastModified", lastModified);
+ contentNode.setProperty(Property.JCR_LAST_MODIFIED, lastModified);
session().save();
- fileNode.checkin();
+ if (versioning)
+ session().getWorkspace().getVersionManager()
+ .checkin(fileNode.getPath());
if (log.isDebugEnabled())
log.debug("Updated " + path);
try {
List<Calendar> versions = new ArrayList<Calendar>();
Node fileNode = (Node) session().getItem(path);
- VersionHistory history = fileNode.getVersionHistory();
+ VersionHistory history = session().getWorkspace()
+ .getVersionManager().getVersionHistory(fileNode.getPath());
for (VersionIterator it = history.getAllVersions(); it.hasNext();) {
Version version = (Version) it.next();
versions.add(version.getCreated());
public InputStream retrieve(String path) {
try {
- Node node = (Node) session().getItem(path + "/jcr:content");
- Property property = node.getProperty("jcr:data");
- return property.getStream();
+ Node node = (Node) session().getItem(
+ path + "/" + Property.JCR_CONTENT);
+ Property property = node.getProperty(Property.JCR_DATA);
+ return property.getBinary().getStream();
} catch (Exception e) {
throw new ArgeoException("Cannot retrieve " + path, e);
}
try {
Node fileNode = (Node) session().getItem(path);
- VersionHistory history = fileNode.getVersionHistory();
+ VersionHistory history = session().getWorkspace()
+ .getVersionManager().getVersionHistory(fileNode.getPath());
int count = 0;
Version version = null;
for (VersionIterator it = history.getAllVersions(); it.hasNext();) {
protected InputStream fromVersion(Version version)
throws RepositoryException {
Node frozenNode = version.getNode("jcr:frozenNode");
- InputStream in = frozenNode.getNode("jcr:content").getProperty(
- "jcr:data").getStream();
+ InputStream in = frozenNode.getNode(Property.JCR_CONTENT)
+ .getProperty(Property.JCR_DATA).getBinary().getStream();
return in;
}
public void setSession(Session session) {
this.session = session;
}
-
-
+
}