]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/proxy/AbstractUrlProxy.java
0984276ddbb90c810d824fde99e3082e81560c17
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
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.jcr
.JcrException
;
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
;
26 private String proxyWorkspace
= "proxy";
28 protected abstract Node
retrieve(Session session
, String path
);
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
);
43 * Called before the (admin) session is saved at the end of the initialization.
44 * Does nothing by default, to be overridden.
46 protected void beforeInitSessionSave(Session session
) throws RepositoryException
{
50 JcrUtils
.logoutQuietly(jcrAdminSession
);
54 * Called before the (admin) session is logged out when resources are released.
55 * Does nothing by default, to be overridden.
57 protected void beforeDestroySessionLogout() throws RepositoryException
{
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;
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
);
73 nodeClient
= clientSession
.getNode(path
);
75 } catch (RepositoryException e
) {
76 throw new JcrException("Cannot proxy " + path
, e
);
78 if (nodeClient
== null)
79 JcrUtils
.logoutQuietly(clientSession
);
83 protected synchronized Node
retrieveAndSave(String path
) {
85 Node node
= retrieve(jcrAdminSession
, path
);
88 jcrAdminSession
.save();
90 } catch (RepositoryException e
) {
91 JcrUtils
.discardQuietly(jcrAdminSession
);
92 throw new JcrException("Cannot retrieve and save " + path
, e
);
98 /** Session is not saved */
99 protected synchronized Node
proxyUrl(Session session
, String remoteUrl
, String path
) throws RepositoryException
{
101 if (session
.itemExists(path
)) {
102 // throw new ArgeoJcrException("Node " + path + " already exists");
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);
113 JcrUtils
.discardQuietly(session
);
115 // IOUtils.closeQuietly(in);
120 protected synchronized Node
importFile(Session session
, String path
, InputStream in
) throws RepositoryException
{
121 Binary binary
= 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
);
129 node
= session
.getNode(path
);
130 content
= node
.getNode(Node
.JCR_CONTENT
);
132 binary
= session
.getValueFactory().createBinary(in
);
133 content
.setProperty(Property
.JCR_DATA
, binary
);
134 JcrUtils
.updateLastModifiedAndParents(node
, null, true);
137 JcrUtils
.closeQuietly(binary
);
141 /** Whether the file should be updated. */
142 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
146 public void setJcrRepository(Repository jcrRepository
) {
147 this.jcrRepository
= jcrRepository
;
150 public void setProxyWorkspace(String localWorkspace
) {
151 this.proxyWorkspace
= localWorkspace
;