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