GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / groovy / org / codehaus / groovy / grails / plugins / web / filters / FilterToHandlerAdapter.groovy
blobdcb1eeeba77abb5aee8a8b51dfbdf86dbd802da5
1 /*
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.plugins.web.filters
18 import java.util.regex.Pattern
19 import javax.servlet.http.HttpServletRequest
20 import javax.servlet.http.HttpServletResponse
21 import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
22 import org.codehaus.groovy.grails.web.util.WebUtils
23 import org.springframework.web.servlet.HandlerInterceptor
24 import org.springframework.web.servlet.ModelAndView
25 import org.springframework.web.util.UrlPathHelper
26 import org.springframework.util.AntPathMatcher
27 import org.codehaus.groovy.grails.web.servlet.view.NullView
29 /**
30 * Adapter between a FilterConfig object and a Spring HandlerInterceptor.
31 * @author mike
32 * @author Graeme Rocher
34 class FilterToHandlerAdapter implements HandlerInterceptor {
35 def filterConfig;
36 def configClass;
38 def controllerRegex;
39 def actionRegex;
40 def uriPattern;
41 def urlPathHelper = new UrlPathHelper()
43 /**
44 * Returns the name of the controller targeted by the given request.
46 String controllerName(request) {
47 return request.getAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE).toString()
50 /**
51 * Returns the name of the action targeted by the given request.
53 String actionName(request) {
54 return request.getAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE).toString()
57 String uri(HttpServletRequest request) {
58 def uri = request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)
59 if(!uri) uri = request.getRequestURI()
60 return uri.substring(request.getContextPath().length())
63 boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
64 if (filterConfig.before) {
66 String controllerName = controllerName(request)
67 String actionName = actionName(request)
68 String uri = uri(request)
70 if (!accept(controllerName, actionName, uri)) return true;
72 def callable = filterConfig.before.clone()
73 def result = callable.call();
74 if(result instanceof Boolean) {
75 if(!result && filterConfig.modelAndView) {
76 renderModelAndView(filterConfig, request, response, controllerName)
78 return result
82 return true;
85 void postHandle(HttpServletRequest request, HttpServletResponse response, o, ModelAndView modelAndView) throws java.lang.Exception {
86 if (filterConfig.after) {
88 String controllerName = controllerName(request)
89 String actionName = actionName(request)
90 String uri = uri(request)
92 if (!accept(controllerName, actionName, uri)) return;
94 def callable = filterConfig.after.clone()
95 def result = callable.call(modelAndView?.model);
96 if(result instanceof Boolean) {
97 // if false is returned don't render a view
98 if(!result) {
99 modelAndView.viewName = null
100 modelAndView.view = new NullView(response.contentType)
103 else if(filterConfig.modelAndView && modelAndView) {
104 if(filterConfig.modelAndView.viewName) {
105 modelAndView.viewName = filterConfig.modelAndView.viewName
107 modelAndView.model.putAll(filterConfig.modelAndView.model)
109 else if(filterConfig.modelAndView?.viewName) {
110 renderModelAndView(filterConfig, request, response, controllerName)
116 private renderModelAndView(delegate, request, response, controllerName) {
117 def viewResolver = WebUtils.lookupViewResolver(delegate.servletContext)
118 def view
119 ModelAndView modelAndView = delegate.modelAndView
120 if (modelAndView.viewName)
121 view = WebUtils.resolveView(request, modelAndView.viewName, controllerName, viewResolver)
122 else if (modelAndView.view)
123 view = modelAndView.view
124 view?.render(modelAndView.model, request, response)
127 void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws java.lang.Exception {
128 if (filterConfig.afterView) {
130 String controllerName = controllerName(request)
131 String actionName = actionName(request)
132 String uri = uri(request)
134 if (!accept(controllerName, actionName, uri)) return;
135 def callable = filterConfig.afterView.clone()
136 callable.call(e);
140 def pathMatcher = new AntPathMatcher()
141 boolean accept(String controllerName, String actionName, String uri) {
142 if (controllerRegex == null || actionRegex == null) {
143 def scope = filterConfig.scope
145 if (scope.controller) {
146 controllerRegex = Pattern.compile(scope.controller.replaceAll("\\*", ".*"))
148 else {
149 controllerRegex = Pattern.compile(".*")
152 if (scope.action) {
153 actionRegex = Pattern.compile(scope.action.replaceAll("\\*", ".*"))
155 else {
156 actionRegex = Pattern.compile(".*")
159 if (scope.uri) {
160 uriPattern = scope.uri.toString()
164 if(uriPattern) {
165 return pathMatcher.match(uriPattern, uri)
167 else if(controllerRegex && actionRegex) {
168 return controllerRegex.matcher(controllerName).matches() && actionRegex.matcher(actionName).matches()
172 String toString() {
173 return "FilterToHandlerAdapter[$filterConfig, $configClass]"