KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > dwr > DwrController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.dwr;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.directwebremoting.WebContextFactory.WebContextBuilder;
35 import org.directwebremoting.impl.ContainerUtil;
36 import org.directwebremoting.impl.StartupUtil;
37 import org.directwebremoting.servlet.UrlProcessor;
38 import org.directwebremoting.spring.SpringContainer;
39 import org.directwebremoting.util.FakeServletConfig;
40 import org.springframework.beans.BeansException;
41 import org.springframework.beans.factory.BeanFactory;
42 import org.springframework.beans.factory.BeanFactoryAware;
43 import org.springframework.beans.factory.BeanNameAware;
44 import org.springframework.beans.factory.InitializingBean;
45 import org.springframework.util.Assert;
46 import org.springframework.web.context.ServletContextAware;
47 import org.springframework.web.servlet.ModelAndView;
48 import org.springframework.web.servlet.mvc.AbstractController;
49
50 public class DwrController extends AbstractController
51         implements BeanNameAware, InitializingBean, BeanFactoryAware,
52         ServletContextAware {
53
54     private String JavaDoc beanName;
55
56     private String JavaDoc mapping;
57
58     private List JavaDoc configurators;
59
60     private boolean includeDefaultConfig = true;
61
62     private ServletConfig JavaDoc servletConfig;
63
64     private Map JavaDoc parameters;
65
66     private SpringContainer container;
67
68     protected WebContextBuilder webContextBuilder;
69
70     public void setMapping(String JavaDoc mapping) {
71         this.mapping = mapping;
72     }
73
74     public void setBeanName(String JavaDoc beanName) {
75         this.beanName = beanName;
76         if (mapping == null) {
77             mapping = beanName;
78         }
79     }
80
81     /**
82      * Is called by the Spring container to set the bean factory.
83      * This bean factory is then used to obtain the global DWR configuration
84      * from. This global configuration is optional as DWR will provide defaults
85      * where possible.
86      */

87     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
88         container = new SpringContainer();
89         container.setBeanFactory(beanFactory);
90     }
91
92     /**
93      * Sets the configurators to apply to this controller. The configurators
94      * are used to set up DWR correctly.
95      */

96     public void setConfigurators(List JavaDoc configurators) {
97         this.configurators = configurators;
98     }
99
100     /**
101      * Sets parameters just like the init-parameters of the DwrServlet.
102      */

103     public void setParameters(Map JavaDoc parameters) {
104         this.parameters = parameters;
105     }
106
107     /**
108      * Sets whether the default DWR configuration should be included
109      * (default is <code>true</code>).
110      * <p>
111      * This default configuration contains all build-in creators and converters.
112      * You normally want this default configuration to be included.
113      */

114     public void setIncludeDefaultConfig(boolean includeDefaultConfig) {
115         this.includeDefaultConfig = includeDefaultConfig;
116     }
117
118     /**
119      * Is called by the Spring container after all properties have been set.
120      * This method actually makes sure the container is correctly initialized
121      * and all configurators are processed.
122      *
123      * @throws Exception in case setting up fails
124      */

125     public void afterPropertiesSet() throws Exception JavaDoc {
126         ServletContext JavaDoc servletContext = getServletContext();
127         Assert.notNull(configurators, "The 'configurators' property must be set");
128         servletConfig = new FakeServletConfig(beanName, servletContext, parameters);
129
130         try {
131             ContainerUtil.setupDefaults(container, servletConfig);
132             ContainerUtil.setupFromServletConfig(container, servletConfig);
133             container.setupFinished();
134
135             webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
136             StartupUtil.initServerContext(servletConfig, servletContext, container);
137
138             if (includeDefaultConfig) {
139                 ContainerUtil.configureFromSystemDwrXml(container);
140             }
141
142             ContainerUtil.configure(container, configurators);
143             ContainerUtil.publishContainer(container, servletConfig);
144         }
145         finally {
146             webContextBuilder.unset();
147         }
148     }
149
150     /**
151      * Handles all request to this controller.
152      * <p>
153      * It delegates to the <code>UrlProcessor</code> and also takes care of
154      * setting and unsetting of the current <code>WebContext</code>.
155      */

156     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request,
157             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
158
159         HttpServletRequest JavaDoc shiftedRequest = new PathShiftingRequestWrapper(
160                 request, getPathOffset(request));
161
162         try {
163             // Set up the web context and delegate to the processor
164
webContextBuilder.set(shiftedRequest, response, servletConfig,
165                     getServletContext(), container);
166
167             UrlProcessor processor = (UrlProcessor)
168                     container.getBean(UrlProcessor.class.getName());
169
170             processor.handle(shiftedRequest, response);
171         }
172         finally {
173             webContextBuilder.unset();
174         }
175
176         return null;
177     }
178
179     protected int getPathOffset(HttpServletRequest JavaDoc request) {
180         String JavaDoc pathInfo = request.getPathInfo();
181         int i = pathInfo.indexOf(mapping);
182         return i + mapping.length();
183     }
184
185 }
186
Popular Tags