11/* Copyright 2004-2005 the original author or authors.
2- *
2+ *
33 * Licensed under the Apache License, Version 2.0 (the "License");
44 * you may not use this file except in compliance with the License.
55 * You may obtain a copy of the License at
6- *
6+ *
77 * http://www.apache.org/licenses/LICENSE-2.0
8- *
8+ *
99 * Unless required by applicable law or agreed to in writing, software
1010 * distributed under the License is distributed on an "AS IS" BASIS,
1111 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4343import org .springframework .web .util .UrlPathHelper ;
4444
4545/**
46- * Implementation of the GrailsApplicationAttributes interface that holds knowledge about how to obtain
47- * certain attributes from either the ServletContext or the HttpServletRequest instance.
46+ * Holds knowledge about how to obtain certain attributes from either the ServletContext
47+ * or the HttpServletRequest instance.
4848 *
4949 * @see org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
5050 *
5151 * @author Graeme Rocher
5252 * @since 0.3
53- *
54- * Created: 17-Jan-2006
5553 */
5654public class DefaultGrailsApplicationAttributes implements GrailsApplicationAttributes {
57-
58- private static Log LOG = LogFactory .getLog (DefaultGrailsApplicationAttributes .class );
55+
56+ private static Log LOG = LogFactory .getLog (DefaultGrailsApplicationAttributes .class );
5957
6058 private UrlPathHelper urlHelper = new UrlPathHelper ();
61-
59+
6260 private ServletContext context ;
6361 private ApplicationContext appContext ;
64-
62+
6563 // Beans used very often
6664 private GroovyPagesTemplateEngine pagesTemplateEngine ;
6765 private GrailsApplication grailsApplication ;
@@ -71,126 +69,127 @@ public class DefaultGrailsApplicationAttributes implements GrailsApplicationAttr
7169
7270 public DefaultGrailsApplicationAttributes (ServletContext context ) {
7371 this .context = context ;
74- if (context != null ) {
75- this . appContext = (ApplicationContext )context .getAttribute (APPLICATION_CONTEXT );
72+ if (context != null ) {
73+ appContext = (ApplicationContext )context .getAttribute (APPLICATION_CONTEXT );
7674 }
7775 initBeans ();
7876 }
7977
8078 public ApplicationContext getApplicationContext () {
8179 return appContext ;
8280 }
83-
81+
8482 private void initBeans () {
85- if (appContext != null ) {
86- this .pagesTemplateEngine =(GroovyPagesTemplateEngine )fetchBeanFromAppCtx (GroovyPagesTemplateEngine .BEAN_ID );
87- this .pluginManager =(GrailsPluginManager )fetchBeanFromAppCtx (GrailsPluginManager .BEAN_NAME );
88- this .grailsApplication =(GrailsApplication )fetchBeanFromAppCtx (GrailsApplication .APPLICATION_ID );
89- this .groovyPagesUriService = (GroovyPagesUriService )fetchBeanFromAppCtx (GroovyPagesUriService .BEAN_ID );
90- this .messageSource = (MessageSource )fetchBeanFromAppCtx ("messageSource" );
91- } else {
92- LOG .warn ("ApplicationContext not found in " + APPLICATION_CONTEXT + " attribute of servlet context." );
93- }
94- if (this .groovyPagesUriService ==null ) {
95- this .groovyPagesUriService = new DefaultGroovyPagesUriService ();
96- }
97- }
98-
99- private Object fetchBeanFromAppCtx (String name ) {
100- try {
101- return appContext .getBean (name );
102- } catch (BeansException e ) {
103- LOG .warn ("Bean named '" + name + "' is missing." );
104- return null ;
105- }
83+ if (appContext != null ) {
84+ pagesTemplateEngine = fetchBeanFromAppCtx (GroovyPagesTemplateEngine .BEAN_ID );
85+ pluginManager = fetchBeanFromAppCtx (GrailsPluginManager .BEAN_NAME );
86+ grailsApplication = fetchBeanFromAppCtx (GrailsApplication .APPLICATION_ID );
87+ groovyPagesUriService = fetchBeanFromAppCtx (GroovyPagesUriService .BEAN_ID );
88+ messageSource = fetchBeanFromAppCtx ("messageSource" );
89+ }
90+ else {
91+ LOG .warn ("ApplicationContext not found in " + APPLICATION_CONTEXT + " attribute of servlet context." );
92+ }
93+ if (groovyPagesUriService == null ) {
94+ groovyPagesUriService = new DefaultGroovyPagesUriService ();
95+ }
96+ }
97+
98+ @ SuppressWarnings ("unchecked" )
99+ private <T > T fetchBeanFromAppCtx (String name ) {
100+ try {
101+ return (T )appContext .getBean (name );
102+ }
103+ catch (BeansException e ) {
104+ LOG .warn ("Bean named '" + name + "' is missing." );
105+ return null ;
106+ }
106107 }
107108
108109 public String getPluginContextPath (HttpServletRequest request ) {
109110 GroovyObject controller = getController (request );
110- if (controller != null ) {
111+ if (controller != null ) {
111112 String path = pluginManager .getPluginPathForInstance (controller );
112113 return path == null ? "" : path ;
113114 }
114- else {
115- return "" ;
116- }
115+
116+ return "" ;
117117 }
118118
119119 public GroovyObject getController (ServletRequest request ) {
120120 return (GroovyObject )request .getAttribute (CONTROLLER );
121121 }
122122
123123 public String getControllerUri (ServletRequest request ) {
124- return "/" + getControllerName (request );
124+ return "/" + getControllerName (request );
125125 }
126126
127127 private String getControllerName (ServletRequest request ) {
128- String controllerName = (String ) request .getAttribute (GrailsApplicationAttributes .CONTROLLER_NAME_ATTRIBUTE );
129- if (controllerName == null || controllerName .length () == 0 ) {
130- GroovyObject controller = getController (request );
131- if (controller != null ) {
132- controllerName = (String )controller .getProperty (ControllerDynamicMethods .CONTROLLER_NAME_PROPERTY );
133- request .setAttribute (GrailsApplicationAttributes .CONTROLLER_NAME_ATTRIBUTE , controllerName );
134- }
135- }
128+ String controllerName = (String ) request .getAttribute (GrailsApplicationAttributes .CONTROLLER_NAME_ATTRIBUTE );
129+ if (controllerName == null || controllerName .length () == 0 ) {
130+ GroovyObject controller = getController (request );
131+ if (controller != null ) {
132+ controllerName = (String )controller .getProperty (ControllerDynamicMethods .CONTROLLER_NAME_PROPERTY );
133+ request .setAttribute (GrailsApplicationAttributes .CONTROLLER_NAME_ATTRIBUTE , controllerName );
134+ }
135+ }
136136 return controllerName != null ? controllerName : "" ;
137137 }
138138
139139 /**
140- *
141140 * @deprecated Use {@link org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest#getContextPath() instead}
142141 * @param request The Servlet Reqest
143142 * @return The Application URI
144143 */
145144 @ Deprecated
146145 public String getApplicationUri (ServletRequest request ) {
147- String appUri = (String ) request .getAttribute (GrailsApplicationAttributes .APP_URI_ATTRIBUTE );
148- if (appUri == null ) {
149- appUri = this . urlHelper .getContextPath ((HttpServletRequest )request );
150- }
151- return appUri ;
146+ String appUri = (String ) request .getAttribute (GrailsApplicationAttributes .APP_URI_ATTRIBUTE );
147+ if (appUri == null ) {
148+ appUri = urlHelper .getContextPath ((HttpServletRequest )request );
149+ }
150+ return appUri ;
152151 }
153152
154153 public ServletContext getServletContext () {
155- return this . context ;
154+ return context ;
156155 }
157156
158157 public FlashScope getFlashScope (ServletRequest request ) {
159- if (request instanceof HttpServletRequest ) {
160- HttpServletRequest servletRequest = (HttpServletRequest ) request ;
161- HttpSession session = servletRequest .getSession (false );
162- FlashScope fs ;
163- if (session != null ) {
164- fs = (FlashScope )session .getAttribute (FLASH_SCOPE );
158+ if (!(request instanceof HttpServletRequest )) {
159+ return null ;
160+ }
161+
162+ HttpServletRequest servletRequest = (HttpServletRequest ) request ;
163+ HttpSession session = servletRequest .getSession (false );
164+ FlashScope fs ;
165+ if (session != null ) {
166+ fs = (FlashScope )session .getAttribute (FLASH_SCOPE );
167+ }
168+ else {
169+ fs = (FlashScope )request .getAttribute (FLASH_SCOPE );
170+ }
171+ if (fs == null ) {
172+ fs = new GrailsFlashScope ();
173+ if (session !=null ) {
174+ session .setAttribute (FLASH_SCOPE ,fs );
165175 }
166176 else {
167- fs = (FlashScope )request .getAttribute (FLASH_SCOPE );
168- }
169- if (fs == null ) {
170- fs = new GrailsFlashScope ();
171- if (session !=null ) {
172- session .setAttribute (FLASH_SCOPE ,fs );
173- }
174- else {
175- request .setAttribute (FLASH_SCOPE ,fs );
176- }
177+ request .setAttribute (FLASH_SCOPE ,fs );
177178 }
178- return fs ;
179179 }
180- return null ;
180+ return fs ;
181181 }
182182
183- public String getTemplateUri (CharSequence templateName , ServletRequest request ) {
184- return groovyPagesUriService .getTemplateURI (getControllerName (request ), templateName .toString ());
185- }
183+ public String getTemplateUri (CharSequence templateName , ServletRequest request ) {
184+ return groovyPagesUriService .getTemplateURI (getControllerName (request ), templateName .toString ());
185+ }
186186
187- public String getViewUri (String viewName , HttpServletRequest request ) {
187+ public String getViewUri (String viewName , HttpServletRequest request ) {
188188 return groovyPagesUriService .getDeployedViewURI (getControllerName (request ), viewName );
189- }
189+ }
190190
191191 public String getControllerActionUri (ServletRequest request ) {
192192 GroovyObject controller = getController (request );
193-
194193 return (String )controller .getProperty (ControllerDynamicMethods .ACTION_URI_PROPERTY );
195194 }
196195
@@ -199,53 +198,56 @@ public Errors getErrors(ServletRequest request) {
199198 }
200199
201200 public GroovyPagesTemplateEngine getPagesTemplateEngine () {
202- if (pagesTemplateEngine != null ) {
203- return pagesTemplateEngine ;
204- } else {
205- throw new GroovyPagesException ("No bean named [" +GroovyPagesTemplateEngine .BEAN_ID +"] defined in Spring application context!" );
206- }
201+ if (pagesTemplateEngine != null ) {
202+ return pagesTemplateEngine ;
203+ }
204+
205+ throw new GroovyPagesException ("No bean named [" + GroovyPagesTemplateEngine .BEAN_ID +
206+ "] defined in Spring application context!" );
207207 }
208208
209209 public GrailsApplication getGrailsApplication () {
210210 return grailsApplication ;
211211 }
212-
212+
213213 public GroovyObject getTagLibraryForTag (HttpServletRequest request , HttpServletResponse response ,String tagName ) {
214- return getTagLibraryForTag (request , response , tagName , GroovyPage .DEFAULT_NAMESPACE );
215- }
216-
217- public GroovyObject getTagLibraryForTag (HttpServletRequest request , HttpServletResponse response ,String tagName , String namespace ) {
218- String nonNullNamesapce = namespace == null ? GroovyPage .DEFAULT_NAMESPACE : namespace ;
219- String fullTagName = nonNullNamesapce + ":" + tagName ;
220-
221- GrailsTagLibClass tagLibClass = (GrailsTagLibClass ) getGrailsApplication ().getArtefactForFeature (
214+ return getTagLibraryForTag (request , response , tagName , GroovyPage .DEFAULT_NAMESPACE );
215+ }
216+
217+ public GroovyObject getTagLibraryForTag (HttpServletRequest request , HttpServletResponse response ,String tagName , String namespace ) {
218+ String nonNullNamesapce = namespace == null ? GroovyPage .DEFAULT_NAMESPACE : namespace ;
219+ String fullTagName = nonNullNamesapce + ":" + tagName ;
220+
221+ GrailsTagLibClass tagLibClass = (GrailsTagLibClass ) getGrailsApplication ().getArtefactForFeature (
222222 TagLibArtefactHandler .TYPE , fullTagName );
223- if (tagLibClass == null )return null ;
224- return (GroovyObject )getApplicationContext ()
225- .getBean (tagLibClass .getFullName ());
226- }
227-
228- public Writer getOut (HttpServletRequest request ) {
229- return (Writer )request .getAttribute (OUT );
230- }
231-
232- public void setOut (HttpServletRequest request , Writer out2 ) {
233- request .setAttribute (OUT , out2 );
234- }
235-
236- public String getNoSuffixViewURI (GroovyObject controller , String viewName ) {
237- return groovyPagesUriService .getNoSuffixViewURI (controller , viewName );
238- }
239-
240- public String getTemplateURI (GroovyObject controller , String templateName ) {
241- return groovyPagesUriService .getTemplateURI (controller , templateName );
242- }
243-
244- public GroovyPagesUriService getGroovyPagesUriService () {
245- return groovyPagesUriService ;
246- }
247-
248- public MessageSource getMessageSource () {
249- return messageSource ;
250- }
223+ if (tagLibClass == null ) {
224+ return null ;
225+ }
226+
227+ return (GroovyObject )getApplicationContext ().getBean (tagLibClass .getFullName ());
228+ }
229+
230+ public Writer getOut (HttpServletRequest request ) {
231+ return (Writer )request .getAttribute (OUT );
232+ }
233+
234+ public void setOut (HttpServletRequest request , Writer out2 ) {
235+ request .setAttribute (OUT , out2 );
236+ }
237+
238+ public String getNoSuffixViewURI (GroovyObject controller , String viewName ) {
239+ return groovyPagesUriService .getNoSuffixViewURI (controller , viewName );
240+ }
241+
242+ public String getTemplateURI (GroovyObject controller , String templateName ) {
243+ return groovyPagesUriService .getTemplateURI (controller , templateName );
244+ }
245+
246+ public GroovyPagesUriService getGroovyPagesUriService () {
247+ return groovyPagesUriService ;
248+ }
249+
250+ public MessageSource getMessageSource () {
251+ return messageSource ;
252+ }
251253}
0 commit comments