]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/proxy/AbstractUrlProxy.java
1 package org
.argeo
.jcr
.proxy
;
3 import java
.io
.InputStream
;
6 import javax
.jcr
.Binary
;
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
;
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
;
20 /** Base class for URL based proxys. */
21 public abstract class AbstractUrlProxy
implements ResourceProxy
{
22 private final static Log log
= LogFactory
.getLog(AbstractUrlProxy
.class);
24 private Repository jcrRepository
;
25 private Session jcrAdminSession
;
27 protected abstract Node
retrieve(Session session
, String relativePath
);
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
);
42 * Called before the (admin) session is saved at the end of the
43 * initialization. Does nothing by default, to be overridden.
45 protected void beforeInitSessionSave(Session session
)
46 throws RepositoryException
{
50 JcrUtils
.logoutQuietly(jcrAdminSession
);
54 * Called before the (admin) session is logged out when resources are
55 * released. Does nothing by default, to be overridden.
57 protected void beforeDestroySessionLogout() throws RepositoryException
{
60 public Node
proxy(Session session
, String path
) {
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
);
71 return session
.getNode(nodePath
);
72 } catch (RepositoryException e
) {
73 JcrUtils
.discardQuietly(jcrAdminSession
);
74 throw new ArgeoException("Cannot proxy " + path
, e
);
78 protected synchronized Node
retrieveAndSave(String path
) {
80 Node node
= retrieve(jcrAdminSession
, path
);
83 jcrAdminSession
.save();
85 } catch (RepositoryException e
) {
86 JcrUtils
.discardQuietly(jcrAdminSession
);
87 throw new ArgeoException("Cannot retrieve and save " + path
, e
);
91 /** Session is not saved */
92 protected Node
proxyUrl(Session session
, String baseUrl
, String path
) {
94 String remoteUrl
= baseUrl
+ path
;
95 InputStream in
= null;
97 URL u
= new URL(remoteUrl
);
99 node
= importFile(session
, getNodePath(path
), in
);
100 } catch (Exception e
) {
101 if (log
.isTraceEnabled()) {
102 log
.trace("Cannot read " + remoteUrl
+ ", skipping... "
104 // log.trace("Cannot read because of ", e);
106 JcrUtils
.discardQuietly(session
);
108 IOUtils
.closeQuietly(in
);
113 protected Node
importFile(Session session
, String nodePath
, InputStream in
)
114 throws RepositoryException
{
115 // FIXME allow parallel proxying
116 Binary binary
= null;
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
);
125 JcrUtils
.closeQuietly(binary
);
129 public void setJcrRepository(Repository jcrRepository
) {
130 this.jcrRepository
= jcrRepository
;