]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/jcr/proxy/AbstractUrlProxy.java
1 package org
.argeo
.jcr
.proxy
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
7 import javax
.jcr
.Binary
;
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
;
15 import org
.argeo
.api
.cms
.CmsLog
;
16 import org
.argeo
.jcr
.JcrException
;
17 import org
.argeo
.jcr
.JcrUtils
;
19 /** Base class for URL based proxys. */
20 public abstract class AbstractUrlProxy
implements ResourceProxy
{
21 private final static CmsLog log
= CmsLog
.getLog(AbstractUrlProxy
.class);
23 private Repository jcrRepository
;
24 private Session jcrAdminSession
;
25 private String proxyWorkspace
= "proxy";
27 protected abstract Node
retrieve(Session session
, String path
);
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
);
42 * Called before the (admin) session is saved at the end of the initialization.
43 * Does nothing by default, to be overridden.
45 protected void beforeInitSessionSave(Session session
) throws RepositoryException
{
49 JcrUtils
.logoutQuietly(jcrAdminSession
);
53 * Called before the (admin) session is logged out when resources are released.
54 * Does nothing by default, to be overridden.
56 protected void beforeDestroySessionLogout() throws RepositoryException
{
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;
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
);
72 nodeClient
= clientSession
.getNode(path
);
74 } catch (RepositoryException e
) {
75 throw new JcrException("Cannot proxy " + path
, e
);
77 if (nodeClient
== null)
78 JcrUtils
.logoutQuietly(clientSession
);
82 protected synchronized Node
retrieveAndSave(String path
) {
84 Node node
= retrieve(jcrAdminSession
, path
);
87 jcrAdminSession
.save();
89 } catch (RepositoryException e
) {
90 JcrUtils
.discardQuietly(jcrAdminSession
);
91 throw new JcrException("Cannot retrieve and save " + path
, e
);
97 /** Session is not saved */
98 protected synchronized Node
proxyUrl(Session session
, String remoteUrl
, String path
) throws RepositoryException
{
100 if (session
.itemExists(path
)) {
101 // throw new ArgeoJcrException("Node " + path + " already exists");
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);
112 JcrUtils
.discardQuietly(session
);
114 // IOUtils.closeQuietly(in);
119 protected synchronized Node
importFile(Session session
, String path
, InputStream in
) throws RepositoryException
{
120 Binary binary
= 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
);
128 node
= session
.getNode(path
);
129 content
= node
.getNode(Node
.JCR_CONTENT
);
131 binary
= session
.getValueFactory().createBinary(in
);
132 content
.setProperty(Property
.JCR_DATA
, binary
);
133 JcrUtils
.updateLastModifiedAndParents(node
, null, true);
136 JcrUtils
.closeQuietly(binary
);
140 /** Whether the file should be updated. */
141 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
145 public void setJcrRepository(Repository jcrRepository
) {
146 this.jcrRepository
= jcrRepository
;
149 public void setProxyWorkspace(String localWorkspace
) {
150 this.proxyWorkspace
= localWorkspace
;