]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/proxy/AbstractUrlProxy.java
Fix small bugs
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / proxy / AbstractUrlProxy.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.jcr.proxy;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21
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;
29
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;
35
36 /** Base class for URL based proxys. */
37 public abstract class AbstractUrlProxy implements ResourceProxy {
38 private final static Log log = LogFactory.getLog(AbstractUrlProxy.class);
39
40 private Repository jcrRepository;
41 private Session jcrAdminSession;
42
43 protected abstract Node retrieve(Session session, String relativePath);
44
45 void init() {
46 try {
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);
54 }
55 }
56
57 /**
58 * Called before the (admin) session is saved at the end of the
59 * initialization. Does nothing by default, to be overridden.
60 */
61 protected void beforeInitSessionSave(Session session)
62 throws RepositoryException {
63 }
64
65 void destroy() {
66 JcrUtils.logoutQuietly(jcrAdminSession);
67 }
68
69 /**
70 * Called before the (admin) session is logged out when resources are
71 * released. Does nothing by default, to be overridden.
72 */
73 protected void beforeDestroySessionLogout() throws RepositoryException {
74 }
75
76 public Node proxy(Session session, String path) {
77 try {
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);
84 if (nodeT == null)
85 return null;
86 }
87 return session.getNode(nodePath);
88 } catch (RepositoryException e) {
89 JcrUtils.discardQuietly(jcrAdminSession);
90 throw new ArgeoException("Cannot proxy " + path, e);
91 }
92 }
93
94 protected synchronized Node retrieveAndSave(String path) {
95 try {
96 Node node = retrieve(jcrAdminSession, path);
97 if (node == null)
98 return null;
99 jcrAdminSession.save();
100 return node;
101 } catch (RepositoryException e) {
102 JcrUtils.discardQuietly(jcrAdminSession);
103 throw new ArgeoException("Cannot retrieve and save " + path, e);
104 }
105 }
106
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");
113 Node node = null;
114 String remoteUrl = baseUrl + path;
115 InputStream in = null;
116 try {
117 URL u = new URL(remoteUrl);
118 in = u.openStream();
119 node = importFile(session, nodePath, in);
120 } catch (IOException e) {
121 if (log.isTraceEnabled()) {
122 log.trace("Cannot read " + remoteUrl + ", skipping... "
123 + e.getMessage());
124 // log.trace("Cannot read because of ", e);
125 }
126 JcrUtils.discardQuietly(session);
127 } finally {
128 IOUtils.closeQuietly(in);
129 }
130 return node;
131 }
132
133 protected Node importFile(Session session, String nodePath, InputStream in)
134 throws RepositoryException {
135 // FIXME allow parallel proxying
136 Binary binary = null;
137 try {
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);
143 return node;
144 } finally {
145 JcrUtils.closeQuietly(binary);
146 }
147 }
148
149 public void setJcrRepository(Repository jcrRepository) {
150 this.jcrRepository = jcrRepository;
151 }
152
153 }