001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.text.MessageFormat;
023: import java.text.ParseException;
024: import java.util.Vector;
025: import java.util.List;
026:
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServlet;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.commons.digester.Digester;
033: import org.apache.commons.lang.StringUtils;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * @author Liudong
038: * 用来处理.jspe的请求
039: * show_log.jspe=>index.jsp?main=show_log.jspe
040: */
041: public class EmbedPageServlet extends HttpServlet {
042:
043: public final static String URL_SEPARATOR = "/";
044: protected MessageFormat mappingFormat;
045: protected String paramName = "main";
046: protected String baseDir = "/WEB-INF/jsp/";
047:
048: private String prefix;
049:
050: /* (non-Javadoc)
051: * @see javax.servlet.GenericServlet#init()
052: */
053: public void init() throws ServletException {
054:
055: initServlet();
056: if (mappingFormat == null) {
057: //servletMapping = "*.jspe";
058: mappingFormat = new MessageFormat("{0}.jspe");
059: }
060: String bd = getInitParameter("baseDir");
061: if (bd != null && bd.trim().length() > 0) {
062: baseDir = bd;
063: if (!baseDir.startsWith(URL_SEPARATOR))
064: baseDir = URL_SEPARATOR + baseDir;
065: if (!baseDir.endsWith(URL_SEPARATOR))
066: baseDir += URL_SEPARATOR;
067: }
068:
069: String cp = getInitParameter("container");
070: if (cp != null && cp.trim().length() > 0) {
071: if (!cp.startsWith(URL_SEPARATOR))
072: cp = baseDir + URL_SEPARATOR + cp;
073: } else
074: log("FATAL: cannot read parameter container's value, must assign a valid jsp");
075:
076: String pn = getInitParameter("paramName");
077: if (pn != null && pn.trim().length() > 0)
078: paramName = pn;
079:
080: StringBuffer newPage = new StringBuffer(64);
081: newPage.append(cp);
082: newPage.append('?');
083: newPage.append(paramName);
084: newPage.append('=');
085:
086: prefix = newPage.toString();
087: }
088:
089: /* (non-Javadoc)
090: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
091: */
092: protected void doGet(HttpServletRequest req, HttpServletResponse res)
093: throws ServletException, IOException {
094: res.setLocale(req.getLocale());
095: String jspPage = null;
096: try {
097: jspPage = getForwardPage(req.getServletPath());
098: } catch (ParseException e) {
099: String sPath = (String) req
100: .getAttribute(Globals.ACTION_PATH_KEY);
101: if (sPath != null) {
102: int param_idx = sPath.indexOf('?');
103: if (param_idx != -1)
104: sPath = sPath.substring(0, param_idx);
105: try {
106: jspPage = getForwardPage(sPath);
107: } catch (ParseException ee) {
108: log("parse forward path from action failed.", ee);
109: }
110: }
111: }
112: //检查JSP文件是否存在
113: if (jspPage.startsWith(URL_SEPARATOR))
114: jspPage = jspPage.substring(URL_SEPARATOR.length());
115: StringBuffer jspPath = new StringBuffer(baseDir);
116: jspPath.append(jspPage);
117: if (!isJspExists(jspPath.toString())) {
118: res.sendError(HttpServletResponse.SC_NOT_FOUND);
119: return;
120: }
121: jspPath = null;
122: //跳到新的界面
123: StringBuffer newPage = new StringBuffer(prefix);
124: newPage.append(jspPage);
125:
126: String url = newPage.toString();
127: getServletContext().getRequestDispatcher(url).include(req, res);
128: newPage = null;
129: }
130:
131: protected String getForwardPage(String path) throws ParseException {
132: Object[] ps = mappingFormat.parse(path);
133: return ps[0] + ".jsp";
134: }
135:
136: /* (non-Javadoc)
137: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
138: */
139: protected void doPost(HttpServletRequest req,
140: HttpServletResponse res) throws ServletException,
141: IOException {
142: this .doGet(req, res);
143: }
144:
145: /**
146: * 判断某个JSP文件是否存在
147: * @param jsp
148: * @return
149: */
150: protected boolean isJspExists(String jsp) {
151: String webpath = getServletContext().getRealPath(jsp);
152: if (!pages.contains(webpath)) {
153: boolean exists = new File(webpath).exists();
154: if (exists)
155: pages.add(webpath);
156: return exists;
157: }
158: return true;
159: }
160:
161: private List pages = new Vector();
162:
163: public void destroy() {
164: if (pages != null) {
165: pages.clear();
166: pages = null;
167: }
168: }
169:
170: /**
171: * <p>Initialize the servlet mapping under which our controller servlet
172: * is being accessed. This will be used in the <code>&html:form></code>
173: * tag to generate correct destination URLs for form submissions.</p>
174: *
175: * @throws ServletException if error happens while scanning web.xml
176: */
177: protected void initServlet() throws ServletException {
178: // Prepare a Digester to scan the web application deployment descriptor
179: Digester digester = new Digester();
180: digester.push(this );
181: //digester.
182: digester.setNamespaceAware(true);
183: digester.setValidating(false);
184:
185: // Register our local copy of the DTDs that we can find
186: for (int i = 0; i < registrations.length; i += 2) {
187: URL url = this .getClass().getResource(registrations[i + 1]);
188: if (url != null) {
189: digester.register(registrations[i], url.toString());
190: }
191: }
192:
193: // Configure the processing rules that we need
194: digester.addCallMethod("web-app/servlet-mapping",
195: "addServletMapping", 2);
196: digester
197: .addCallParam("web-app/servlet-mapping/servlet-name", 0);
198: digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
199:
200: InputStream input = getServletContext().getResourceAsStream(
201: "/WEB-INF/web.xml");
202:
203: if (input == null)
204: throw new ServletException("Cannot read web.xml");
205:
206: try {
207: digester.parse(input);
208: } catch (IOException e) {
209: throw new ServletException(e);
210: } catch (SAXException e) {
211: throw new ServletException(e);
212: } finally {
213: try {
214: input.close();
215: } catch (IOException e) {
216: throw new ServletException(e);
217: }
218: }
219: }
220:
221: /**
222: * <p>Remember a servlet mapping from our web application deployment
223: * descriptor, if it is for this servlet.</p>
224: *
225: * @param servletName The name of the servlet being mapped
226: * @param urlPattern The URL pattern to which this servlet is mapped
227: */
228: public void addServletMapping(String servletName, String urlPattern) {
229: if (servletName == null)
230: return;
231: if (servletName.equals(getServletConfig().getServletName())) {
232: String servletMapping = StringUtils.replace(urlPattern,
233: "*", "{0}");
234: mappingFormat = new MessageFormat(servletMapping);
235: }
236: }
237:
238: /**
239: * <p>The set of public identifiers, and corresponding resource names, for
240: * the versions of the configuration file DTDs that we know about. There
241: * <strong>MUST</strong> be an even number of Strings in this list!</p>
242: */
243: protected String registrations[] = {
244: "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN",
245: "/org/apache/struts/resources/struts-config_1_0.dtd",
246: "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
247: "/org/apache/struts/resources/struts-config_1_1.dtd",
248: "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
249: "/org/apache/struts/resources/struts-config_1_2.dtd",
250: "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",
251: "/org/apache/struts/resources/web-app_2_2.dtd",
252: "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
253: "/org/apache/struts/resources/web-app_2_3.dtd" };
254:
255: }
|