]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/jcr/JcrResourceAdapter.java
Prepare next development cycle
[lgpl/argeo-commons.git] / argeo / jcr / 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 JcrResourceAdapter() {
35 }
36
37 public JcrResourceAdapter(Session session) {
38 this.session = session;
39 }
40
41 public void mkdirs(String path) {
42 JcrUtils.mkdirs(session(), path, "nt:folder", versioning);
43 }
44
45 public void create(String path, Resource file, String mimeType) {
46 try {
47 create(path, file.getInputStream(), mimeType);
48 } catch (IOException e) {
49 throw new ArgeoException("Cannot read " + file, e);
50 }
51 }
52
53 public void create(String path, InputStream in, String mimeType) {
54 try {
55 if (session().itemExists(path)) {
56 throw new ArgeoException("Node " + path + " already exists.");
57 }
58
59 int index = path.lastIndexOf('/');
60 String parentPath = path.substring(0, index);
61 if (parentPath.equals(""))
62 parentPath = "/";
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);
67
68 Node folderNode = (Node) session().getItem(parentPath);
69 Node fileNode = folderNode.addNode(fileName, "nt:file");
70
71 Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
72 if (mimeType != null)
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");
80
81 if (versioning)
82 fileNode.addMixin("mix:versionable");
83
84 session().save();
85
86 if (versioning)
87 fileNode.checkin();
88
89 if (log.isDebugEnabled())
90 log.debug("Created " + path);
91 } catch (Exception e) {
92 throw new ArgeoException("Cannot create node for " + path, e);
93 }
94
95 }
96
97 public void update(String path, Resource file) {
98 try {
99 update(path, file.getInputStream());
100 } catch (IOException e) {
101 throw new ArgeoException("Cannot read " + file, e);
102 }
103 }
104
105 public void update(String path, InputStream in) {
106 try {
107
108 if (!session().itemExists(path)) {
109 String type = new MimetypesFileTypeMap()
110 .getContentType(FilenameUtils.getName(path));
111 create(path, in, type);
112 return;
113 }
114
115 Node fileNode = (Node) session().getItem(path);
116 Node contentNode = fileNode.getNode("jcr:content");
117 fileNode.checkout();
118 contentNode.setProperty("jcr:data", in);
119 Calendar lastModified = Calendar.getInstance();
120 // lastModified.setTimeInMillis(file.lastModified());
121 contentNode.setProperty("jcr:lastModified", lastModified);
122
123 session().save();
124 fileNode.checkin();
125
126 if (log.isDebugEnabled())
127 log.debug("Updated " + path);
128 } catch (Exception e) {
129 throw new ArgeoException("Cannot update node " + path, e);
130 }
131 }
132
133 public List<Calendar> listVersions(String path) {
134 if (!versioning)
135 throw new ArgeoException("Versioning is not activated");
136
137 try {
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()) {
145 log.debug(version);
146 // debug(version);
147 }
148 }
149 return versions;
150 } catch (Exception e) {
151 throw new ArgeoException("Cannot list version of node " + path, e);
152 }
153 }
154
155 public InputStream retrieve(String path) {
156 try {
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);
162 }
163 }
164
165 public synchronized InputStream retrieve(String path, Integer revision) {
166 if (!versioning)
167 throw new ArgeoException("Versioning is not activated");
168
169 try {
170 Node fileNode = (Node) session().getItem(path);
171 VersionHistory history = fileNode.getVersionHistory();
172 int count = 0;
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 "
180 + revision);
181 return in;
182 }
183 count++;
184 }
185 } catch (Exception e) {
186 throw new ArgeoException("Cannot retrieve version " + revision
187 + " of " + path, e);
188 }
189
190 throw new ArgeoException("Version " + revision
191 + " does not exist for node " + path);
192 }
193
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();
199 return in;
200 }
201
202 protected Session session() {
203 return session;
204 }
205
206 public void setVersioning(Boolean versioning) {
207 this.versioning = versioning;
208 }
209
210 public void setDefaultEncoding(String defaultEncoding) {
211 this.defaultEncoding = defaultEncoding;
212 }
213
214 protected String fill(Integer number) {
215 int size = 4;
216 String str = number.toString();
217 for (int i = str.length(); i < size; i++) {
218 str = "0" + str;
219 }
220 return str;
221 }
222
223 public void setSession(Session session) {
224 this.session = session;
225 }
226
227 }