]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/MultipleRepositoryHandlerMapping.java
4095185dffa740020f125c39180c3ec8b98d797f
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr.mvc / src / main / java / org / argeo / jcr / mvc / MultipleRepositoryHandlerMapping.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.mvc;
17
18 import java.util.Enumeration;
19 import java.util.Properties;
20
21 import javax.jcr.Repository;
22 import javax.jcr.RepositoryFactory;
23 import javax.servlet.ServletConfig;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.argeo.jcr.ArgeoJcrConstants;
32 import org.argeo.jcr.ArgeoJcrUtils;
33 import org.springframework.beans.BeansException;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.context.ApplicationContextAware;
36 import org.springframework.context.ConfigurableApplicationContext;
37 import org.springframework.web.context.ServletContextAware;
38 import org.springframework.web.servlet.HandlerExecutionChain;
39 import org.springframework.web.servlet.HandlerMapping;
40
41 /** Handles multiple JCR servers with a single servlet. */
42 public abstract class MultipleRepositoryHandlerMapping implements
43 HandlerMapping, ApplicationContextAware, ServletContextAware {
44 private final static Log log = LogFactory
45 .getLog(MultipleRepositoryHandlerMapping.class);
46
47 // private final static String MKCOL = "MKCOL";
48
49 private ConfigurableApplicationContext applicationContext;
50 private ServletContext servletContext;
51
52 // private RepositoryRegister repositoryRegister;
53 private RepositoryFactory repositoryFactory;
54
55 /** Actually creates the servlet to be registered. */
56 protected abstract HttpServlet createServlet(Repository repository,
57 String pathPrefix) throws ServletException;
58
59 public HandlerExecutionChain getHandler(HttpServletRequest request)
60 throws Exception {
61 if (log.isTraceEnabled()) {
62 log.trace("getContextPath=" + request.getContextPath());
63 log.trace("getServletPath=" + request.getServletPath());
64 log.trace("getPathInfo=" + request.getPathInfo());
65 }
66
67 String pathInfo = request.getPathInfo();
68
69 // tokenize path
70 // List<String> tokens = JcrUtils.tokenize(pathInfo);
71 // String[] tokens = extractPrefix(pathInfo);
72
73 // check if repository can be found
74 // if (tokens[0] == null || (tokens[1] == null && tokens[0].equals("")))
75 // return null;
76
77 // MKCOL on repository or root node doesn't make sense
78 // if ((tokens.size() == 1 || tokens.size() == 2)
79 // && request.getMethod().equals(MKCOL))
80 // return null;
81
82 // String repositoryAlias = extractRepositoryName(tokens);
83 String repositoryAlias = extractRepositoryAlias(pathInfo);
84 if (repositoryAlias.equals(""))
85 return null;
86 request.setAttribute(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
87 repositoryAlias);
88 String pathPrefix = request.getServletPath() + '/' + repositoryAlias;
89 String beanName = pathPrefix;
90
91 if (!applicationContext.containsBean(beanName)) {
92 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
93 repositoryFactory, repositoryAlias);
94 // Repository repository = repositoryRegister.getRepositories().get(
95 // repositoryAlias);
96 HttpServlet servlet = createServlet(repository, pathPrefix);
97 applicationContext.getBeanFactory().registerSingleton(beanName,
98 servlet);
99 // TODO: unregister it as well
100 }
101 HttpServlet remotingServlet = (HttpServlet) applicationContext
102 .getBean(beanName);
103 HandlerExecutionChain hec = new HandlerExecutionChain(remotingServlet);
104 return hec;
105 }
106
107 /** Returns the first two token of the path */
108 // protected String[] extractPrefix(String pathInfo) {
109 // String[] res = new String[2];
110 // StringTokenizer st = new StringTokenizer(pathInfo, "/");
111 // if (st.hasMoreTokens())
112 // res[0] = st.nextToken();
113 // if (st.hasMoreTokens())
114 // res[1] = st.nextToken();
115 // return res;
116 // }
117
118 /** Returns the first token of the path */
119 protected String extractRepositoryAlias(String pathInfo) {
120 StringBuffer buf = new StringBuffer();
121 for (int i = 1; i < pathInfo.length(); i++) {
122 char c = pathInfo.charAt(i);
123 if (c == '/')
124 break;
125 buf.append(c);
126 }
127 return buf.toString();
128 }
129
130 /** The repository name is the first part of the path info */
131 // protected String extractRepositoryName(List<String> pathTokens) {
132 // StringBuffer currName = new StringBuffer("");
133 // for (String token : pathTokens) {
134 // currName.append(token);
135 // if (repositoryRegister.getRepositories().containsKey(
136 // currName.toString()))
137 // return currName.toString();
138 // currName.append('/');
139 // }
140 // throw new ArgeoException("No repository can be found for request "
141 // + pathTokens);
142 // }
143
144 public void setApplicationContext(ApplicationContext applicationContext)
145 throws BeansException {
146 this.applicationContext = (ConfigurableApplicationContext) applicationContext;
147 }
148
149 public void setServletContext(ServletContext servletContext) {
150 this.servletContext = servletContext;
151 }
152
153 // public void setRepositoryRegister(RepositoryRegister repositoryRegister)
154 // {
155 // this.repositoryRegister = repositoryRegister;
156 // }
157
158 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
159 this.repositoryFactory = repositoryFactory;
160 }
161
162 protected class DelegatingServletConfig implements ServletConfig {
163 private String name;
164 private Properties initParameters;
165
166 public DelegatingServletConfig(String name, Properties initParameters) {
167 super();
168 this.name = name;
169 this.initParameters = initParameters;
170 }
171
172 public String getServletName() {
173 return name;
174 }
175
176 public ServletContext getServletContext() {
177 return servletContext;
178 }
179
180 public String getInitParameter(String paramName) {
181 return initParameters.getProperty(paramName);
182 }
183
184 @SuppressWarnings("rawtypes")
185 public Enumeration getInitParameterNames() {
186 return initParameters.keys();
187 }
188 }
189 }