]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/proxy/AbstractUrlProxy.java
First working remote node
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / proxy / AbstractUrlProxy.java
1 package org.argeo.jcr.proxy;
2
3 import java.io.InputStream;
4 import java.net.URL;
5
6 import javax.jcr.Binary;
7 import javax.jcr.Node;
8 import javax.jcr.Property;
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.nodetype.NodeType;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.ArgeoException;
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
27 protected abstract Node retrieve(Session session, String relativePath);
28
29 void init() {
30 try {
31 jcrAdminSession = jcrRepository.login();
32 beforeInitSessionSave(jcrAdminSession);
33 if (jcrAdminSession.hasPendingChanges())
34 jcrAdminSession.save();
35 } catch (Exception e) {
36 JcrUtils.discardQuietly(jcrAdminSession);
37 throw new ArgeoException("Cannot initialize Maven proxy", e);
38 }
39 }
40
41 /**
42 * Called before the (admin) session is saved at the end of the
43 * initialization. Does nothing by default, to be overridden.
44 */
45 protected void beforeInitSessionSave(Session session)
46 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
55 * released. Does nothing by default, to be overridden.
56 */
57 protected void beforeDestroySessionLogout() throws RepositoryException {
58 }
59
60 public Node proxy(Session session, String path) {
61 try {
62 if (session.hasPendingChanges())
63 throw new ArgeoException(
64 "Cannot proxy based on a session with pending changes");
65 String nodePath = getNodePath(path);
66 if (!session.itemExists(nodePath)) {
67 Node nodeT = retrieveAndSave(path);
68 if (nodeT == null)
69 return null;
70 }
71 return session.getNode(nodePath);
72 } catch (RepositoryException e) {
73 JcrUtils.discardQuietly(jcrAdminSession);
74 throw new ArgeoException("Cannot proxy " + path, e);
75 }
76 }
77
78 protected synchronized Node retrieveAndSave(String path) {
79 try {
80 Node node = retrieve(jcrAdminSession, path);
81 if (node == null)
82 return null;
83 jcrAdminSession.save();
84 return node;
85 } catch (RepositoryException e) {
86 JcrUtils.discardQuietly(jcrAdminSession);
87 throw new ArgeoException("Cannot retrieve and save " + path, e);
88 }
89 }
90
91 /** Session is not saved */
92 protected Node proxyUrl(Session session, String baseUrl, String path) {
93 Node node = null;
94 String remoteUrl = baseUrl + path;
95 InputStream in = null;
96 try {
97 URL u = new URL(remoteUrl);
98 in = u.openStream();
99 node = importFile(session, getNodePath(path), in);
100 } catch (Exception e) {
101 if (log.isTraceEnabled()) {
102 log.trace("Cannot read " + remoteUrl + ", skipping... "
103 + e.getMessage());
104 // log.trace("Cannot read because of ", e);
105 }
106 JcrUtils.discardQuietly(session);
107 } finally {
108 IOUtils.closeQuietly(in);
109 }
110 return node;
111 }
112
113 protected Node importFile(Session session, String nodePath, InputStream in)
114 throws RepositoryException {
115 // FIXME allow parallel proxying
116 Binary binary = null;
117 try {
118 Node node = JcrUtils.mkdirs(jcrAdminSession, nodePath,
119 NodeType.NT_FILE, NodeType.NT_FOLDER, false);
120 Node content = node.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
121 binary = session.getValueFactory().createBinary(in);
122 content.setProperty(Property.JCR_DATA, binary);
123 return node;
124 } finally {
125 JcrUtils.closeQuietly(binary);
126 }
127 }
128
129 public void setJcrRepository(Repository jcrRepository) {
130 this.jcrRepository = jcrRepository;
131 }
132
133 }