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
.mvc
;
18 import java
.util
.Enumeration
;
19 import java
.util
.Properties
;
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
;
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
;
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);
47 // private final static String MKCOL = "MKCOL";
49 private ConfigurableApplicationContext applicationContext
;
50 private ServletContext servletContext
;
52 // private RepositoryRegister repositoryRegister;
53 private RepositoryFactory repositoryFactory
;
55 /** Actually creates the servlet to be registered. */
56 protected abstract HttpServlet
createServlet(Repository repository
,
57 String pathPrefix
) throws ServletException
;
59 public HandlerExecutionChain
getHandler(HttpServletRequest request
)
61 if (log
.isTraceEnabled()) {
62 log
.trace("getContextPath=" + request
.getContextPath());
63 log
.trace("getServletPath=" + request
.getServletPath());
64 log
.trace("getPathInfo=" + request
.getPathInfo());
67 String pathInfo
= request
.getPathInfo();
70 // List<String> tokens = JcrUtils.tokenize(pathInfo);
71 // String[] tokens = extractPrefix(pathInfo);
73 // check if repository can be found
74 // if (tokens[0] == null || (tokens[1] == null && tokens[0].equals("")))
77 // MKCOL on repository or root node doesn't make sense
78 // if ((tokens.size() == 1 || tokens.size() == 2)
79 // && request.getMethod().equals(MKCOL))
82 // String repositoryAlias = extractRepositoryName(tokens);
83 String repositoryAlias
= extractRepositoryAlias(pathInfo
);
84 if (repositoryAlias
.equals(""))
86 request
.setAttribute(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
,
88 String pathPrefix
= request
.getServletPath() + '/' + repositoryAlias
;
89 String beanName
= pathPrefix
;
91 if (!applicationContext
.containsBean(beanName
)) {
92 Repository repository
= ArgeoJcrUtils
.getRepositoryByAlias(
93 repositoryFactory
, repositoryAlias
);
94 // Repository repository = repositoryRegister.getRepositories().get(
96 HttpServlet servlet
= createServlet(repository
, pathPrefix
);
97 applicationContext
.getBeanFactory().registerSingleton(beanName
,
99 // TODO: unregister it as well
101 HttpServlet remotingServlet
= (HttpServlet
) applicationContext
103 HandlerExecutionChain hec
= new HandlerExecutionChain(remotingServlet
);
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();
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
);
127 return buf
.toString();
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('/');
140 // throw new ArgeoException("No repository can be found for request "
144 public void setApplicationContext(ApplicationContext applicationContext
)
145 throws BeansException
{
146 this.applicationContext
= (ConfigurableApplicationContext
) applicationContext
;
149 public void setServletContext(ServletContext servletContext
) {
150 this.servletContext
= servletContext
;
153 // public void setRepositoryRegister(RepositoryRegister repositoryRegister)
155 // this.repositoryRegister = repositoryRegister;
158 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
159 this.repositoryFactory
= repositoryFactory
;
162 protected class DelegatingServletConfig
implements ServletConfig
{
164 private Properties initParameters
;
166 public DelegatingServletConfig(String name
, Properties initParameters
) {
169 this.initParameters
= initParameters
;
172 public String
getServletName() {
176 public ServletContext
getServletContext() {
177 return servletContext
;
180 public String
getInitParameter(String paramName
) {
181 return initParameters
.getProperty(paramName
);
184 @SuppressWarnings("rawtypes")
185 public Enumeration
getInitParameterNames() {
186 return initParameters
.keys();