1 16 package dlog4j; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.text.MessageFormat ; 23 import java.text.ParseException ; 24 import java.util.Vector ; 25 import java.util.List ; 26 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.digester.Digester; 33 import org.apache.commons.lang.StringUtils; 34 import org.xml.sax.SAXException ; 35 36 41 public class EmbedPageServlet extends HttpServlet { 42 43 public final static String URL_SEPARATOR = "/"; 44 protected MessageFormat mappingFormat; 45 protected String paramName = "main"; 46 protected String baseDir = "/WEB-INF/jsp/"; 47 48 private String prefix ; 49 50 53 public void init() throws ServletException { 54 55 initServlet(); 56 if(mappingFormat==null){ 57 mappingFormat = new MessageFormat ("{0}.jspe"); 59 } 60 String 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 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 pn = getInitParameter("paramName"); 78 if(pn!=null&&pn.trim().length()>0) 79 paramName = pn; 80 81 StringBuffer newPage = new StringBuffer (64); 82 newPage.append(cp); 83 newPage.append('?'); 84 newPage.append(paramName); 85 newPage.append('='); 86 87 prefix = newPage.toString(); 88 } 89 90 93 protected void doGet(HttpServletRequest req, HttpServletResponse res) 94 throws ServletException , IOException 95 { 96 res.setLocale(req.getLocale()); 97 String jspPage = null; 98 try{ 99 jspPage = getForwardPage(req.getServletPath()); 100 }catch(ParseException e){ 101 String sPath = (String )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 ee){ 109 log("parse forward path from action failed.",ee); 110 } 111 } 112 } 113 if(jspPage.startsWith(URL_SEPARATOR)) 115 jspPage = jspPage.substring(URL_SEPARATOR.length()); 116 StringBuffer jspPath = new StringBuffer (baseDir); 117 jspPath.append(jspPage); 118 if(!isJspExists(jspPath.toString())){ 119 res.sendError(HttpServletResponse.SC_NOT_FOUND); 120 return; 121 } 122 jspPath = null; 123 StringBuffer newPage = new StringBuffer (prefix); 125 newPage.append(jspPage); 126 127 String url = newPage.toString(); 128 getServletContext().getRequestDispatcher(url).include(req,res); 129 newPage = null; 130 } 131 132 protected String getForwardPage(String path) throws ParseException { 133 Object [] ps = mappingFormat.parse(path); 134 return ps[0] + ".jsp"; 135 } 136 137 140 protected void doPost(HttpServletRequest req, HttpServletResponse res) 141 throws ServletException , IOException { 142 this.doGet(req, res); 143 } 144 145 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 176 protected void initServlet() throws ServletException { 177 Digester digester = new Digester(); 179 digester.push(this); 180 digester.setNamespaceAware(true); 182 digester.setValidating(false); 183 184 for (int i = 0; i < registrations.length; i += 2) { 186 URL url = this.getClass().getResource(registrations[i+1]); 187 if (url != null) { 188 digester.register(registrations[i], url.toString()); 189 } 190 } 191 192 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 input = getServletContext().getResourceAsStream("/WEB-INF/web.xml"); 199 200 if(input==null) 201 throw new ServletException ("Cannot read web.xml"); 202 203 try { 204 digester.parse(input); 205 } catch (IOException e) { 206 throw new ServletException (e); 207 } catch (SAXException e) { 208 throw new ServletException (e); 209 } finally { 210 try { 211 input.close(); 212 } catch (IOException e) { 213 throw new ServletException (e); 214 } 215 } 216 } 217 224 public void addServletMapping(String servletName, String urlPattern) { 225 if (servletName == null) 226 return; 227 if (servletName.equals(getServletConfig().getServletName())) { 228 String servletMapping = StringUtils.replace(urlPattern, "*", "{0}"); 229 mappingFormat = new MessageFormat (servletMapping); 230 } 231 } 232 233 238 protected String 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
|