@ -31,10 +31,16 @@ import org.springframework.web.servlet.HandlerMapping;
*
* @author Jon Schneider
* @author Andy Wilkinson
* @author Brian Clozel
* @since 2.0 .0
* /
public final class WebMvcTags {
private static final Tag URI_NOT_FOUND = Tag . of ( "uri" , "NOT_FOUND" ) ;
private static final Tag URI_REDIRECTION = Tag . of ( "uri" , "REDIRECTION" ) ;
private WebMvcTags ( ) {
}
@ -69,21 +75,24 @@ public final class WebMvcTags {
* @return the uri tag derived from the request
* /
public static Tag uri ( HttpServletRequest request , HttpServletResponse response ) {
if ( re sponse ! = null ) {
HttpStatus status = extractStatus ( response ) ;
if ( status ! = null & & status . is3xxRedirection ( ) ) {
return Tag . of ( "uri" , "REDIRECTION" ) ;
if ( re quest ! = null ) {
String pattern = getMatchingPattern ( request ) ;
if ( pattern ! = null ) {
return Tag . of ( "uri" , pattern ) ;
}
if ( status ! = null & & status . equals ( HttpStatus . NOT_FOUND ) ) {
return Tag . of ( "uri" , "NOT_FOUND" ) ;
else if ( response ! = null ) {
HttpStatus status = extractStatus ( response ) ;
if ( status ! = null & & status . is3xxRedirection ( ) ) {
return URI_REDIRECTION ;
}
if ( status ! = null & & status . equals ( HttpStatus . NOT_FOUND ) ) {
return URI_NOT_FOUND ;
}
}
String pathInfo = getPathInfo ( request ) ;
return Tag . of ( "uri" , pathInfo . isEmpty ( ) ? "root" : pathInfo ) ;
}
if ( request = = null ) {
return Tag . of ( "uri" , "UNKNOWN" ) ;
}
String uri = getUri ( request ) ;
uri = uri . replaceAll ( "//+" , "/" ) . replaceAll ( "/$" , "" ) ;
return Tag . of ( "uri" , uri . isEmpty ( ) ? "root" : uri ) ;
return Tag . of ( "uri" , "UNKNOWN" ) ;
}
private static HttpStatus extractStatus ( HttpServletResponse response ) {
@ -95,11 +104,16 @@ public final class WebMvcTags {
}
}
private static String get Uri ( HttpServletRequest request ) {
String uri = ( String ) request
private static String get MatchingPattern ( HttpServletRequest request ) {
return ( String ) request
. getAttribute ( HandlerMapping . BEST_MATCHING_PATTERN_ATTRIBUTE ) ;
uri = ( uri ! = null ? uri : request . getPathInfo ( ) ) ;
return ( StringUtils . hasText ( uri ) ? uri : "/" ) ;
}
private static String getPathInfo ( HttpServletRequest request ) {
String uri = StringUtils . hasText ( request . getPathInfo ( ) ) ?
request . getPathInfo ( ) : "/" ;
return uri . replaceAll ( "//+" , "/" )
. replaceAll ( "/$" , "" ) ;
}
/ * *