Skip to content

Commit a56bff0

Browse files
committed
cleanup
1 parent be56264 commit a56bff0

File tree

4 files changed

+97
-111
lines changed

4 files changed

+97
-111
lines changed

src/java/org/codehaus/groovy/grails/web/mapping/exceptions/UrlMappingException.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@
1717
import org.codehaus.groovy.grails.exceptions.GrailsException;
1818

1919
/**
20-
* An exception thrown when an error occurs evaluating an URL mapping
20+
* Thrown when an error occurs evaluating an URL mapping.
2121
*
2222
* @author Graeme Rocher
2323
* @since 0.5
24-
*
25-
*
26-
* <p/>
27-
* Created: Feb 28, 2007
28-
* Time: 6:22:38 PM
2924
*/
3025
public class UrlMappingException extends GrailsException {
31-
private static final long serialVersionUID = -2062478398945064606L;
3226

33-
public UrlMappingException(String message) {
27+
private static final long serialVersionUID = -2062478398945064606L;
28+
29+
public UrlMappingException(String message) {
3430
super(message);
3531
}
3632

src/java/org/codehaus/groovy/grails/web/mapping/filter/UrlMappingsFilter.java

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@
1515
package org.codehaus.groovy.grails.web.mapping.filter;
1616

1717
import grails.util.GrailsUtil;
18+
19+
import java.io.IOException;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.ServletContext;
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
1831
import org.apache.commons.logging.Log;
1932
import org.apache.commons.logging.LogFactory;
2033
import org.codehaus.groovy.grails.commons.ControllerArtefactHandler;
2134
import org.codehaus.groovy.grails.commons.GrailsApplication;
2235
import org.codehaus.groovy.grails.commons.GrailsClass;
36+
import org.codehaus.groovy.grails.compiler.GrailsClassLoader;
2337
import org.codehaus.groovy.grails.web.mapping.UrlMappingInfo;
2438
import org.codehaus.groovy.grails.web.mapping.UrlMappingsHolder;
2539
import org.codehaus.groovy.grails.web.mapping.exceptions.UrlMappingException;
@@ -28,38 +42,20 @@
2842
import org.codehaus.groovy.grails.web.servlet.WrappedResponseHolder;
2943
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
3044
import org.codehaus.groovy.grails.web.util.WebUtils;
31-
import org.codehaus.groovy.grails.compiler.GrailsClassLoader;
3245
import org.springframework.web.filter.OncePerRequestFilter;
46+
import org.springframework.web.multipart.MultipartException;
47+
import org.springframework.web.multipart.MultipartHttpServletRequest;
3348
import org.springframework.web.servlet.HandlerInterceptor;
3449
import org.springframework.web.servlet.ModelAndView;
3550
import org.springframework.web.servlet.View;
3651
import org.springframework.web.servlet.ViewResolver;
3752
import org.springframework.web.util.UrlPathHelper;
38-
import org.springframework.web.multipart.MultipartException;
39-
import org.springframework.web.multipart.MultipartHttpServletRequest;
40-
41-
import javax.servlet.FilterChain;
42-
import javax.servlet.ServletContext;
43-
import javax.servlet.ServletException;
44-
import javax.servlet.http.HttpServletRequest;
45-
import javax.servlet.http.HttpServletResponse;
46-
import java.io.IOException;
47-
import java.util.Collections;
48-
import java.util.HashMap;
49-
import java.util.List;
50-
import java.util.Map;
5153

5254
/**
53-
* <p>A Servlet filter that uses the Grails UrlMappings to match and forward requests to a relevant controller
54-
* and action
55+
* Uses the Grails UrlMappings to match and forward requests to a relevant controller and action.
5556
*
5657
* @author Graeme Rocher
5758
* @since 0.5
58-
*
59-
*
60-
* <p/>
61-
* Created: Mar 6, 2007
62-
* Time: 7:58:19 AM
6359
*/
6460
public class UrlMappingsFilter extends OncePerRequestFilter {
6561

@@ -71,15 +67,18 @@ public class UrlMappingsFilter extends OncePerRequestFilter {
7167
private GrailsApplication application;
7268
private ViewResolver viewResolver;
7369

70+
@Override
7471
protected void initFilterBean() throws ServletException {
7572
super.initFilterBean();
7673
urlHelper.setUrlDecode(false);
7774
final ServletContext servletContext = getServletContext();
78-
this.handlerInterceptors = WebUtils.lookupHandlerInterceptors(servletContext);
75+
this.handlerInterceptors = WebUtils.lookupHandlerInterceptors(servletContext);
7976
this.application = WebUtils.lookupApplication(servletContext);
8077
this.viewResolver = WebUtils.lookupViewResolver(servletContext);
8178
}
8279

80+
@SuppressWarnings("unchecked")
81+
@Override
8382
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
8483
UrlMappingsHolder holder = WebUtils.lookupUrlMappings(getServletContext());
8584

@@ -91,31 +90,30 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
9190
checkForCompilationErrors();
9291

9392
GrailsClass[] controllers = application.getArtefacts(ControllerArtefactHandler.TYPE);
94-
if((controllers == null || controllers.length == 0 || holder == null) && !"/".equals(uri)) {
93+
if ((controllers == null || controllers.length == 0 || holder == null) && !"/".equals(uri)) {
9594
processFilterChain(request, response, filterChain);
9695
return;
97-
}else if (excludePatterns!=null && excludePatterns.size()>0){
98-
for (String excludePattern:excludePatterns){
99-
if (uri.equals(excludePattern)||
100-
(excludePattern.endsWith("*")&&
101-
excludePattern.substring(0,excludePattern.length()-1).
102-
regionMatches(0,uri,0,excludePattern.length()-1))){
96+
}
97+
98+
if (excludePatterns != null && excludePatterns.size() > 0) {
99+
for (String excludePattern:excludePatterns) {
100+
if (uri.equals(excludePattern) ||
101+
(excludePattern.endsWith("*") &&
102+
excludePattern.substring(0,excludePattern.length() -1 ).regionMatches(0, uri, 0, excludePattern.length() - 1))) {
103103
processFilterChain(request, response, filterChain);
104104
return;
105105
}
106106
}
107107
}
108108

109-
if(LOG.isDebugEnabled()) {
109+
if (LOG.isDebugEnabled()) {
110110
LOG.debug("Executing URL mapping filter...");
111111
LOG.debug(holder);
112112
}
113113

114-
115-
116-
if(WebUtils.areFileExtensionsEnabled()) {
114+
if (WebUtils.areFileExtensionsEnabled()) {
117115
String format = WebUtils.getFormatFromURI(uri);
118-
if(format!=null) {
116+
if (format != null) {
119117
MimeType[] configuredMimes = MimeType.getConfiguredMimeTypes();
120118
// only remove the file extension if its one of the configured mimes in Config.groovy
121119
for (MimeType configuredMime : configuredMimes) {
@@ -134,7 +132,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
134132
try {
135133
// GRAILS-3369: Save the original request parameters.
136134
Map backupParameters;
137-
138135
try {
139136
backupParameters = new HashMap(webRequest.getParams());
140137
}
@@ -168,8 +165,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
168165
}
169166
catch (Exception e) {
170167
GrailsUtil.deepSanitize(e);
171-
if(e instanceof MultipartException) {
172-
throw ((MultipartException)e);
168+
if (e instanceof MultipartException) {
169+
throw ((MultipartException)e);
173170
}
174171
LOG.error("Error when matching URL mapping [" + info + "]:" + e.getMessage(), e);
175172
continue;
@@ -180,68 +177,63 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
180177
request = checkMultipart(request);
181178

182179
if (viewName == null || viewName.endsWith(GSP_SUFFIX) || viewName.endsWith(JSP_SUFFIX)) {
183-
184-
if(info.isParsingRequest()) {
180+
if (info.isParsingRequest()) {
185181
webRequest.informParameterCreationListeners();
186182
}
187183
String forwardUrl = WebUtils.forwardRequestForUrlMappingInfo(request, response, info);
188184
if (LOG.isDebugEnabled()) {
189185
LOG.debug("Matched URI [" + uri + "] to URL mapping [" + info + "], forwarding to [" + forwardUrl + "] with response [" + response.getClass() + "]");
190186
}
191-
192187
}
193188
else {
194-
if(!renderViewForUrlMappingInfo(request, response, info, viewName)) {
189+
if (!renderViewForUrlMappingInfo(request, response, info, viewName)) {
195190
dispatched = false;
196191
}
197192
}
198193
break;
199194
}
200-
201195
}
202196
}
203197
finally {
204198
WrappedResponseHolder.setWrappedResponse(null);
205199
}
206200

207-
if(!dispatched) {
208-
if(LOG.isDebugEnabled()) {
201+
if (!dispatched) {
202+
if (LOG.isDebugEnabled()) {
209203
LOG.debug("No match found, processing remaining filter chain.");
210204
}
211205
processFilterChain(request, response, filterChain);
212206
}
213-
214207
}
215208

216209
private void checkForCompilationErrors() {
217-
if(!application.isWarDeployed()) {
210+
if (application.isWarDeployed()) {
211+
return;
212+
}
218213

219-
ClassLoader classLoader = application.getClassLoader();
220-
if(classLoader instanceof GrailsClassLoader) {
221-
GrailsClassLoader gcl = (GrailsClassLoader) classLoader;
222-
if(gcl.hasCompilationErrors()) {
223-
throw gcl.getCompilationError();
224-
}
214+
ClassLoader classLoader = application.getClassLoader();
215+
if (classLoader instanceof GrailsClassLoader) {
216+
GrailsClassLoader gcl = (GrailsClassLoader) classLoader;
217+
if (gcl.hasCompilationErrors()) {
218+
throw gcl.getCompilationError();
225219
}
226220
}
227221
}
228222

229223
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
230224
// Lookup from request attribute. The resolver that handles MultiPartRequest is dealt with earlier inside DefaultUrlMappingInfo with Grails
231225
HttpServletRequest resolvedRequest = (HttpServletRequest) request.getAttribute(MultipartHttpServletRequest.class.getName());
232-
if(resolvedRequest!=null) return resolvedRequest;
226+
if (resolvedRequest!=null) return resolvedRequest;
233227
return request;
234228
}
235229

236-
237230
private boolean renderViewForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, String viewName) {
238-
if(viewResolver != null) {
231+
if (viewResolver != null) {
239232
View v;
240233
try {
241-
242234
// execute pre handler interceptors
243235
for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
244-
if(!handlerInterceptor.preHandle(request, response, this)) return false;
236+
if (!handlerInterceptor.preHandle(request, response, this)) return false;
245237
}
246238

247239
// execute post handlers directly after, since there is no controller. The filter has a chance to modify the view at this point;
@@ -257,9 +249,8 @@ private boolean renderViewForUrlMappingInfo(HttpServletRequest request, HttpServ
257249
for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
258250
handlerInterceptor.afterCompletion(request, response, this, null);
259251
}
260-
261-
262-
} catch (Exception e) {
252+
}
253+
catch (Exception e) {
263254
GrailsUtil.deepSanitize(e);
264255
for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
265256
try {
@@ -278,12 +269,12 @@ private boolean renderViewForUrlMappingInfo(HttpServletRequest request, HttpServ
278269
private void processFilterChain(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
279270
try {
280271
WrappedResponseHolder.setWrappedResponse(response);
281-
if(filterChain != null)
272+
if (filterChain != null) {
282273
filterChain.doFilter(request,response);
283-
} finally {
274+
}
275+
}
276+
finally {
284277
WrappedResponseHolder.setWrappedResponse(null);
285278
}
286279
}
287-
288-
289280
}

src/test/grails/util/PluginBuildSettingsTests.groovy

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import org.springframework.core.io.Resource
88
* @author Graeme Rocher
99
* @since 1.1
1010
*/
11-
public class PluginBuildSettingsTests extends GroovyTestCase{
11+
class PluginBuildSettingsTests extends GroovyTestCase {
12+
1213
private static final File TEST_PROJ_DIR = new File("test/test-projects/plugin-build-settings")
1314

1415
PluginBuildSettings createPluginBuildSettings() {
@@ -20,7 +21,6 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
2021
void testGetPluginSourceFiles() {
2122
PluginBuildSettings pluginSettings = createPluginBuildSettings()
2223
def sourceFiles = pluginSettings.getPluginSourceFiles()
23-
2424
}
2525

2626
void testGetMetadataForPlugin() {
@@ -34,20 +34,19 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
3434
// exercise cache
3535
assertNotNull "cache xml was null, shoudln't have been", pluginSettings.getMetadataForPlugin("hibernate")
3636
}
37+
3738
void testGetPluginDirForName() {
3839
PluginBuildSettings pluginSettings = createPluginBuildSettings()
3940

4041
assertNotNull "should have found plugin dir",pluginSettings.getPluginDirForName("hibernate")
4142
assertNotNull "should have found plugin dir",pluginSettings.getPluginDirForName("hibernate")
42-
4343
}
4444

4545
void testGetPluginLibDirs() {
4646
PluginBuildSettings pluginSettings = createPluginBuildSettings()
4747

4848
assertEquals 2, pluginSettings.getPluginLibDirectories().size()
4949
assertEquals 2, pluginSettings.getPluginLibDirectories().size()
50-
5150
}
5251

5352
void testGetPluginDescriptors() {
@@ -88,7 +87,6 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
8887
void testGetPluginInfos() {
8988
PluginBuildSettings pluginSettings = createPluginBuildSettings()
9089

91-
9290
def pluginInfos = pluginSettings.getPluginInfos()
9391

9492
assertEquals 2, pluginInfos.size()
@@ -100,8 +98,7 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
10098
void testGetPluginResourceBundles() {
10199
def bundles = createPluginBuildSettings().pluginResourceBundles
102100

103-
// Check the bundles that there are 3 bundles (2 from Hibernate,
104-
// 1 from WebFlow).
101+
// Check the bundles that there are 3 bundles (2 from Hibernate, 1 from WebFlow).
105102
assertEquals 4, bundles.size()
106103

107104
// Check that the caching doesn't break anything.
@@ -142,8 +139,7 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
142139
void testGetPluginJarFiles() {
143140
def jars = createPluginBuildSettings().pluginJarFiles
144141

145-
// Make sure that all the JARs provided by the plugins are
146-
// included in the list.
142+
// Make sure that all the JARs provided by the plugins are included in the list.
147143
assertEquals 3, jars.size()
148144

149145
// Check that the caching doesn't break anything.
@@ -159,10 +155,9 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
159155
void testGetSupportPluginInfosWithPluginManager() {
160156
def pluginSettings = createPluginBuildSettings()
161157
pluginSettings.pluginManager = [
162-
getGrailsPlugin: { String pluginName ->
163-
return [ supportsCurrentScopeAndEnvironment: {-> pluginName == "hibernate" } ] as GrailsPlugin
164-
}
165-
] as GrailsPluginManager
158+
getGrailsPlugin: { String pluginName ->
159+
[supportsCurrentScopeAndEnvironment: { -> pluginName == "hibernate" }] as GrailsPlugin
160+
}] as GrailsPluginManager
166161

167162
def pluginInfos = pluginSettings.supportedPluginInfos
168163

@@ -171,13 +166,15 @@ public class PluginBuildSettingsTests extends GroovyTestCase{
171166

172167
void testIsGlobalPluginLocation() {
173168
def pluginSettings = createPluginBuildSettings()
174-
assertFalse pluginSettings.isGlobalPluginLocation([ getFile: {-> new File(".").absoluteFile} ] as Resource)
175-
assertFalse pluginSettings.isGlobalPluginLocation([ getFile: {-> new File(TEST_PROJ_DIR, "test")} ] as Resource)
169+
assertFalse pluginSettings.isGlobalPluginLocation([ getFile: { -> new File(".").absoluteFile} ] as Resource)
170+
assertFalse pluginSettings.isGlobalPluginLocation([ getFile: { -> new File(TEST_PROJ_DIR, "test")} ] as Resource)
176171
assertTrue pluginSettings.isGlobalPluginLocation([
177-
getFile: {-> new File(pluginSettings.buildSettings.globalPluginsDir, "test")}
178-
] as Resource)
172+
getFile: {
173+
-> new File(pluginSettings.buildSettings.globalPluginsDir, "test")
174+
}] as Resource)
179175
assertTrue pluginSettings.isGlobalPluginLocation([
180-
getFile: {-> new File(pluginSettings.buildSettings.globalPluginsDir, "test/../gwt")}
181-
] as Resource)
176+
getFile: {
177+
-> new File(pluginSettings.buildSettings.globalPluginsDir, "test/../gwt")
178+
}] as Resource)
182179
}
183180
}

0 commit comments

Comments
 (0)