1 23 package com.sun.enterprise.tools.admingui.servlet; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.InputStream ; 28 import java.util.Date ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletResponse ; 33 import com.sun.enterprise.tools.admingui.util.Util; 34 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 35 36 import java.util.StringTokenizer ; 38 39 42 import org.apache.jasper.compiler.JspUtil; 43 44 45 48 public class LockhartContentSource implements DownloadServlet.ContentSource { 49 50 57 public String getId() { 58 return "Lockhart"; } 60 61 69 public InputStream getInputStream(DownloadServlet.Context ctx) { 70 HttpServletRequest request = 71 (HttpServletRequest ) ctx.getServletRequest(); 72 if (isJSP(request)) { 73 serveJSPPage(ctx); 74 ctx.setAttribute("JSP_PAGE_SERVED", "true"); 76 77 return null; 78 } 79 80 String pathInfo = request.getPathInfo(); 81 if (pathInfo == null || pathInfo.length() == 0) { 82 return null; 83 } 84 85 String servletPath = request.getServletPath(); 86 int index = pathInfo.lastIndexOf('.'); 87 String extnsn = pathInfo.substring(index + 1); 89 ctx.setAttribute(DownloadServlet.EXTENSION, extnsn); InputStream is = getClass().getClassLoader().getResourceAsStream(servletPath.substring(1)+pathInfo); 91 92 ctx.setAttribute("filePath", is); 93 ctx.setAttribute("JSP_PAGE_SERVED", "false"); 94 95 return is; 97 } 98 99 104 public boolean isJSP(HttpServletRequest request) { 105 String uri = request.getRequestURI(); 106 String pathInfo = request.getPathInfo(); 107 if ((uri.indexOf("jsp") > -1) && pathInfo.endsWith(".jsp")) { 108 return true; 109 } 110 return false; 111 } 112 113 118 public void cleanUp(DownloadServlet.Context ctx) { 119 InputStream is = 121 (InputStream ) ctx.getAttribute("filePath"); if (is != null) { 124 try { 125 is.close(); 126 } catch (Exception ex) { 127 } 129 } 130 ctx.removeAttribute("filePath"); } 132 133 138 public long getLastModified(DownloadServlet.Context context) { 139 if (isJSP((HttpServletRequest ) context.getServletRequest())) { 140 return -1; 142 } 143 144 return DEFAULT_MODIFIED_DATE; 146 } 147 148 private void serveJSPPage(DownloadServlet.Context ctx) { 149 HttpServletRequest request = (HttpServletRequest )ctx.getServletRequest(); 150 HttpServletResponse response = (HttpServletResponse )ctx.getServletResponse(); 151 String pathInfo = (String )request.getAttribute(PATH_INFO_ATTRIBUTE); 152 String servletPath = (String )request.getAttribute(SERVLET_PATH_ATTRIBUTE); 153 154 if(servletPath == null) { 155 servletPath = request.getServletPath(); 156 pathInfo = request.getPathInfo(); 157 } 158 String className = getClassName(servletPath, pathInfo); 159 160 try { 161 Class cls = Class.forName(className); 162 Object obj = cls.newInstance(); 163 ((HttpServlet )obj).init(ctx.getServletConfig()); 164 ((HttpServlet )obj).service(request, response); 165 } catch(Exception ex) { 166 throw new FrameworkException( 167 "Unabled to handle pre-compiled JSP '"+servletPath+pathInfo+ 168 "'. Expected pre-compiled classname: '"+className+"'.", ex); 169 } 170 } 171 172 177 protected String getClassName(String servletPath, String jspName) { 178 return getClassName(servletPath+jspName); 179 } 180 181 182 186 protected String getClassName(String fullPath) { 187 fullPath = fullPath.trim(); 188 int lastSlash = fullPath.lastIndexOf('/'); 189 if (lastSlash == -1) { 190 return JSP_CLASS_PREFIX+JspUtil.makeJavaIdentifier(fullPath); 193 } 194 195 String packageName = fullPath.substring(0, ++lastSlash); 197 198 String jspName = fullPath.substring(lastSlash); 200 201 for (int loc=packageName.indexOf("//"); loc != -1; 204 loc=packageName.indexOf("//")) { 205 packageName = packageName.replaceAll("//", "/"); 206 } 207 208 if (packageName.startsWith("/")) { 210 packageName = packageName.substring(1); 211 } 212 213 StringTokenizer tok = new StringTokenizer (packageName, "/"); 215 StringBuffer className = new StringBuffer (JSP_CLASS_PREFIX); 216 while (tok.hasMoreTokens()) { 217 className.append(JspUtil.makeJavaIdentifier(tok.nextToken())); 219 className.append('.'); 220 } 221 222 className.append(JspUtil.makeJavaIdentifier(jspName)); 224 225 if (Util.isLoggableFINER()) { 226 Util.logFINER("CLASSNAME = "+className); 227 } 228 229 return className.toString(); 231 } 232 233 238 private static final String PATH_INFO_ATTRIBUTE = 239 "javax.servlet.include.path_info"; 240 241 246 private static final String SERVLET_PATH_ATTRIBUTE = 247 "javax.servlet.include.servlet_path"; 248 249 252 private static final String JSP_CLASS_PREFIX = "org.apache.jsp."; 253 254 258 static final long DEFAULT_MODIFIED_DATE = (new Date ()).getTime(); 259 } 260 | Popular Tags |