]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrResourceAdapter.java
3c69e0b1efc043f5a6cef71a75b9f86183769564
[lgpl/argeo-commons.git] / JcrResourceAdapter.java
1 package org.argeo.jcr;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.List;
8
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;
17
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;
23
24 public class JcrResourceAdapter {
25 private final static Log log = LogFactory.getLog(JcrResourceAdapter.class);
26
27 private Session session;
28
29 private Boolean versioning = true;
30 private String defaultEncoding = "UTF-8";
31
32 // private String restoreBase = "/.restore";
33
34 public void mkdirs(String path) {
35 JcrUtils.mkdirs(session(), path, "nt:folder", versioning);
36 }
37
38 public void create(String path, Resource file, String mimeType) {
39 try {
40 create(path, file.getInputStream(), mimeType);
41 } catch (IOException e) {
42 throw new ArgeoException("Cannot read " + file, e);
43 }
44 }
45
46 public void create(String path, InputStream in, String mimeType) {
47 try {
48 if (session().itemExists(path)) {
49 throw new ArgeoException("Node " + path + " already exists.");
50 }
51
52 int index = path.lastIndexOf('/');
53 String parentPath = path.substring(0, index);
54 if (parentPath.equals(""))
55 parentPath = "/";
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);
60
61 Node folderNode = (Node) session().getItem(parentPath);
62 Node fileNode = folderNode.addNode(fileName, "nt:file");
63
64 Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
65 if (mimeType != null)
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");
73
74 if (versioning)
75 fileNode.addMixin("mix:versionable");
76
77 session().save();
78
79 if (versioning)
80 fileNode.checkin();
81
82 if (log.isDebugEnabled())
83 log.debug("Created " + path);
84 } catch (Exception e) {
85 throw new ArgeoException("Cannot create node for " + path, e);
86 }
87
88 }
89
90 public void update(String path, Resource file) {
91 try {
92 update(path, file.getInputStream());
93 } catch (IOException e) {
94 throw new ArgeoException("Cannot read " + file, e);
95 }
96 }
97
98 public void update(String path, InputStream in) {
99 try {
100
101 if (!session().itemExists(path)) {
102 String type = new MimetypesFileTypeMap()
103 .getContentType(FilenameUtils.getName(path));
104 create(path, in, type);
105 return;
106 }
107
108 Node fileNode = (Node) session().getItem(path);
109 Node contentNode = fileNode.getNode("jcr:content");
110 fileNode.checkout();
111 contentNode.setProperty("jcr:data", in);
112 Calendar lastModified = Calendar.getInstance();
113 // lastModified.setTimeInMillis(file.lastModified());
114 contentNode.setProperty("jcr:lastModified", lastModified);
115
116 session().save();
117 fileNode.checkin();
118
119 if (log.isDebugEnabled())
120 log.debug("Updated " + path);
121 } catch (Exception e) {
122 throw new ArgeoException("Cannot update node " + path, e);
123 }
124 }
125
126 public List<Calendar> listVersions(String path) {
127 if (!versioning)
128 throw new ArgeoException("Versioning is not activated");
129
130 try {
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()) {
138 log.debug(version);
139 // debug(version);
140 }
141 }
142 return versions;
143 } catch (Exception e) {
144 throw new ArgeoException("Cannot list version of node " + path, e);
145 }
146 }
147
148 public InputStream retrieve(String path) {
149 try {
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);
155 }
156 }
157
158 public synchronized InputStream retrieve(String path, Integer revision) {
159 if (!versioning)
160 throw new ArgeoException("Versioning is not activated");
161
162 try {
163 Node fileNode = (Node) session().getItem(path);
164 VersionHistory history = fileNode.getVersionHistory();
165 int count = 0;
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 "
173 + revision);
174 return in;
175 }
176 count++;
177 }
178 } catch (Exception e) {
179 throw new ArgeoException("Cannot retrieve version " + revision
180 + " of " + path, e);
181 }
182
183 throw new ArgeoException("Version " + revision
184 + " does not exist for node " + path);
185 }
186
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();
192 return in;
193 }
194
195 protected Session session() {
196 return session;
197 }
198
199 public void setVersioning(Boolean versioning) {
200 this.versioning = versioning;
201 }
202
203 public void setDefaultEncoding(String defaultEncoding) {
204 this.defaultEncoding = defaultEncoding;
205 }
206
207 protected String fill(Integer number) {
208 int size = 4;
209 String str = number.toString();
210 for (int i = str.length(); i < size; i++) {
211 str = "0" + str;
212 }
213 return str;
214 }
215
216 public void setSession(Session session) {
217 this.session = session;
218 }
219
220
221 }