1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2011 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -430,10 +430,19 @@ protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServl
430
430
return mav ;
431
431
}
432
432
433
+ /**
434
+ * This method always returns -1 since an annotated controller can have many methods,
435
+ * each requiring separate lastModified calculations. Instead, an
436
+ * @{@link RequestMapping}-annotated method can calculate the lastModified value, call
437
+ * {@link org.springframework.web.context.request.WebRequest#checkNotModified(long)}
438
+ * to check it, and return {@code null} if that returns {@code true}.
439
+ * @see org.springframework.web.context.request.WebRequest#checkNotModified(long)
440
+ */
433
441
public long getLastModified (HttpServletRequest request , Object handler ) {
434
442
return -1 ;
435
443
}
436
444
445
+
437
446
/**
438
447
* Build a HandlerMethodResolver for the given handler type.
439
448
*/
@@ -463,8 +472,7 @@ private ServletHandlerMethodResolver getMethodResolver(Object handler) {
463
472
* @see ServletRequestDataBinder#bind(javax.servlet.ServletRequest)
464
473
* @see ServletRequestDataBinder#convertIfNecessary(Object, Class, org.springframework.core.MethodParameter)
465
474
*/
466
- protected ServletRequestDataBinder createBinder (HttpServletRequest request , Object target , String objectName )
467
- throws Exception {
475
+ protected ServletRequestDataBinder createBinder (HttpServletRequest request , Object target , String objectName ) throws Exception {
468
476
return new ServletRequestDataBinder (target , objectName );
469
477
}
470
478
@@ -620,21 +628,20 @@ public Method resolveHandlerMethod(HttpServletRequest request) throws ServletExc
620
628
}
621
629
else {
622
630
if (!allowedMethods .isEmpty ()) {
623
- throw new HttpRequestMethodNotSupportedException (request .getMethod (),
624
- StringUtils .toStringArray (allowedMethods ));
631
+ throw new HttpRequestMethodNotSupportedException (request .getMethod (), StringUtils .toStringArray (allowedMethods ));
625
632
}
626
- throw new NoSuchRequestHandlingMethodException (lookupPath , request .getMethod (),
627
- request .getParameterMap ());
633
+ throw new NoSuchRequestHandlingMethodException (lookupPath , request .getMethod (), request .getParameterMap ());
628
634
}
629
635
}
630
636
631
637
/**
632
638
* Determines the combined pattern for the given methodLevelPattern and path.
633
- * <p>Uses the following algorithm: <ol>
639
+ * <p>Uses the following algorithm:
640
+ * <ol>
634
641
* <li>If there is a type-level mapping with path information, it is {@linkplain
635
642
* PathMatcher#combine(String, String) combined} with the method-level pattern.</li>
636
- * <li>If there is a {@linkplain HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE best matching pattern} in the
637
- * request, it is combined with the method-level pattern.</li>
643
+ * <li>If there is a {@linkplain HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE best matching pattern}
644
+ * in the request, it is combined with the method-level pattern.</li>
638
645
* <li>Otherwise, the method-level pattern is returned.</li>
639
646
* </ol>
640
647
*/
@@ -646,53 +653,54 @@ private String getCombinedPattern(String methodLevelPattern, String lookupPath,
646
653
typeLevelPattern = "/" + typeLevelPattern ;
647
654
}
648
655
String combinedPattern = pathMatcher .combine (typeLevelPattern , methodLevelPattern );
649
- if (isPathMatchInternal (combinedPattern , lookupPath )) {
650
- return combinedPattern ;
656
+ String matchingPattern = getMatchingPattern (combinedPattern , lookupPath );
657
+ if (matchingPattern != null ) {
658
+ return matchingPattern ;
651
659
}
652
660
}
653
661
return null ;
654
662
}
655
663
String bestMatchingPattern = (String ) request .getAttribute (HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE );
656
664
if (StringUtils .hasText (bestMatchingPattern ) && bestMatchingPattern .endsWith ("*" )) {
657
665
String combinedPattern = pathMatcher .combine (bestMatchingPattern , methodLevelPattern );
658
- if (! combinedPattern . equals ( bestMatchingPattern ) &&
659
- ( isPathMatchInternal ( combinedPattern , lookupPath ) )) {
660
- return combinedPattern ;
666
+ String matchingPattern = getMatchingPattern ( combinedPattern , lookupPath );
667
+ if ( matchingPattern != null && ! matchingPattern . equals ( bestMatchingPattern )) {
668
+ return matchingPattern ;
661
669
}
662
670
}
663
- if (isPathMatchInternal (methodLevelPattern , lookupPath )) {
664
- return methodLevelPattern ;
665
- }
666
- return null ;
671
+ return getMatchingPattern (methodLevelPattern , lookupPath );
667
672
}
668
673
669
- private boolean isPathMatchInternal (String pattern , String lookupPath ) {
670
- if (pattern .equals (lookupPath ) || pathMatcher . match ( pattern , lookupPath ) ) {
671
- return true ;
674
+ private String getMatchingPattern (String pattern , String lookupPath ) {
675
+ if (pattern .equals (lookupPath )) {
676
+ return pattern ;
672
677
}
673
678
boolean hasSuffix = pattern .indexOf ('.' ) != -1 ;
674
- if (!hasSuffix && pathMatcher .match (pattern + ".*" , lookupPath )) {
675
- return true ;
679
+ if (!hasSuffix ) {
680
+ String patternWithSuffix = pattern + ".*" ;
681
+ if (pathMatcher .match (patternWithSuffix , lookupPath )) {
682
+ return patternWithSuffix ;
683
+ }
684
+ }
685
+ if (pathMatcher .match (pattern , lookupPath )) {
686
+ return pattern ;
676
687
}
677
688
boolean endsWithSlash = pattern .endsWith ("/" );
678
- if (!endsWithSlash && pathMatcher .match (pattern + "/" , lookupPath )) {
679
- return true ;
689
+ if (!endsWithSlash ) {
690
+ String patternWithSlash = pattern + "/" ;
691
+ if (pathMatcher .match (patternWithSlash , lookupPath )) {
692
+ return patternWithSlash ;
693
+ }
680
694
}
681
- return false ;
695
+ return null ;
682
696
}
683
697
684
698
@ SuppressWarnings ("unchecked" )
685
- private void extractHandlerMethodUriTemplates (String mappedPattern ,
686
- String lookupPath ,
687
- HttpServletRequest request ) {
688
-
699
+ private void extractHandlerMethodUriTemplates (String mappedPattern , String lookupPath , HttpServletRequest request ) {
689
700
Map <String , String > variables =
690
701
(Map <String , String >) request .getAttribute (HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE );
691
-
692
702
int patternVariableCount = StringUtils .countOccurrencesOf (mappedPattern , "{" );
693
-
694
- if ( (variables == null || patternVariableCount != variables .size ())
695
- && pathMatcher .match (mappedPattern , lookupPath )) {
703
+ if ((variables == null || patternVariableCount != variables .size ()) && pathMatcher .match (mappedPattern , lookupPath )) {
696
704
variables = pathMatcher .extractUriTemplateVariables (mappedPattern , lookupPath );
697
705
request .setAttribute (HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE , variables );
698
706
}
0 commit comments