1 package org
.argeo
.jcr
.mvc
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Enumeration
;
6 import java
.util
.Properties
;
8 import javax
.jcr
.Repository
;
9 import javax
.servlet
.ServletConfig
;
10 import javax
.servlet
.ServletContext
;
11 import javax
.servlet
.ServletException
;
12 import javax
.servlet
.http
.HttpServlet
;
13 import javax
.servlet
.http
.HttpServletRequest
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.ArgeoException
;
18 import org
.argeo
.jcr
.RepositoryRegister
;
19 import org
.springframework
.beans
.BeansException
;
20 import org
.springframework
.context
.ApplicationContext
;
21 import org
.springframework
.context
.ApplicationContextAware
;
22 import org
.springframework
.context
.ConfigurableApplicationContext
;
23 import org
.springframework
.web
.context
.ServletContextAware
;
24 import org
.springframework
.web
.servlet
.HandlerExecutionChain
;
25 import org
.springframework
.web
.servlet
.HandlerMapping
;
27 public abstract class MultipleRepositoryHandlerMapping
implements
28 HandlerMapping
, ApplicationContextAware
, ServletContextAware
{
29 private final static Log log
= LogFactory
30 .getLog(MultipleRepositoryHandlerMapping
.class);
32 private final static String MKCOL
= "MKCOL";
34 private ConfigurableApplicationContext applicationContext
;
35 private ServletContext servletContext
;
37 private RepositoryRegister repositoryRegister
;
39 /** Actually creates the servlet to be registered. */
40 protected abstract HttpServlet
createServlet(Repository repository
,
41 String pathPrefix
) throws ServletException
;
43 public HandlerExecutionChain
getHandler(HttpServletRequest request
)
45 if (log
.isTraceEnabled()) {
46 log
.trace("getContextPath=" + request
.getContextPath());
47 log
.trace("getServletPath=" + request
.getServletPath());
48 log
.trace("getPathInfo=" + request
.getPathInfo());
51 String pathInfo
= request
.getPathInfo();
55 List
<String
> tokens
= new ArrayList
<String
>();
56 StringBuffer curr
= new StringBuffer();
57 char[] arr
= pathInfo
.toCharArray();
58 chars
: for (int i
= 0; i
< arr
.length
; i
++) {
61 if (i
== 0 || (i
== arr
.length
- 1))
63 if (curr
.length() > 0) {
64 tokens
.add(curr
.toString());
65 curr
= new StringBuffer();
70 if (curr
.length() > 0) {
71 tokens
.add(curr
.toString());
72 curr
= new StringBuffer();
75 // check if repository can be found
76 if (tokens
.size() == 0
77 || (tokens
.size() == 1 && tokens
.get(0).equals("")))
79 // MKCOL on repository or root node doesn't make sense
80 if ((tokens
.size() == 1 || tokens
.size() == 2)
81 && request
.getMethod().equals(MKCOL
))
83 String repositoryName
= extractRepositoryName(tokens
);
84 String pathPrefix
= request
.getServletPath() + '/' + repositoryName
;
85 String beanName
= pathPrefix
;
87 if (!applicationContext
.containsBean(beanName
)) {
88 Repository repository
= repositoryRegister
.getRepositories().get(
90 HttpServlet servlet
= createServlet(repository
, pathPrefix
);
91 applicationContext
.getBeanFactory().registerSingleton(beanName
,
93 // TODO: unregister it as well
95 HttpServlet remotingServlet
= (HttpServlet
) applicationContext
97 HandlerExecutionChain hec
= new HandlerExecutionChain(remotingServlet
);
101 /** The repository name is the first part of the path info */
102 protected String
extractRepositoryName(List
<String
> pathTokens
) {
103 StringBuffer currName
= new StringBuffer("");
104 for (String token
: pathTokens
) {
105 currName
.append(token
);
106 if (repositoryRegister
.getRepositories().containsKey(
107 currName
.toString()))
108 return currName
.toString();
109 currName
.append('/');
111 throw new ArgeoException("No repository can be found for request "
115 public void setApplicationContext(ApplicationContext applicationContext
)
116 throws BeansException
{
117 this.applicationContext
= (ConfigurableApplicationContext
) applicationContext
;
120 public void setServletContext(ServletContext servletContext
) {
121 this.servletContext
= servletContext
;
124 public void setRepositoryRegister(RepositoryRegister repositoryRegister
) {
125 this.repositoryRegister
= repositoryRegister
;
128 protected class DelegatingServletConfig
implements ServletConfig
{
130 private Properties initParameters
;
132 public DelegatingServletConfig(String name
, Properties initParameters
) {
135 this.initParameters
= initParameters
;
138 public String
getServletName() {
142 public ServletContext
getServletContext() {
143 return servletContext
;
146 public String
getInitParameter(String paramName
) {
147 return initParameters
.getProperty(paramName
);
150 @SuppressWarnings("rawtypes")
151 public Enumeration
getInitParameterNames() {
152 return initParameters
.keys();