2121import org .jruby .rack .servlet .ServletRackContext ;
2222
2323/**
24- * A filter that does dispatch to the Ruby side and might alter the incoming
24+ * A filter that does dispatch to the Ruby side and might alter the incoming
2525 * request URI while attempting to map to an available static resource.
26- *
26+ *
2727 * Related to serving static .html resources, supports configuration options:
28- *
28+ *
2929 * * {@link #isAddsHtmlToPathInfo()}, true by default - controls whether static
30- * resource resolution will be attempted, request methods {@code getPathInfo()}
30+ * resource resolution will be attempted, request methods {@code getPathInfo()}
3131 * and {@code getRequestURI()} will be modified to reflect a .html path
32- *
32+ *
3333 * * {@link #isVerifiesHtmlResource()} off by default - attempts to resolve the
3434 * resource using {@code context.getResource(path)} before changing the path
35- *
35+ *
3636 * @see UnmappedRackFilter
3737 */
3838public class RackFilter extends UnmappedRackFilter {
39-
39+
4040 private boolean addsHtmlToPathInfo = true ;
4141 private boolean verifiesHtmlResource = false ;
4242
@@ -49,19 +49,19 @@ public RackFilter(RackDispatcher dispatcher, RackContext context) {
4949 super (dispatcher , context );
5050 initializeFromConfig ();
5151 }
52-
52+
5353 @ Override
5454 public void init (FilterConfig config ) throws ServletException {
5555 super .init (config );
5656 initializeFromConfig (); // configure init parameters the "old" way
57-
57+
5858 // filter init params are preffered and override context params :
5959 String value = config .getInitParameter ("addsHtmlToPathInfo" );
6060 if ( value != null ) setAddsHtmlToPathInfo (Boolean .parseBoolean (value ));
6161 value = config .getInitParameter ("verifiesHtmlResource" );
6262 if ( value != null ) setVerifiesHtmlResource (Boolean .parseBoolean (value ));
6363 }
64-
64+
6565 private void initializeFromConfig () {
6666 final RackConfig rackConfig = getContext ().getConfig (); // backward compatibility :
6767 addsHtmlToPathInfo = rackConfig .getBooleanProperty ("jruby.rack.filter.adds.html" , true );
@@ -70,25 +70,26 @@ private void initializeFromConfig() {
7070
7171 @ Override
7272 protected void doFilterInternal (
73- final RequestCapture requestCapture ,
73+ final RequestCapture requestCapture ,
7474 final ResponseCapture responseCapture ,
75- final FilterChain chain ,
75+ final FilterChain chain ,
7676 final RackEnvironment env ) throws IOException , ServletException {
7777 ServletRequest pathChangedRequest = addHtmlToPathAndVerifyResource (requestCapture , env );
7878 chain .doFilter (pathChangedRequest , responseCapture );
7979 }
80-
80+
8181 private ServletRequest addHtmlToPathAndVerifyResource (ServletRequest request , RackEnvironment env ) {
8282 HttpServletRequest httpRequest = (HttpServletRequest ) request ;
83-
83+
8484 if ( ! isAddsHtmlToPathInfo () ) return httpRequest ;
8585
8686 final String path = env .getPathInfo ();
8787
88- if ( path .lastIndexOf ('.' ) <= path .lastIndexOf ('/' ) ) {
89-
88+ final int lastDelim = path .lastIndexOf ('/' );
89+ if ( path .lastIndexOf ('.' ) <= lastDelim ) {
90+
9091 final StringBuilder htmlSuffix = new StringBuilder (10 );
91- if (path .endsWith ( "/" ) ) {
92+ if ( lastDelim == path .length () - 1 ) { // ends-with '/'
9293 htmlSuffix .append ("index" );
9394 }
9495 htmlSuffix .append (".html" );
@@ -137,17 +138,18 @@ public String getRequestURI() {
137138 return httpRequest ;
138139 }
139140
140- private boolean resourceExists (final String path ) {
141+ protected boolean resourceExists (final String path ) {
141142 ServletRackContext servletContext = (ServletRackContext ) getContext ();
142143 try {
143144 return servletContext .getResource (path ) != null ;
144- } catch (MalformedURLException e ) {
145+ }
146+ catch (MalformedURLException e ) {
145147 return false ;
146148 }
147149 }
148-
150+
149151 // getters - setters :
150-
152+
151153 public boolean isAddsHtmlToPathInfo () {
152154 return addsHtmlToPathInfo ;
153155 }
@@ -163,5 +165,5 @@ public void setAddsHtmlToPathInfo(boolean addsHtmlToPathInfo) {
163165 public void setVerifiesHtmlResource (boolean verifiesHtmlResource ) {
164166 this .verifiesHtmlResource = verifiesHtmlResource ;
165167 }
166-
168+
167169}
0 commit comments