1 /* Copyright 2004-2005 the original author or authors.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org
.codehaus
.groovy
.grails
.web
.servlet
;
17 import groovy
.lang
.GroovyObject
;
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
.commons
.GrailsTagLibClass
;
22 import org
.codehaus
.groovy
.grails
.commons
.TagLibArtefactHandler
;
23 import org
.codehaus
.groovy
.grails
.web
.metaclass
.ControllerDynamicMethods
;
24 import org
.codehaus
.groovy
.grails
.web
.pages
.GroovyPage
;
25 import org
.codehaus
.groovy
.grails
.web
.pages
.GroovyPagesTemplateEngine
;
26 import org
.codehaus
.groovy
.grails
.web
.pages
.exceptions
.GroovyPagesException
;
27 import org
.codehaus
.groovy
.grails
.plugins
.PluginMetaManager
;
28 import org
.springframework
.context
.ApplicationContext
;
29 import org
.springframework
.validation
.Errors
;
30 import org
.springframework
.web
.util
.UrlPathHelper
;
32 import javax
.servlet
.ServletContext
;
33 import javax
.servlet
.ServletRequest
;
34 import javax
.servlet
.http
.HttpServletRequest
;
35 import javax
.servlet
.http
.HttpServletResponse
;
36 import javax
.servlet
.http
.HttpSession
;
37 import java
.io
.Writer
;
40 * Implementation of the GrailsApplicationAttributes interface that holds knowledge about how to obtain
41 * certain attributes from either the ServletContext or the HttpServletRequest instance.
43 * @see org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
45 * @author Graeme Rocher
48 * Created: 17-Jan-2006
50 public class DefaultGrailsApplicationAttributes
implements GrailsApplicationAttributes
{
52 private static Log LOG
= LogFactory
.getLog(DefaultGrailsApplicationAttributes
.class);
54 private UrlPathHelper urlHelper
= new UrlPathHelper();
56 private ServletContext context
;
58 public DefaultGrailsApplicationAttributes(ServletContext context
) {
59 this.context
= context
;
62 public ApplicationContext
getApplicationContext() {
63 return (ApplicationContext
)this.context
.getAttribute(APPLICATION_CONTEXT
);
66 public String
getPluginContextPath(HttpServletRequest request
) {
67 GroovyObject controller
= getController(request
);
68 GrailsApplication application
= getGrailsApplication();
70 if(controller
!= null && application
!= null) {
71 final Class controllerClass
= controller
.getClass();
72 PluginMetaManager metaManager
= (PluginMetaManager
)getApplicationContext().getBean(PluginMetaManager
.BEAN_ID
);
73 String path
= metaManager
.getPluginPathForResource(controllerClass
.getName());
74 return path
== null ?
"" : path
;
81 public GroovyObject
getController(ServletRequest request
) {
82 return (GroovyObject
)request
.getAttribute(CONTROLLER
);
85 public String
getControllerUri(ServletRequest request
) {
86 GroovyObject controller
= getController(request
);
87 if(controller
!= null)
88 return (String
)controller
.getProperty(ControllerDynamicMethods
.CONTROLLER_URI_PROPERTY
);
90 return "/" + request
.getAttribute(GrailsApplicationAttributes
.CONTROLLER_NAME_ATTRIBUTE
);
94 public String
getApplicationUri(ServletRequest request
) {
95 return this.urlHelper
.getContextPath((HttpServletRequest
)request
);
98 public ServletContext
getServletContext() {
102 public FlashScope
getFlashScope(ServletRequest request
) {
103 if(request
instanceof HttpServletRequest
) {
104 HttpServletRequest servletRequest
= (HttpServletRequest
) request
;
105 HttpSession session
= servletRequest
.getSession(false);
107 if(session
!= null) {
108 fs
= (FlashScope
)session
.getAttribute(FLASH_SCOPE
);
111 fs
= (FlashScope
)request
.getAttribute(FLASH_SCOPE
);
114 fs
= new GrailsFlashScope();
116 session
.setAttribute(FLASH_SCOPE
,fs
);
119 request
.setAttribute(FLASH_SCOPE
,fs
);
127 public String
getTemplateUri(String templateName
, ServletRequest request
) {
128 StringBuffer buf
= new StringBuffer();
130 if(templateName
.startsWith("/")) {
131 String tmp
= templateName
.substring(1,templateName
.length());
132 if(tmp
.indexOf('/') > -1) {
134 int i
= tmp
.lastIndexOf('/');
135 buf
.append(tmp
.substring(0, i
));
137 buf
.append(tmp
.substring(i
+ 1,tmp
.length()));
141 buf
.append(templateName
.substring(1,templateName
.length()));
145 String pathToTemplate
= "";
147 int lastSlash
= templateName
.lastIndexOf('/');
148 if (lastSlash
> -1) {
149 pathToTemplate
= templateName
.substring(0, lastSlash
+ 1);
150 templateName
= templateName
.substring(lastSlash
+ 1);
152 buf
.append(getControllerUri(request
))
154 .append(pathToTemplate
)
156 .append(templateName
);
163 public String
getControllerActionUri(ServletRequest request
) {
164 GroovyObject controller
= getController(request
);
166 return (String
)controller
.getProperty(ControllerDynamicMethods
.ACTION_URI_PROPERTY
);
169 public Errors
getErrors(ServletRequest request
) {
170 return (Errors
)request
.getAttribute(ERRORS
);
173 public GroovyPagesTemplateEngine
getPagesTemplateEngine() {
174 ApplicationContext appCtx
= getApplicationContext();
175 if(appCtx
.containsBean(GroovyPagesTemplateEngine
.BEAN_ID
)) {
176 return (GroovyPagesTemplateEngine
)appCtx
.getBean(GroovyPagesTemplateEngine
.BEAN_ID
);
179 throw new GroovyPagesException("No bean named ["+GroovyPagesTemplateEngine
.BEAN_ID
+"] defined in Spring application context!");
183 public GrailsApplication
getGrailsApplication() {
184 return (GrailsApplication
)getApplicationContext()
185 .getBean(GrailsApplication
.APPLICATION_ID
);
189 public GroovyObject
getTagLibraryForTag(HttpServletRequest request
, HttpServletResponse response
,String tagName
) {
190 return getTagLibraryForTag(request
, response
, tagName
, GroovyPage
.DEFAULT_NAMESPACE
);
193 public GroovyObject
getTagLibraryForTag(HttpServletRequest request
, HttpServletResponse response
,String tagName
, String namespace
) {
194 String nonNullNamesapce
= namespace
== null ? GroovyPage
.DEFAULT_NAMESPACE
: namespace
;
195 String fullTagName
= nonNullNamesapce
+ ":" + tagName
;
197 GrailsTagLibClass tagLibClass
= (GrailsTagLibClass
) getGrailsApplication().getArtefactForFeature(
198 TagLibArtefactHandler
.TYPE
, fullTagName
);
199 if(tagLibClass
== null)return null;
200 return (GroovyObject
)getApplicationContext()
201 .getBean(tagLibClass
.getFullName());
204 public String
getViewUri(String viewName
, HttpServletRequest request
) {
205 StringBuffer buf
= new StringBuffer(PATH_TO_VIEWS
);
207 if(viewName
.startsWith("/")) {
208 String tmp
= viewName
.substring(1,viewName
.length());
209 if(tmp
.indexOf('/') > -1) {
211 buf
.append(tmp
.substring(0,tmp
.lastIndexOf('/')));
213 buf
.append(tmp
.substring(tmp
.lastIndexOf('/') + 1,tmp
.length()));
217 buf
.append(viewName
.substring(1,viewName
.length()));
221 buf
.append(getControllerUri(request
))
232 public Writer
getOut(HttpServletRequest request
) {
233 return (Writer
)request
.getAttribute(OUT
);
236 public void setOut(HttpServletRequest request
, Writer out2
) {
237 request
.setAttribute(OUT
, out2
);