1 16 package org.apache.myfaces.webapp.filter; 17 18 import javax.servlet.*; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.xml.parsers.SAXParser ; 21 import javax.xml.parsers.SAXParserFactory ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 26 27 42 public class WelcomeFileFilter 43 implements Filter 44 { 45 47 private FilterConfig _config; 48 private ServletContext _context; 49 private String [] _welcomeFiles = new String [0]; 50 51 53 56 public void destroy() 57 { 58 _config = null; 59 _context = null; 60 _welcomeFiles = null; 61 } 62 63 80 public void doFilter( 81 ServletRequest request, ServletResponse response, FilterChain chain) 82 throws IOException , ServletException 83 { 84 if (_config == null) 85 { 86 return; 87 } 88 89 HttpServletRequest httpRequest = (HttpServletRequest ) request; 90 String uri = httpRequest.getRequestURI(); 91 92 if (uri.lastIndexOf('.') > uri.lastIndexOf('/')) 96 { 97 chain.doFilter(request, response); 98 99 return; 100 } 101 102 String contextPath = httpRequest.getContextPath(); 103 String welcomeFile = null; 104 StringBuffer sb = new StringBuffer (uri); 105 106 if (!uri.endsWith("/")) 107 { 108 sb.append('/'); 109 } 110 111 String baseURI = sb.delete( 112 0, 113 contextPath.length()).toString(); 114 115 for (int i = 0; i < _welcomeFiles.length; i++) 119 { 120 sb.setLength(0); 121 sb.append(baseURI).append(_welcomeFiles[i]); 122 123 File file = new File (_context.getRealPath(sb.toString())); 124 125 if (file.exists()) 127 { 128 if (_welcomeFiles[i].endsWith(".jsp")) 131 { 132 sb.replace( 135 sb.lastIndexOf(".jsp"), 136 sb.length(), 137 ".jsf"); 138 welcomeFile = sb.toString(); 139 } 140 141 break; 144 } 145 } 146 147 if (welcomeFile == null) 148 { 149 sb.setLength(0); 150 sb.append(baseURI); 151 sb.append("index.jsf"); 152 welcomeFile = sb.toString(); 153 } 154 155 RequestDispatcher rd = httpRequest.getRequestDispatcher(welcomeFile); 156 rd.forward(request, response); 157 158 return; 159 } 160 161 167 public void init(FilterConfig config) 168 throws ServletException 169 { 170 if (config == null) 171 { 172 return; 173 } 174 175 this._config = config; 176 this._context = config.getServletContext(); 177 178 try 179 { 180 SAXParserFactory factory = SAXParserFactory.newInstance(); 181 factory.setValidating(true); 182 factory.setNamespaceAware(false); 183 184 SAXParser parser = factory.newSAXParser(); 185 WelcomeFileHandler handler = new WelcomeFileHandler(); 186 InputStream is = 187 _context.getResourceAsStream("WEB-INF/web.xml"); 188 189 if (is == null) 190 { 191 _context.log("Unable to get inputstream for web.xml"); 192 } 193 194 parser.parse(is, handler); 195 _welcomeFiles = handler.getWelcomeFiles(); 196 _context.log("Number of welcome files: " + _welcomeFiles.length); 197 } 198 catch (Exception ex) 199 { 200 throw new ServletException(ex); 201 } 202 } 203 } 204 | Popular Tags |