]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java
3c69e0b1efc043f5a6cef71a75b9f86183769564
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
.RepositoryException
;
13 import javax
.jcr
.Session
;
14 import javax
.jcr
.version
.Version
;
15 import javax
.jcr
.version
.VersionHistory
;
16 import javax
.jcr
.version
.VersionIterator
;
18 import org
.apache
.commons
.io
.FilenameUtils
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.ArgeoException
;
22 import org
.springframework
.core
.io
.Resource
;
24 public class JcrResourceAdapter
{
25 private final static Log log
= LogFactory
.getLog(JcrResourceAdapter
.class);
27 private Session session
;
29 private Boolean versioning
= true;
30 private String defaultEncoding
= "UTF-8";
32 // private String restoreBase = "/.restore";
34 public void mkdirs(String path
) {
35 JcrUtils
.mkdirs(session(), path
, "nt:folder", versioning
);
38 public void create(String path
, Resource file
, String mimeType
) {
40 create(path
, file
.getInputStream(), mimeType
);
41 } catch (IOException e
) {
42 throw new ArgeoException("Cannot read " + file
, e
);
46 public void create(String path
, InputStream in
, String mimeType
) {
48 if (session().itemExists(path
)) {
49 throw new ArgeoException("Node " + path
+ " already exists.");
52 int index
= path
.lastIndexOf('/');
53 String parentPath
= path
.substring(0, index
);
54 if (parentPath
.equals(""))
56 String fileName
= path
.substring(index
+ 1);
57 if (!session().itemExists(parentPath
))
58 throw new ArgeoException("Parent folder of node " + path
59 + " does not exist: " + parentPath
);
61 Node folderNode
= (Node
) session().getItem(parentPath
);
62 Node fileNode
= folderNode
.addNode(fileName
, "nt:file");
64 Node contentNode
= fileNode
.addNode("jcr:content", "nt:resource");
66 contentNode
.setProperty("jcr:mimeType", mimeType
);
67 contentNode
.setProperty("jcr:encoding", defaultEncoding
);
68 contentNode
.setProperty("jcr:data", in
);
69 Calendar lastModified
= Calendar
.getInstance();
70 // lastModified.setTimeInMillis(file.lastModified());
71 contentNode
.setProperty("jcr:lastModified", lastModified
);
72 // resNode.addMixin("mix:referenceable");
75 fileNode
.addMixin("mix:versionable");
82 if (log
.isDebugEnabled())
83 log
.debug("Created " + path
);
84 } catch (Exception e
) {
85 throw new ArgeoException("Cannot create node for " + path
, e
);
90 public void update(String path
, Resource file
) {
92 update(path
, file
.getInputStream());
93 } catch (IOException e
) {
94 throw new ArgeoException("Cannot read " + file
, e
);
98 public void update(String path
, InputStream in
) {
101 if (!session().itemExists(path
)) {
102 String type
= new MimetypesFileTypeMap()
103 .getContentType(FilenameUtils
.getName(path
));
104 create(path
, in
, type
);
108 Node fileNode
= (Node
) session().getItem(path
);
109 Node contentNode
= fileNode
.getNode("jcr:content");
111 contentNode
.setProperty("jcr:data", in
);
112 Calendar lastModified
= Calendar
.getInstance();
113 // lastModified.setTimeInMillis(file.lastModified());
114 contentNode
.setProperty("jcr:lastModified", lastModified
);
119 if (log
.isDebugEnabled())
120 log
.debug("Updated " + path
);
121 } catch (Exception e
) {
122 throw new ArgeoException("Cannot update node " + path
, e
);
126 public List
<Calendar
> listVersions(String path
) {
128 throw new ArgeoException("Versioning is not activated");
131 List
<Calendar
> versions
= new ArrayList
<Calendar
>();
132 Node fileNode
= (Node
) session().getItem(path
);
133 VersionHistory history
= fileNode
.getVersionHistory();
134 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
135 Version version
= (Version
) it
.next();
136 versions
.add(version
.getCreated());
137 if (log
.isTraceEnabled()) {
143 } catch (Exception e
) {
144 throw new ArgeoException("Cannot list version of node " + path
, e
);
148 public InputStream
retrieve(String path
) {
150 Node node
= (Node
) session().getItem(path
+ "/jcr:content");
151 Property property
= node
.getProperty("jcr:data");
152 return property
.getStream();
153 } catch (Exception e
) {
154 throw new ArgeoException("Cannot retrieve " + path
, e
);
158 public synchronized InputStream
retrieve(String path
, Integer revision
) {
160 throw new ArgeoException("Versioning is not activated");
163 Node fileNode
= (Node
) session().getItem(path
);
164 VersionHistory history
= fileNode
.getVersionHistory();
166 Version version
= null;
167 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
168 version
= (Version
) it
.next();
169 if (count
== revision
+ 1) {
170 InputStream in
= fromVersion(version
);
171 if (log
.isDebugEnabled())
172 log
.debug("Retrieved " + path
+ " at revision "
178 } catch (Exception e
) {
179 throw new ArgeoException("Cannot retrieve version " + revision
183 throw new ArgeoException("Version " + revision
184 + " does not exist for node " + path
);
187 protected InputStream
fromVersion(Version version
)
188 throws RepositoryException
{
189 Node frozenNode
= version
.getNode("jcr:frozenNode");
190 InputStream in
= frozenNode
.getNode("jcr:content").getProperty(
191 "jcr:data").getStream();
195 protected Session
session() {
199 public void setVersioning(Boolean versioning
) {
200 this.versioning
= versioning
;
203 public void setDefaultEncoding(String defaultEncoding
) {
204 this.defaultEncoding
= defaultEncoding
;
207 protected String
fill(Integer number
) {
209 String str
= number
.toString();
210 for (int i
= str
.length(); i
< size
; i
++) {
216 public void setSession(Session session
) {
217 this.session
= session
;