]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/proxy/AbstractUrlProxy.java
e088e1f0c4c6b88d3e9877f4f1b09bcfdc520415
2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
.proxy
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
22 import javax
.jcr
.Binary
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.Property
;
25 import javax
.jcr
.Repository
;
26 import javax
.jcr
.RepositoryException
;
27 import javax
.jcr
.Session
;
28 import javax
.jcr
.nodetype
.NodeType
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.apache
.commons
.logging
.Log
;
32 import org
.apache
.commons
.logging
.LogFactory
;
33 import org
.argeo
.ArgeoException
;
34 import org
.argeo
.jcr
.JcrUtils
;
36 /** Base class for URL based proxys. */
37 public abstract class AbstractUrlProxy
implements ResourceProxy
{
38 private final static Log log
= LogFactory
.getLog(AbstractUrlProxy
.class);
40 private Repository jcrRepository
;
41 private Session jcrAdminSession
;
43 protected abstract Node
retrieve(Session session
, String relativePath
);
47 jcrAdminSession
= jcrRepository
.login();
48 beforeInitSessionSave(jcrAdminSession
);
49 if (jcrAdminSession
.hasPendingChanges())
50 jcrAdminSession
.save();
51 } catch (Exception e
) {
52 JcrUtils
.discardQuietly(jcrAdminSession
);
53 throw new ArgeoException("Cannot initialize Maven proxy", e
);
58 * Called before the (admin) session is saved at the end of the
59 * initialization. Does nothing by default, to be overridden.
61 protected void beforeInitSessionSave(Session session
)
62 throws RepositoryException
{
66 JcrUtils
.logoutQuietly(jcrAdminSession
);
70 * Called before the (admin) session is logged out when resources are
71 * released. Does nothing by default, to be overridden.
73 protected void beforeDestroySessionLogout() throws RepositoryException
{
76 public Node
proxy(Session session
, String path
) {
78 if (session
.hasPendingChanges())
79 throw new ArgeoException(
80 "Cannot proxy based on a session with pending changes");
81 String nodePath
= getNodePath(path
);
82 if (!session
.itemExists(nodePath
)) {
83 Node nodeT
= retrieveAndSave(path
);
87 return session
.getNode(nodePath
);
88 } catch (RepositoryException e
) {
89 JcrUtils
.discardQuietly(jcrAdminSession
);
90 throw new ArgeoException("Cannot proxy " + path
, e
);
94 protected synchronized Node
retrieveAndSave(String path
) {
96 Node node
= retrieve(jcrAdminSession
, path
);
99 jcrAdminSession
.save();
101 } catch (RepositoryException e
) {
102 JcrUtils
.discardQuietly(jcrAdminSession
);
103 throw new ArgeoException("Cannot retrieve and save " + path
, e
);
107 /** Session is not saved */
108 protected Node
proxyUrl(Session session
, String baseUrl
, String path
)
109 throws RepositoryException
{
110 String nodePath
= getNodePath(path
);
111 if (jcrAdminSession
.itemExists(nodePath
))
112 throw new ArgeoException("Node " + nodePath
+ " already exists");
114 String remoteUrl
= baseUrl
+ path
;
115 InputStream in
= null;
117 URL u
= new URL(remoteUrl
);
119 node
= importFile(session
, nodePath
, in
);
120 } catch (IOException e
) {
121 if (log
.isTraceEnabled()) {
122 log
.trace("Cannot read " + remoteUrl
+ ", skipping... "
124 // log.trace("Cannot read because of ", e);
126 JcrUtils
.discardQuietly(session
);
128 IOUtils
.closeQuietly(in
);
133 protected Node
importFile(Session session
, String nodePath
, InputStream in
)
134 throws RepositoryException
{
135 // FIXME allow parallel proxying
136 Binary binary
= null;
138 Node node
= JcrUtils
.mkdirs(jcrAdminSession
, nodePath
,
139 NodeType
.NT_FILE
, NodeType
.NT_FOLDER
, false);
140 Node content
= node
.addNode(Node
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
141 binary
= session
.getValueFactory().createBinary(in
);
142 content
.setProperty(Property
.JCR_DATA
, binary
);
145 JcrUtils
.closeQuietly(binary
);
149 public void setJcrRepository(Repository jcrRepository
) {
150 this.jcrRepository
= jcrRepository
;