]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/proxy/AbstractUrlProxy.java
Move time UUID nodeid back to the factory
[lgpl/argeo-commons.git] / org.argeo.cms.jcr / src / org / argeo / jcr / proxy / AbstractUrlProxy.java
1 package org.argeo.jcr.proxy;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6
7 import javax.jcr.Binary;
8 import javax.jcr.Node;
9 import javax.jcr.Property;
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.Session;
13 import javax.jcr.nodetype.NodeType;
14
15 import org.argeo.api.cms.CmsLog;
16 import org.argeo.jcr.JcrException;
17 import org.argeo.jcr.JcrUtils;
18
19 /** Base class for URL based proxys. */
20 public abstract class AbstractUrlProxy implements ResourceProxy {
21 private final static CmsLog log = CmsLog.getLog(AbstractUrlProxy.class);
22
23 private Repository jcrRepository;
24 private Session jcrAdminSession;
25 private String proxyWorkspace = "proxy";
26
27 protected abstract Node retrieve(Session session, String path);
28
29 void init() {
30 try {
31 jcrAdminSession = JcrUtils.loginOrCreateWorkspace(jcrRepository, proxyWorkspace);
32 beforeInitSessionSave(jcrAdminSession);
33 if (jcrAdminSession.hasPendingChanges())
34 jcrAdminSession.save();
35 } catch (RepositoryException e) {
36 JcrUtils.discardQuietly(jcrAdminSession);
37 throw new JcrException("Cannot initialize URL proxy", e);
38 }
39 }
40
41 /**
42 * Called before the (admin) session is saved at the end of the initialization.
43 * Does nothing by default, to be overridden.
44 */
45 protected void beforeInitSessionSave(Session session) throws RepositoryException {
46 }
47
48 void destroy() {
49 JcrUtils.logoutQuietly(jcrAdminSession);
50 }
51
52 /**
53 * Called before the (admin) session is logged out when resources are released.
54 * Does nothing by default, to be overridden.
55 */
56 protected void beforeDestroySessionLogout() throws RepositoryException {
57 }
58
59 public Node proxy(String path) {
60 // we open a JCR session with client credentials in order not to use the
61 // admin session in multiple thread or make it a bottleneck.
62 Node nodeAdmin = null;
63 Node nodeClient = null;
64 Session clientSession = null;
65 try {
66 clientSession = jcrRepository.login(proxyWorkspace);
67 if (!clientSession.itemExists(path) || shouldUpdate(clientSession, path)) {
68 nodeAdmin = retrieveAndSave(path);
69 if (nodeAdmin != null)
70 nodeClient = clientSession.getNode(path);
71 } else
72 nodeClient = clientSession.getNode(path);
73 return nodeClient;
74 } catch (RepositoryException e) {
75 throw new JcrException("Cannot proxy " + path, e);
76 } finally {
77 if (nodeClient == null)
78 JcrUtils.logoutQuietly(clientSession);
79 }
80 }
81
82 protected synchronized Node retrieveAndSave(String path) {
83 try {
84 Node node = retrieve(jcrAdminSession, path);
85 if (node == null)
86 return null;
87 jcrAdminSession.save();
88 return node;
89 } catch (RepositoryException e) {
90 JcrUtils.discardQuietly(jcrAdminSession);
91 throw new JcrException("Cannot retrieve and save " + path, e);
92 } finally {
93 notifyAll();
94 }
95 }
96
97 /** Session is not saved */
98 protected synchronized Node proxyUrl(Session session, String remoteUrl, String path) throws RepositoryException {
99 Node node = null;
100 if (session.itemExists(path)) {
101 // throw new ArgeoJcrException("Node " + path + " already exists");
102 }
103 try (InputStream in = new URL(remoteUrl).openStream()) {
104 // URL u = new URL(remoteUrl);
105 // in = u.openStream();
106 node = importFile(session, path, in);
107 } catch (IOException e) {
108 if (log.isDebugEnabled()) {
109 log.debug("Cannot read " + remoteUrl + ", skipping... " + e.getMessage());
110 // log.trace("Cannot read because of ", e);
111 }
112 JcrUtils.discardQuietly(session);
113 // } finally {
114 // IOUtils.closeQuietly(in);
115 }
116 return node;
117 }
118
119 protected synchronized Node importFile(Session session, String path, InputStream in) throws RepositoryException {
120 Binary binary = null;
121 try {
122 Node content = null;
123 Node node = null;
124 if (!session.itemExists(path)) {
125 node = JcrUtils.mkdirs(session, path, NodeType.NT_FILE, NodeType.NT_FOLDER, false);
126 content = node.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
127 } else {
128 node = session.getNode(path);
129 content = node.getNode(Node.JCR_CONTENT);
130 }
131 binary = session.getValueFactory().createBinary(in);
132 content.setProperty(Property.JCR_DATA, binary);
133 JcrUtils.updateLastModifiedAndParents(node, null, true);
134 return node;
135 } finally {
136 JcrUtils.closeQuietly(binary);
137 }
138 }
139
140 /** Whether the file should be updated. */
141 protected Boolean shouldUpdate(Session clientSession, String nodePath) {
142 return false;
143 }
144
145 public void setJcrRepository(Repository jcrRepository) {
146 this.jcrRepository = jcrRepository;
147 }
148
149 public void setProxyWorkspace(String localWorkspace) {
150 this.proxyWorkspace = localWorkspace;
151 }
152
153 }