]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrResourceAdapter.java
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 JcrResourceAdapter() {
37 public JcrResourceAdapter(Session session
) {
38 this.session
= session
;
41 public void mkdirs(String path
) {
42 JcrUtils
.mkdirs(session(), path
, "nt:folder", versioning
);
45 public void create(String path
, Resource file
, String mimeType
) {
47 create(path
, file
.getInputStream(), mimeType
);
48 } catch (IOException e
) {
49 throw new ArgeoException("Cannot read " + file
, e
);
53 public void create(String path
, InputStream in
, String mimeType
) {
55 if (session().itemExists(path
)) {
56 throw new ArgeoException("Node " + path
+ " already exists.");
59 int index
= path
.lastIndexOf('/');
60 String parentPath
= path
.substring(0, index
);
61 if (parentPath
.equals(""))
63 String fileName
= path
.substring(index
+ 1);
64 if (!session().itemExists(parentPath
))
65 throw new ArgeoException("Parent folder of node " + path
66 + " does not exist: " + parentPath
);
68 Node folderNode
= (Node
) session().getItem(parentPath
);
69 Node fileNode
= folderNode
.addNode(fileName
, "nt:file");
71 Node contentNode
= fileNode
.addNode("jcr:content", "nt:resource");
73 contentNode
.setProperty("jcr:mimeType", mimeType
);
74 contentNode
.setProperty("jcr:encoding", defaultEncoding
);
75 contentNode
.setProperty("jcr:data", in
);
76 Calendar lastModified
= Calendar
.getInstance();
77 // lastModified.setTimeInMillis(file.lastModified());
78 contentNode
.setProperty("jcr:lastModified", lastModified
);
79 // resNode.addMixin("mix:referenceable");
82 fileNode
.addMixin("mix:versionable");
89 if (log
.isDebugEnabled())
90 log
.debug("Created " + path
);
91 } catch (Exception e
) {
92 throw new ArgeoException("Cannot create node for " + path
, e
);
97 public void update(String path
, Resource file
) {
99 update(path
, file
.getInputStream());
100 } catch (IOException e
) {
101 throw new ArgeoException("Cannot read " + file
, e
);
105 public void update(String path
, InputStream in
) {
108 if (!session().itemExists(path
)) {
109 String type
= new MimetypesFileTypeMap()
110 .getContentType(FilenameUtils
.getName(path
));
111 create(path
, in
, type
);
115 Node fileNode
= (Node
) session().getItem(path
);
116 Node contentNode
= fileNode
.getNode("jcr:content");
118 contentNode
.setProperty("jcr:data", in
);
119 Calendar lastModified
= Calendar
.getInstance();
120 // lastModified.setTimeInMillis(file.lastModified());
121 contentNode
.setProperty("jcr:lastModified", lastModified
);
126 if (log
.isDebugEnabled())
127 log
.debug("Updated " + path
);
128 } catch (Exception e
) {
129 throw new ArgeoException("Cannot update node " + path
, e
);
133 public List
<Calendar
> listVersions(String path
) {
135 throw new ArgeoException("Versioning is not activated");
138 List
<Calendar
> versions
= new ArrayList
<Calendar
>();
139 Node fileNode
= (Node
) session().getItem(path
);
140 VersionHistory history
= fileNode
.getVersionHistory();
141 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
142 Version version
= (Version
) it
.next();
143 versions
.add(version
.getCreated());
144 if (log
.isTraceEnabled()) {
150 } catch (Exception e
) {
151 throw new ArgeoException("Cannot list version of node " + path
, e
);
155 public InputStream
retrieve(String path
) {
157 Node node
= (Node
) session().getItem(path
+ "/jcr:content");
158 Property property
= node
.getProperty("jcr:data");
159 return property
.getStream();
160 } catch (Exception e
) {
161 throw new ArgeoException("Cannot retrieve " + path
, e
);
165 public synchronized InputStream
retrieve(String path
, Integer revision
) {
167 throw new ArgeoException("Versioning is not activated");
170 Node fileNode
= (Node
) session().getItem(path
);
171 VersionHistory history
= fileNode
.getVersionHistory();
173 Version version
= null;
174 for (VersionIterator it
= history
.getAllVersions(); it
.hasNext();) {
175 version
= (Version
) it
.next();
176 if (count
== revision
+ 1) {
177 InputStream in
= fromVersion(version
);
178 if (log
.isDebugEnabled())
179 log
.debug("Retrieved " + path
+ " at revision "
185 } catch (Exception e
) {
186 throw new ArgeoException("Cannot retrieve version " + revision
190 throw new ArgeoException("Version " + revision
191 + " does not exist for node " + path
);
194 protected InputStream
fromVersion(Version version
)
195 throws RepositoryException
{
196 Node frozenNode
= version
.getNode("jcr:frozenNode");
197 InputStream in
= frozenNode
.getNode("jcr:content").getProperty(
198 "jcr:data").getStream();
202 protected Session
session() {
206 public void setVersioning(Boolean versioning
) {
207 this.versioning
= versioning
;
210 public void setDefaultEncoding(String defaultEncoding
) {
211 this.defaultEncoding
= defaultEncoding
;
214 protected String
fill(Integer number
) {
216 String str
= number
.toString();
217 for (int i
= str
.length(); i
< size
; i
++) {
223 public void setSession(Session session
) {
224 this.session
= session
;