1 16 package org.directwebremoting.impl; 17 18 import java.io.InputStream ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 23 24 import javax.servlet.ServletContext ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 28 import org.directwebremoting.WebContext; 29 import org.directwebremoting.WebContextFactory; 30 import org.directwebremoting.extend.PageNormalizer; 31 import org.directwebremoting.servlet.PathConstants; 32 import org.directwebremoting.util.DomUtil; 33 import org.directwebremoting.util.EmptyEntityResolver; 34 import org.directwebremoting.util.LogErrorHandler; 35 import org.directwebremoting.util.Logger; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NodeList ; 39 import org.xml.sax.InputSource ; 40 41 48 public class DefaultPageNormalizer implements PageNormalizer 49 { 50 53 public String normalizePage(String unnormalized) 54 { 55 synchronized (initLock) 56 { 57 if (welcomeFiles == null) 58 { 59 if (servletContext != null) 60 { 61 welcomeFiles = getWebXmlWelcomeFileList(servletContext); 62 } 63 else 64 { 65 WebContext webContext = WebContextFactory.get(); 66 if (webContext == null) 67 { 68 log.warn("Can't find ServletContext to check for <welcome-file-list> in web.xml. Assuming defaults."); 69 log.warn(" - To prevent this message from happening, either call the PageNormalizer from a DWR thread"); 70 log.warn(" - Or seed the PageNormalizer with a ServletContext before access from outside a DWR thread"); 71 } 72 else 73 { 74 ServletContext threadServletContext = webContext.getServletContext(); 75 welcomeFiles = getWebXmlWelcomeFileList(threadServletContext); 76 } 77 } 78 } 79 80 if (welcomeFiles == null) 81 { 82 log.debug("Using default welcome file list (index.[jsp|htm[l]])"); 83 welcomeFiles = getDefaultWelcomeFileList(); 84 } 85 } 86 87 if (unnormalized == null) 88 { 89 return null; 90 } 91 92 String normalized = unnormalized; 93 94 if (!normalizeIncludesQueryString) 95 { 96 int queryPos = normalized.indexOf('?'); 97 if (queryPos != -1) 98 { 99 normalized = normalized.substring(0, queryPos); 100 } 101 } 102 103 for (Iterator it = welcomeFiles.iterator(); it.hasNext();) 104 { 105 String welcomeFile = (String ) it.next(); 106 if (normalized.endsWith(welcomeFile)) 107 { 108 normalized = normalized.substring(0, normalized.length() - welcomeFile.length()); 109 break; 110 } 111 } 112 113 return normalized; 114 } 115 116 121 protected static List getWebXmlWelcomeFileList(ServletContext context) 122 { 123 try 124 { 125 InputStream in = context.getResourceAsStream(PathConstants.RESOURCE_WEB_XML); 126 if (in == null) 127 { 128 log.warn("Missing " + PathConstants.RESOURCE_WEB_XML); 129 return null; 130 } 131 132 if (buildFactory == null) 133 { 134 buildFactory = DocumentBuilderFactory.newInstance(); 135 buildFactory.setValidating(false); 136 } 137 138 DocumentBuilder builder = buildFactory.newDocumentBuilder(); 139 builder.setEntityResolver(new EmptyEntityResolver()); 140 builder.setErrorHandler(new LogErrorHandler()); 141 142 InputSource is = new InputSource (in); 143 Document doc = builder.parse(is); 144 145 Element webapp = doc.getDocumentElement(); 146 NodeList welcomeFileListNodes = webapp.getElementsByTagName("welcome-file-list"); 147 if (welcomeFileListNodes.getLength() == 0) 148 { 149 log.debug("web.xml contains no <welcome-file-list> element"); 150 return null; 151 } 152 153 List reply = new ArrayList (); 154 for (int i = 0; i < welcomeFileListNodes.getLength(); i++) 155 { 156 Element welcomeFileListNode = (Element ) welcomeFileListNodes.item(i); 157 NodeList welcomeFileNodes = welcomeFileListNode.getElementsByTagName("welcome-file"); 158 for (int j = 0; j < welcomeFileNodes.getLength(); j++) 159 { 160 Element welcomeFileNode = (Element ) welcomeFileNodes.item(j); 161 String welcomeFile = DomUtil.getText(welcomeFileNode); 162 reply.add(welcomeFile); 163 164 log.debug("Adding welcome-file: " + welcomeFile); 165 } 166 } 167 168 return reply; 169 } 170 catch (Exception ex) 171 { 172 log.warn("Failed to calculate welcome files from web.xml.", ex); 173 return null; 174 } 175 } 176 177 181 protected static List getDefaultWelcomeFileList() 182 { 183 List reply = new ArrayList (); 184 reply.add("index.html"); 185 reply.add("index.htm"); 186 reply.add("index.jsp"); 187 return reply; 188 } 189 190 194 public void setWelcomeFileList(List welcomeFiles) 195 { 196 this.welcomeFiles = welcomeFiles; 197 } 198 199 204 public void setWelcomeFiles(String welcomeFileNames) 205 { 206 StringTokenizer st = new StringTokenizer (welcomeFileNames, "\n,"); 207 while (st.hasMoreTokens()) 208 { 209 welcomeFiles.add(st.nextToken()); 210 } 211 } 212 213 218 public void setNormalizeIncludesQueryString(boolean normalizeIncludesQueryString) 219 { 220 this.normalizeIncludesQueryString = normalizeIncludesQueryString; 221 } 222 223 226 public void setServletContext(ServletContext servletContext) 227 { 228 this.servletContext = servletContext; 229 } 230 231 234 protected ServletContext servletContext = null; 235 236 240 protected boolean normalizeIncludesQueryString = false; 241 242 245 protected static DocumentBuilderFactory buildFactory = null; 246 247 250 protected static final Object initLock = new Object (); 251 252 255 protected List welcomeFiles; 256 257 260 private static final Logger log = Logger.getLogger(DefaultPageNormalizer.class); 261 } 262 | Popular Tags |