2 * Copyright 2004-2005 the original author or authors.
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
.codehaus
.groovy
.grails
.web
.servlet
.mvc
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
;
21 import org
.codehaus
.groovy
.grails
.web
.servlet
.GrailsUrlPathHelper
;
22 import org
.springframework
.context
.ApplicationContext
;
23 import org
.springframework
.web
.context
.ServletContextAware
;
24 import org
.springframework
.web
.context
.request
.RequestAttributes
;
25 import org
.springframework
.web
.context
.request
.RequestContextHolder
;
26 import org
.springframework
.web
.servlet
.ModelAndView
;
27 import org
.springframework
.web
.servlet
.mvc
.Controller
;
28 import org
.springframework
.web
.util
.UrlPathHelper
;
30 import javax
.servlet
.ServletContext
;
31 import javax
.servlet
.http
.HttpServletRequest
;
32 import javax
.servlet
.http
.HttpServletResponse
;
35 * <p>Base class for Grails controllers.
37 * @author Steven Devijver
40 public class SimpleGrailsController
implements Controller
, ServletContextAware
{
42 public static final String APPLICATION_CONTEXT_ID
= "simpleGrailsController";
44 private UrlPathHelper urlPathHelper
= new GrailsUrlPathHelper();
45 private GrailsApplication application
= null;
46 private ServletContext servletContext
;
48 private static final Log LOG
= LogFactory
.getLog(SimpleGrailsController
.class);
51 public SimpleGrailsController() {
56 public void setGrailsApplication(GrailsApplication application
) {
57 this.application
= application
;
61 * <p>This method wraps regular request and response objects into Grails request and response objects.
63 * <p>It can handle maps as model types next to ModelAndView instances.
65 * @param request HTTP request
66 * @param response HTTP response
69 public ModelAndView
handleRequest(HttpServletRequest request
,
70 HttpServletResponse response
) throws Exception
{
71 // Step 1: determine the correct URI of the request.
72 String uri
= this.urlPathHelper
.getPathWithinApplication(request
);
73 if(LOG
.isDebugEnabled()) {
74 LOG
.debug("[SimpleGrailsController] Processing request for uri ["+uri
+"]");
78 RequestAttributes ra
= RequestContextHolder
.getRequestAttributes();
80 if(!(ra
instanceof GrailsWebRequest
)) {
81 throw new IllegalStateException("Bound RequestContext is not an instance of GrailsWebRequest");
83 GrailsWebRequest webRequest
= (GrailsWebRequest
)ra
;
86 ApplicationContext context
= webRequest
.getAttributes().getApplicationContext();
87 SimpleGrailsControllerHelper helper
= new SimpleGrailsControllerHelper(this.application
,context
,this.servletContext
);
88 ModelAndView mv
= helper
.handleURI(uri
,webRequest
);
90 if(LOG
.isDebugEnabled()) {
92 LOG
.debug("[SimpleGrailsController] Forwarding model and view ["+mv
+"] with class ["+(mv
.getView() != null ? mv
.getView().getClass().getName() : mv
.getViewName())+"]");
98 public void setServletContext(ServletContext servletContext
) {
99 this.servletContext
= servletContext
;
102 public ServletContext
getServletContext() {
103 return this.servletContext
;