]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/JcrResourceAdapter.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
;
18 import java
.io
.InputStream
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Calendar
;
21 import java
.util
.List
;
23 import javax
.jcr
.Binary
;
24 import javax
.jcr
.Node
;
25 import javax
.jcr
.Property
;
26 import javax
.jcr
.RepositoryException
;
27 import javax
.jcr
.Session
;
28 import javax
.jcr
.nodetype
.NodeType
;
29 import javax
.jcr
.version
.Version
;
30 import javax
.jcr
.version
.VersionHistory
;
31 import javax
.jcr
.version
.VersionIterator
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
37 * Bridge Spring resources and JCR folder / files semantics (nt:folder /
38 * nt:file), supporting versioning as well.
40 public class JcrResourceAdapter
{
41 private final static Log log
= LogFactory
.getLog(JcrResourceAdapter
.class);
43 private Session session
;
45 private Boolean versioning
= true;
46 private String defaultEncoding
= "UTF-8";
48 // private String restoreBase = "/.restore";
50 public JcrResourceAdapter() {
53 public JcrResourceAdapter(Session session
) {
54 this.session
= session
;
57 public void mkdirs(String path
) {
58 JcrUtils
.mkdirs(session(), path
, NodeType
.NT_FOLDER
,
59 NodeType
.NT_FOLDER
, versioning
);
62 public void create(String path
, InputStream in
, String mimeType
) {
64 if (session().itemExists(path
)) {
65 throw new ArgeoJcrException("Node " + path
+ " already exists.");
68 int index
= path
.lastIndexOf('/');
69 String parentPath
= path
.substring(0, index
);
70 if (parentPath
.equals(""))
72 String fileName
= path
.substring(index
+ 1);
73 if (!session().itemExists(parentPath
))
74 throw new ArgeoJcrException("Parent folder of node " + path
75 + " does not exist: " + parentPath
);
77 Node folderNode
= (Node
) session().getItem(parentPath
);
78 Node fileNode
= folderNode
.addNode(fileName
, "nt:file");
80 Node contentNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
83 contentNode
.setProperty(Property
.JCR_MIMETYPE
, mimeType
);
84 contentNode
.setProperty(Property
.JCR_ENCODING
, defaultEncoding
);
85 Binary binary
= session().getValueFactory().createBinary(in
);
86 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
87 JcrUtils
.closeQuietly(binary
);
88 Calendar lastModified
= Calendar
.getInstance();
89 // lastModified.setTimeInMillis(file.lastModified());
90 contentNode
.setProperty(Property
.JCR_LAST_MODIFIED
, lastModified
);
91 // resNode.addMixin("mix:referenceable");
94 fileNode
.addMixin("mix:versionable");
99 session().getWorkspace().getVersionManager()
100 .checkin(fileNode
.getPath());
102 if (log
.isDebugEnabled())
103 log
.debug("Created " + path
);
104 } catch (Exception e
) {
105 throw new ArgeoJcrException("Cannot create node for " + path
, e
);
110 public void update(String path
, InputStream in
) {
113 if (!session().itemExists(path
)) {
115 // FIXME: using javax.activation leads to conflict between Java
116 // 1.5 and 1.6 (since javax.activation was included in Java 1.6)
117 // String type = new MimetypesFileTypeMap()
118 // .getContentType(FilenameUtils.getName(path));
119 create(path
, in
, type
);
123 Node fileNode
= (Node
) session().getItem(path
);
124 Node contentNode
= fileNode
.getNode(Property
.JCR_CONTENT
);
126 session().getWorkspace().getVersionManager()
127 .checkout(fileNode
.getPath());
128 Binary binary
= session().getValueFactory().createBinary(in
);
129 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
130 JcrUtils
.closeQuietly(binary
);
131 Calendar lastModified
= Calendar
.getInstance();
132 // lastModified.setTimeInMillis(file.lastModified());
133 contentNode
.setProperty(Property
.JCR_LAST_MODIFIED
, lastModified
);
137 session().getWorkspace().getVersionManager()
138 .checkin(fileNode
.getPath());
140 if (log
.isDebugEnabled())
141 log
.debug("Updated " + path
);
142 } catch (Exception e
) {
143 throw new ArgeoJcrException("Cannot update node " + path
, e
);
147 public List
<Calendar
> listVersions(String path
) {
149 throw new ArgeoJcrException("Versioning is not activated");
152 List
<Calendar
> versions
= new ArrayList
<Calendar
>();
153 Node fileNode
= (Node
) session().getItem(path
);
154 VersionHistory history
= session().getWorkspace()
155 .getVersionManager().getVersionHistory(fileNode
.getPath());
156 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
157 Version version
= (Version
) it
.next();
158 versions
.add(version
.getCreated());
159 if (log
.isTraceEnabled()) {
165 } catch (Exception e
) {
166 throw new ArgeoJcrException("Cannot list version of node " + path
, e
);
170 public InputStream
retrieve(String path
) {
172 Node node
= (Node
) session().getItem(
173 path
+ "/" + Property
.JCR_CONTENT
);
174 Property property
= node
.getProperty(Property
.JCR_DATA
);
175 return property
.getBinary().getStream();
176 } catch (Exception e
) {
177 throw new ArgeoJcrException("Cannot retrieve " + path
, e
);
181 public synchronized InputStream
retrieve(String path
, Integer revision
) {
183 throw new ArgeoJcrException("Versioning is not activated");
186 Node fileNode
= (Node
) session().getItem(path
);
187 VersionHistory history
= session().getWorkspace()
188 .getVersionManager().getVersionHistory(fileNode
.getPath());
190 Version version
= null;
191 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
192 version
= (Version
) it
.next();
193 if (count
== revision
+ 1) {
194 InputStream in
= fromVersion(version
);
195 if (log
.isDebugEnabled())
196 log
.debug("Retrieved " + path
+ " at revision "
202 } catch (Exception e
) {
203 throw new ArgeoJcrException("Cannot retrieve version " + revision
207 throw new ArgeoJcrException("Version " + revision
208 + " does not exist for node " + path
);
211 protected InputStream
fromVersion(Version version
)
212 throws RepositoryException
{
213 Node frozenNode
= version
.getNode("jcr:frozenNode");
214 InputStream in
= frozenNode
.getNode(Property
.JCR_CONTENT
)
215 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
219 protected Session
session() {
223 public void setVersioning(Boolean versioning
) {
224 this.versioning
= versioning
;
227 public void setDefaultEncoding(String defaultEncoding
) {
228 this.defaultEncoding
= defaultEncoding
;
231 protected String
fill(Integer number
) {
233 String str
= number
.toString();
234 for (int i
= str
.length(); i
< size
; i
++) {
240 public void setSession(Session session
) {
241 this.session
= session
;