]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jcr/JcrResourceAdapter.java
1 package org
.argeo
.server
.jcr
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Calendar
;
9 import javax
.activation
.MimetypesFileTypeMap
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
15 import javax
.jcr
.SimpleCredentials
;
16 import javax
.jcr
.version
.Version
;
17 import javax
.jcr
.version
.VersionHistory
;
18 import javax
.jcr
.version
.VersionIterator
;
20 import org
.apache
.commons
.io
.FilenameUtils
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.springframework
.beans
.factory
.DisposableBean
;
26 import org
.springframework
.beans
.factory
.InitializingBean
;
27 import org
.springframework
.core
.io
.Resource
;
29 public class JcrResourceAdapter
implements InitializingBean
, DisposableBean
{
30 private final static Log log
= LogFactory
.getLog(JcrResourceAdapter
.class);
32 private Repository repository
;
34 private String username
;
35 private String password
;
37 private Session session
;
39 private Boolean versioning
= true;
40 private String defaultEncoding
= "UTF-8";
42 // private String restoreBase = "/.restore";
44 public void mkdirs(String path
) {
45 JcrUtils
.mkdirs(session(), path
, "nt:folder", versioning
);
48 public void create(String path
, Resource file
, String mimeType
) {
50 create(path
, file
.getInputStream(), mimeType
);
51 } catch (IOException e
) {
52 throw new ArgeoException("Cannot read " + file
, e
);
56 public void create(String path
, InputStream in
, String mimeType
) {
58 if (session().itemExists(path
)) {
59 throw new ArgeoException("Node " + path
+ " already exists.");
62 int index
= path
.lastIndexOf('/');
63 String parentPath
= path
.substring(0, index
);
64 if (parentPath
.equals(""))
66 String fileName
= path
.substring(index
+ 1);
67 if (!session().itemExists(parentPath
))
68 throw new ArgeoException("Parent folder of node " + path
69 + " does not exist: " + parentPath
);
71 Node folderNode
= (Node
) session().getItem(parentPath
);
72 Node fileNode
= folderNode
.addNode(fileName
, "nt:file");
74 Node contentNode
= fileNode
.addNode("jcr:content", "nt:resource");
76 contentNode
.setProperty("jcr:mimeType", mimeType
);
77 contentNode
.setProperty("jcr:encoding", defaultEncoding
);
78 contentNode
.setProperty("jcr:data", in
);
79 Calendar lastModified
= Calendar
.getInstance();
80 // lastModified.setTimeInMillis(file.lastModified());
81 contentNode
.setProperty("jcr:lastModified", lastModified
);
82 // resNode.addMixin("mix:referenceable");
85 fileNode
.addMixin("mix:versionable");
92 if (log
.isDebugEnabled())
93 log
.debug("Created " + path
);
94 } catch (Exception e
) {
95 throw new ArgeoException("Cannot create node for " + path
, e
);
100 public void update(String path
, Resource file
) {
102 update(path
, file
.getInputStream());
103 } catch (IOException e
) {
104 throw new ArgeoException("Cannot read " + file
, e
);
108 public void update(String path
, InputStream in
) {
111 if (!session().itemExists(path
)) {
112 String type
= new MimetypesFileTypeMap()
113 .getContentType(FilenameUtils
.getName(path
));
114 create(path
, in
, type
);
118 Node fileNode
= (Node
) session().getItem(path
);
119 Node contentNode
= fileNode
.getNode("jcr:content");
121 contentNode
.setProperty("jcr:data", in
);
122 Calendar lastModified
= Calendar
.getInstance();
123 // lastModified.setTimeInMillis(file.lastModified());
124 contentNode
.setProperty("jcr:lastModified", lastModified
);
129 if (log
.isDebugEnabled())
130 log
.debug("Updated " + path
);
131 } catch (Exception e
) {
132 throw new ArgeoException("Cannot update node " + path
, e
);
136 public List
<Calendar
> listVersions(String path
) {
138 throw new ArgeoException("Versioning is not activated");
141 List
<Calendar
> versions
= new ArrayList
<Calendar
>();
142 Node fileNode
= (Node
) session().getItem(path
);
143 VersionHistory history
= fileNode
.getVersionHistory();
144 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
145 Version version
= (Version
) it
.next();
146 versions
.add(version
.getCreated());
147 if (log
.isTraceEnabled()) {
153 } catch (Exception e
) {
154 throw new ArgeoException("Cannot list version of node " + path
, e
);
158 public InputStream
retrieve(String path
) {
160 Node node
= (Node
) session().getItem(path
+ "/jcr:content");
161 Property property
= node
.getProperty("jcr:data");
162 return property
.getStream();
163 } catch (Exception e
) {
164 throw new ArgeoException("Cannot retrieve " + path
, e
);
168 public synchronized InputStream
retrieve(String path
, Integer revision
) {
170 throw new ArgeoException("Versioning is not activated");
173 Node fileNode
= (Node
) session().getItem(path
);
174 VersionHistory history
= fileNode
.getVersionHistory();
176 Version version
= null;
177 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
178 version
= (Version
) it
.next();
179 if (count
== revision
+ 1) {
180 InputStream in
= fromVersion(version
);
181 if (log
.isDebugEnabled())
182 log
.debug("Retrieved " + path
+ " at revision "
188 } catch (Exception e
) {
189 throw new ArgeoException("Cannot retrieve version " + revision
193 throw new ArgeoException("Version " + revision
194 + " does not exist for node " + path
);
197 protected InputStream
fromVersion(Version version
)
198 throws RepositoryException
{
199 Node frozenNode
= version
.getNode("jcr:frozenNode");
200 InputStream in
= frozenNode
.getNode("jcr:content").getProperty(
201 "jcr:data").getStream();
205 protected Session
session() {
209 public void afterPropertiesSet() throws Exception
{
210 session
= repository
.login(new SimpleCredentials(username
, password
214 public void destroy() throws Exception
{
218 public void setRepository(Repository repository
) {
219 this.repository
= repository
;
222 public void setUsername(String username
) {
223 this.username
= username
;
226 public void setPassword(String password
) {
227 this.password
= password
;
230 public void setVersioning(Boolean versioning
) {
231 this.versioning
= versioning
;
234 public void setDefaultEncoding(String defaultEncoding
) {
235 this.defaultEncoding
= defaultEncoding
;
238 protected String
fill(Integer number
) {
240 String str
= number
.toString();
241 for (int i
= str
.length(); i
< size
; i
++) {