KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > EmbedPageServlet


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.digester.Digester;
33 import org.apache.commons.lang.StringUtils;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * @author Liudong
38  * 用来处理.jspe的请求
39  * show_log.jspe=>index.jsp?main=show_log.jspe
40  */

41 public class EmbedPageServlet extends HttpServlet JavaDoc {
42
43     public final static String JavaDoc URL_SEPARATOR = "/";
44     protected MessageFormat JavaDoc mappingFormat;
45     protected String JavaDoc paramName = "main";
46     protected String JavaDoc baseDir = "/WEB-INF/jsp/";
47     
48     private String JavaDoc prefix ;
49     
50     /* (non-Javadoc)
51      * @see javax.servlet.GenericServlet#init()
52      */

53     public void init() throws ServletException JavaDoc {
54         
55         initServlet();
56         if(mappingFormat==null){
57             //servletMapping = "*.jspe";
58
mappingFormat = new MessageFormat JavaDoc("{0}.jspe");
59         }
60         String JavaDoc bd = getInitParameter("baseDir");
61         if(bd!=null && bd.trim().length()>0){
62             baseDir = bd;
63             if(!baseDir.startsWith(URL_SEPARATOR))
64                 baseDir = URL_SEPARATOR + baseDir;
65             if(!baseDir.endsWith(URL_SEPARATOR))
66                 baseDir += URL_SEPARATOR;
67         }
68
69         String JavaDoc cp = getInitParameter("container");
70         if(cp!=null&&cp.trim().length()>0){
71             if(!cp.startsWith(URL_SEPARATOR))
72                 cp = baseDir + URL_SEPARATOR + cp;
73         }
74         else
75             log("FATAL: cannot read parameter container's value, must assign a valid jsp");
76         
77         String JavaDoc pn = getInitParameter("paramName");
78         if(pn!=null&&pn.trim().length()>0)
79             paramName = pn;
80         
81         StringBuffer JavaDoc newPage = new StringBuffer JavaDoc(64);
82         newPage.append(cp);
83         newPage.append('?');
84         newPage.append(paramName);
85         newPage.append('=');
86         
87         prefix = newPage.toString();
88     }
89
90     /* (non-Javadoc)
91      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
92      */

93     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
94         throws ServletException JavaDoc, IOException JavaDoc
95     {
96         res.setLocale(req.getLocale());
97         String JavaDoc jspPage = null;
98         try{
99             jspPage = getForwardPage(req.getServletPath());
100         }catch(ParseException JavaDoc e){
101             String JavaDoc sPath = (String JavaDoc)req.getAttribute(Globals.ACTION_PATH_KEY);
102             if(sPath!=null){
103                 int param_idx = sPath.indexOf('?');
104                 if(param_idx!=-1)
105                     sPath = sPath.substring(0, param_idx);
106                 try{
107                     jspPage = getForwardPage(sPath);
108                 }catch(ParseException JavaDoc ee){
109                     log("parse forward path from action failed.",ee);
110                 }
111             }
112         }
113         //检查JSP文件是否存在
114
if(jspPage.startsWith(URL_SEPARATOR))
115             jspPage = jspPage.substring(URL_SEPARATOR.length());
116         StringBuffer JavaDoc jspPath = new StringBuffer JavaDoc(baseDir);
117         jspPath.append(jspPage);
118         if(!isJspExists(jspPath.toString())){
119             res.sendError(HttpServletResponse.SC_NOT_FOUND);
120             return;
121         }
122         jspPath = null;
123         //跳到新的界面
124
StringBuffer JavaDoc newPage = new StringBuffer JavaDoc(prefix);
125         newPage.append(jspPage);
126         
127         String JavaDoc url = newPage.toString();
128         getServletContext().getRequestDispatcher(url).include(req,res);
129         newPage = null;
130     }
131     
132     protected String JavaDoc getForwardPage(String JavaDoc path) throws ParseException JavaDoc {
133         Object JavaDoc[] ps = mappingFormat.parse(path);
134         return ps[0] + ".jsp";
135     }
136     
137     /* (non-Javadoc)
138      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
139      */

140     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
141         throws ServletException JavaDoc, IOException JavaDoc {
142         this.doGet(req, res);
143     }
144     
145     /**
146      * 判断某个JSP文件是否存在
147      * @param jsp
148      * @return
149      */

150     protected boolean isJspExists(String JavaDoc jsp){
151         String JavaDoc webpath = getServletContext().getRealPath(jsp);
152         if(!pages.contains(webpath)){
153             boolean exists = new File JavaDoc(webpath).exists();
154             if(exists)
155                 pages.add(webpath);
156             return exists;
157         }
158         return true;
159     }
160
161     private List JavaDoc pages = new Vector JavaDoc();
162
163     public void destroy() {
164         if(pages!=null){
165             pages.clear();
166             pages = null;
167         }
168     }
169     /**
170      * <p>Initialize the servlet mapping under which our controller servlet
171      * is being accessed. This will be used in the <code>&html:form&gt;</code>
172      * tag to generate correct destination URLs for form submissions.</p>
173      *
174      * @throws ServletException if error happens while scanning web.xml
175      */

176     protected void initServlet() throws ServletException JavaDoc {
177         // Prepare a Digester to scan the web application deployment descriptor
178
Digester digester = new Digester();
179         digester.push(this);
180         //digester.
181
digester.setNamespaceAware(true);
182         digester.setValidating(false);
183
184         // Register our local copy of the DTDs that we can find
185
for (int i = 0; i < registrations.length; i += 2) {
186             URL JavaDoc url = this.getClass().getResource(registrations[i+1]);
187             if (url != null) {
188                 digester.register(registrations[i], url.toString());
189             }
190         }
191
192         // Configure the processing rules that we need
193
digester.addCallMethod("web-app/servlet-mapping",
194                                "addServletMapping", 2);
195         digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
196         digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
197
198         InputStream JavaDoc input = getServletContext().getResourceAsStream("/WEB-INF/web.xml");
199
200         if(input==null)
201             throw new ServletException JavaDoc("Cannot read web.xml");
202
203         try {
204             digester.parse(input);
205         } catch (IOException JavaDoc e) {
206             throw new ServletException JavaDoc(e);
207         } catch (SAXException JavaDoc e) {
208             throw new ServletException JavaDoc(e);
209         } finally {
210             try {
211                 input.close();
212             } catch (IOException JavaDoc e) {
213                 throw new ServletException JavaDoc(e);
214             }
215         }
216     }
217     /**
218      * <p>Remember a servlet mapping from our web application deployment
219      * descriptor, if it is for this servlet.</p>
220      *
221      * @param servletName The name of the servlet being mapped
222      * @param urlPattern The URL pattern to which this servlet is mapped
223      */

224     public void addServletMapping(String JavaDoc servletName, String JavaDoc urlPattern) {
225         if (servletName == null)
226             return;
227         if (servletName.equals(getServletConfig().getServletName())) {
228             String JavaDoc servletMapping = StringUtils.replace(urlPattern, "*", "{0}");
229             mappingFormat = new MessageFormat JavaDoc(servletMapping);
230         }
231     }
232
233     /**
234      * <p>The set of public identifiers, and corresponding resource names, for
235      * the versions of the configuration file DTDs that we know about. There
236      * <strong>MUST</strong> be an even number of Strings in this list!</p>
237      */

238     protected String JavaDoc registrations[] = {
239         "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN",
240         "/org/apache/struts/resources/struts-config_1_0.dtd",
241         "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
242         "/org/apache/struts/resources/struts-config_1_1.dtd",
243         "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
244         "/org/apache/struts/resources/struts-config_1_2.dtd",
245         "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",
246         "/org/apache/struts/resources/web-app_2_2.dtd",
247         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
248         "/org/apache/struts/resources/web-app_2_3.dtd"
249     };
250
251 }
252
Popular Tags