]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java
6c7c06b841b1d10bacc5f55c0eaf3612977b53f8
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.jcr
;
19 import java
.io
.InputStream
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Calendar
;
22 import java
.util
.List
;
24 import javax
.jcr
.Binary
;
25 import javax
.jcr
.Node
;
26 import javax
.jcr
.Property
;
27 import javax
.jcr
.RepositoryException
;
28 import javax
.jcr
.Session
;
29 import javax
.jcr
.nodetype
.NodeType
;
30 import javax
.jcr
.version
.Version
;
31 import javax
.jcr
.version
.VersionHistory
;
32 import javax
.jcr
.version
.VersionIterator
;
34 import org
.apache
.commons
.logging
.Log
;
35 import org
.apache
.commons
.logging
.LogFactory
;
36 import org
.argeo
.ArgeoException
;
39 * Bridge Spring resources and JCR folder / files semantics (nt:folder /
40 * nt:file), supporting versioning as well.
42 public class JcrResourceAdapter
{
43 private final static Log log
= LogFactory
.getLog(JcrResourceAdapter
.class);
45 private Session session
;
47 private Boolean versioning
= true;
48 private String defaultEncoding
= "UTF-8";
50 // private String restoreBase = "/.restore";
52 public JcrResourceAdapter() {
55 public JcrResourceAdapter(Session session
) {
56 this.session
= session
;
59 public void mkdirs(String path
) {
60 JcrUtils
.mkdirs(session(), path
, NodeType
.NT_FOLDER
,
61 NodeType
.NT_FOLDER
, versioning
);
64 public void create(String path
, InputStream in
, String mimeType
) {
66 if (session().itemExists(path
)) {
67 throw new ArgeoException("Node " + path
+ " already exists.");
70 int index
= path
.lastIndexOf('/');
71 String parentPath
= path
.substring(0, index
);
72 if (parentPath
.equals(""))
74 String fileName
= path
.substring(index
+ 1);
75 if (!session().itemExists(parentPath
))
76 throw new ArgeoException("Parent folder of node " + path
77 + " does not exist: " + parentPath
);
79 Node folderNode
= (Node
) session().getItem(parentPath
);
80 Node fileNode
= folderNode
.addNode(fileName
, "nt:file");
82 Node contentNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
85 contentNode
.setProperty(Property
.JCR_MIMETYPE
, mimeType
);
86 contentNode
.setProperty(Property
.JCR_ENCODING
, defaultEncoding
);
87 Binary binary
= session().getValueFactory().createBinary(in
);
88 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
89 JcrUtils
.closeQuietly(binary
);
90 Calendar lastModified
= Calendar
.getInstance();
91 // lastModified.setTimeInMillis(file.lastModified());
92 contentNode
.setProperty(Property
.JCR_LAST_MODIFIED
, lastModified
);
93 // resNode.addMixin("mix:referenceable");
96 fileNode
.addMixin("mix:versionable");
101 session().getWorkspace().getVersionManager()
102 .checkin(fileNode
.getPath());
104 if (log
.isDebugEnabled())
105 log
.debug("Created " + path
);
106 } catch (Exception e
) {
107 throw new ArgeoException("Cannot create node for " + path
, e
);
112 public void update(String path
, InputStream in
) {
115 if (!session().itemExists(path
)) {
117 // FIXME: using javax.activation leads to conflict between Java
118 // 1.5 and 1.6 (since javax.activation was included in Java 1.6)
119 // String type = new MimetypesFileTypeMap()
120 // .getContentType(FilenameUtils.getName(path));
121 create(path
, in
, type
);
125 Node fileNode
= (Node
) session().getItem(path
);
126 Node contentNode
= fileNode
.getNode(Property
.JCR_CONTENT
);
128 session().getWorkspace().getVersionManager()
129 .checkout(fileNode
.getPath());
130 Binary binary
= session().getValueFactory().createBinary(in
);
131 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
132 JcrUtils
.closeQuietly(binary
);
133 Calendar lastModified
= Calendar
.getInstance();
134 // lastModified.setTimeInMillis(file.lastModified());
135 contentNode
.setProperty(Property
.JCR_LAST_MODIFIED
, lastModified
);
139 session().getWorkspace().getVersionManager()
140 .checkin(fileNode
.getPath());
142 if (log
.isDebugEnabled())
143 log
.debug("Updated " + path
);
144 } catch (Exception e
) {
145 throw new ArgeoException("Cannot update node " + path
, e
);
149 public List
<Calendar
> listVersions(String path
) {
151 throw new ArgeoException("Versioning is not activated");
154 List
<Calendar
> versions
= new ArrayList
<Calendar
>();
155 Node fileNode
= (Node
) session().getItem(path
);
156 VersionHistory history
= session().getWorkspace()
157 .getVersionManager().getVersionHistory(fileNode
.getPath());
158 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
159 Version version
= (Version
) it
.next();
160 versions
.add(version
.getCreated());
161 if (log
.isTraceEnabled()) {
167 } catch (Exception e
) {
168 throw new ArgeoException("Cannot list version of node " + path
, e
);
172 public InputStream
retrieve(String path
) {
174 Node node
= (Node
) session().getItem(
175 path
+ "/" + Property
.JCR_CONTENT
);
176 Property property
= node
.getProperty(Property
.JCR_DATA
);
177 return property
.getBinary().getStream();
178 } catch (Exception e
) {
179 throw new ArgeoException("Cannot retrieve " + path
, e
);
183 public synchronized InputStream
retrieve(String path
, Integer revision
) {
185 throw new ArgeoException("Versioning is not activated");
188 Node fileNode
= (Node
) session().getItem(path
);
189 VersionHistory history
= session().getWorkspace()
190 .getVersionManager().getVersionHistory(fileNode
.getPath());
192 Version version
= null;
193 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
194 version
= (Version
) it
.next();
195 if (count
== revision
+ 1) {
196 InputStream in
= fromVersion(version
);
197 if (log
.isDebugEnabled())
198 log
.debug("Retrieved " + path
+ " at revision "
204 } catch (Exception e
) {
205 throw new ArgeoException("Cannot retrieve version " + revision
209 throw new ArgeoException("Version " + revision
210 + " does not exist for node " + path
);
213 protected InputStream
fromVersion(Version version
)
214 throws RepositoryException
{
215 Node frozenNode
= version
.getNode("jcr:frozenNode");
216 InputStream in
= frozenNode
.getNode(Property
.JCR_CONTENT
)
217 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
221 protected Session
session() {
225 public void setVersioning(Boolean versioning
) {
226 this.versioning
= versioning
;
229 public void setDefaultEncoding(String defaultEncoding
) {
230 this.defaultEncoding
= defaultEncoding
;
233 protected String
fill(Integer number
) {
235 String str
= number
.toString();
236 for (int i
= str
.length(); i
< size
; i
++) {
242 public void setSession(Session session
) {
243 this.session
= session
;