]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractUrlProxy.java
7bec5ee7a264c27653cb68c921d3294a3ad328e5
[lgpl/argeo-commons.git] / 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 String retrieve(String relativePath);
28
29 void init() {
30 try {
31 jcrAdminSession = jcrRepository.login();
32 beforeInitSessionSave();
33 if (jcrAdminSession.hasPendingChanges())
34 jcrAdminSession.save();
35 } catch (RepositoryException 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() 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
54 * released. Does nothing by default, to be overridden.
55 */
56 protected void beforeDestroySessionLogout() throws RepositoryException {
57 }
58
59 public Node proxy(Session jcrSession, String path) {
60 Node node;
61 try {
62 String nodePath = getNodePath(path);
63 if (!jcrSession.itemExists(nodePath)) {
64 String nodeIdentifier = retrieve(path);
65 if (nodeIdentifier == null) {
66 // log.warn("Could not proxy " + path);
67 return null;
68 } else {
69 node = jcrSession.getNodeByIdentifier(nodeIdentifier);
70 }
71 } else {
72 node = jcrSession.getNode(nodePath);
73 }
74 } catch (RepositoryException e) {
75 throw new ArgeoException("Cannot proxy " + path, e);
76 }
77 return node;
78 }
79
80 protected Node proxyUrl(String baseUrl, String path) {
81 Node node = null;
82 String remoteUrl = baseUrl + path;
83 if (log.isTraceEnabled())
84 log.trace("baseUrl=" + remoteUrl);
85 InputStream in = null;
86 try {
87 URL u = new URL(remoteUrl);
88 in = u.openStream();
89 node = importFile(getNodePath(path), in);
90 if (log.isDebugEnabled())
91 log.debug("Imported " + remoteUrl + " to " + node);
92 } catch (Exception e) {
93 if (log.isTraceEnabled())
94 log.trace("Cannot read " + remoteUrl + ", skipping... "
95 + e.getMessage());
96 if (log.isTraceEnabled()) {
97 log.trace("Cannot read because of ", e);
98 }
99 } finally {
100 IOUtils.closeQuietly(in);
101 }
102
103 return node;
104 }
105
106 protected synchronized Node importFile(String nodePath, InputStream in) {
107 // FIXME allow parallel proxying
108 Binary binary = null;
109 try {
110 Node node = JcrUtils.mkdirs(jcrAdminSession, nodePath,
111 NodeType.NT_FILE, NodeType.NT_FOLDER, false);
112 Node content = node.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
113 binary = jcrAdminSession.getValueFactory().createBinary(in);
114 content.setProperty(Property.JCR_DATA, binary);
115 jcrAdminSession.save();
116 return node;
117 } catch (RepositoryException e) {
118 JcrUtils.discardQuietly(jcrAdminSession);
119 throw new ArgeoException("Cannot initialize Maven proxy", e);
120 } finally {
121 JcrUtils.closeQuietly(binary);
122 }
123 }
124
125 protected Session getJcrAdminSession() {
126 return jcrAdminSession;
127 }
128
129 public void setJcrRepository(Repository jcrRepository) {
130 this.jcrRepository = jcrRepository;
131 }
132
133 }