KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > DefaultPageNormalizer


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.impl;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40
41 /**
42  * The default implementation of PageNormalizer attempts to read from
43  * <code>WEB-INF/web.xml</code> to find a <code>welcome-files</code> element,
44  * and uses a default of removing "<code>index.html</code>" and
45  * "<code>index.jsp</code>" if this proceedure fails.
46  * @author Joe Walker [joe at getahead dot ltd dot uk]
47  */

48 public class DefaultPageNormalizer implements PageNormalizer
49 {
50     /* (non-Javadoc)
51      * @see org.directwebremoting.extend.PageNormalizer#normalizePage(java.lang.String)
52      */

53     public String JavaDoc normalizePage(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc it = welcomeFiles.iterator(); it.hasNext();)
104         {
105             String JavaDoc welcomeFile = (String JavaDoc) 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     /**
117      * Accessor for the list of components to strip to normalize a filename
118      * @param context Our route to reading web.xml
119      * @return A list of the welcome files from web.xml or null if none are found there
120      */

121     protected static List JavaDoc getWebXmlWelcomeFileList(ServletContext JavaDoc context)
122     {
123         try
124         {
125             InputStream JavaDoc 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 JavaDoc builder = buildFactory.newDocumentBuilder();
139             builder.setEntityResolver(new EmptyEntityResolver());
140             builder.setErrorHandler(new LogErrorHandler());
141
142             InputSource JavaDoc is = new InputSource JavaDoc(in);
143             Document JavaDoc doc = builder.parse(is);
144
145             Element JavaDoc webapp = doc.getDocumentElement();
146             NodeList JavaDoc 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 JavaDoc reply = new ArrayList JavaDoc();
154             for (int i = 0; i < welcomeFileListNodes.getLength(); i++)
155             {
156                 Element JavaDoc welcomeFileListNode = (Element JavaDoc) welcomeFileListNodes.item(i);
157                 NodeList JavaDoc welcomeFileNodes = welcomeFileListNode.getElementsByTagName("welcome-file");
158                 for (int j = 0; j < welcomeFileNodes.getLength(); j++)
159                 {
160                     Element JavaDoc welcomeFileNode = (Element JavaDoc) welcomeFileNodes.item(j);
161                     String JavaDoc 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 JavaDoc ex)
171         {
172             log.warn("Failed to calculate welcome files from web.xml.", ex);
173             return null;
174         }
175     }
176
177     /**
178      * Use the default list of components to strip to normalize a filename
179      * @return A list of the default welcome files
180      */

181     protected static List JavaDoc getDefaultWelcomeFileList()
182     {
183         List JavaDoc reply = new ArrayList JavaDoc();
184         reply.add("index.html");
185         reply.add("index.htm");
186         reply.add("index.jsp");
187         return reply;
188     }
189
190     /**
191      * Accessor for the list of components to strip to normalize a filename
192      * @param welcomeFiles the welcomeFiles to set
193      */

194     public void setWelcomeFileList(List JavaDoc welcomeFiles)
195     {
196         this.welcomeFiles = welcomeFiles;
197     }
198
199     /**
200      * Accessor for the list of components to strip to normalize a filename
201      * @param welcomeFileNames the welcomeFiles to set as a comma or newline
202      * separated list.
203      */

204     public void setWelcomeFiles(String JavaDoc welcomeFileNames)
205     {
206         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(welcomeFileNames, "\n,");
207         while (st.hasMoreTokens())
208         {
209             welcomeFiles.add(st.nextToken());
210         }
211     }
212
213     /**
214      * Does the page normalizer include query strings in it's definition of
215      * pages?
216      * @param normalizeIncludesQueryString The new value
217      */

218     public void setNormalizeIncludesQueryString(boolean normalizeIncludesQueryString)
219     {
220         this.normalizeIncludesQueryString = normalizeIncludesQueryString;
221     }
222
223     /**
224      * @param servletContext the servletContext to set
225      */

226     public void setServletContext(ServletContext JavaDoc servletContext)
227     {
228         this.servletContext = servletContext;
229     }
230
231     /**
232      * We need one of these to do the init process.
233      */

234     protected ServletContext JavaDoc servletContext = null;
235
236     /**
237      * Does the page normalizer include query strings in it's definition of
238      * pages?
239      */

240     protected boolean normalizeIncludesQueryString = false;
241
242     /**
243      * How we create new documents
244      */

245     protected static DocumentBuilderFactory JavaDoc buildFactory = null;
246
247     /**
248      * The lock to prevent 2 things from calling init at the same time
249      */

250     protected static final Object JavaDoc initLock = new Object JavaDoc();
251
252     /**
253      * The list of filename components to strip to normalize a filename
254      */

255     protected List JavaDoc welcomeFiles;
256
257     /**
258      * The log stream
259      */

260     private static final Logger log = Logger.getLogger(DefaultPageNormalizer.class);
261 }
262
Popular Tags