KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > servlet > LockhartContentSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.admingui.servlet;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import com.sun.enterprise.tools.admingui.util.Util;
34 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
35
36 //import java.io.*;
37
import java.util.StringTokenizer JavaDoc;
38
39 //import javax.servlet.*;
40
//import javax.servlet.http.*;
41

42 import org.apache.jasper.compiler.JspUtil;
43
44
45 /**
46  *
47  */

48 public class LockhartContentSource implements DownloadServlet.ContentSource {
49
50     /**
51      * <p> This method returns a unique string used to identify this
52      * {@link DownloadServlet#ContentSource}. This string must be
53      * specified in order to select the appropriate
54      * {@link DownloadServlet#ContentSource} when using the
55      * {@link DownloadServlet}.</p>
56      */

57     public String JavaDoc getId() {
58     return "Lockhart"; // NOI18N
59
}
60
61     /**
62      * <p> This method is responsible for generating the content and
63      * returning an InputStream to that content. It is also
64      * responsible for setting any attribute values in the
65      * {@link DownloadServlet#Context}, such as
66      * {@link DownloadServlet#EXTENSION} or
67      * {@link DownloadServlet#CONTENT_TYPE}.</p>
68      */

69     public InputStream JavaDoc getInputStream(DownloadServlet.Context ctx) {
70     HttpServletRequest JavaDoc request =
71         (HttpServletRequest JavaDoc) ctx.getServletRequest();
72     if (isJSP(request)) {
73         serveJSPPage(ctx);
74         //This attribute is used to decide whether to send error code, or not.
75
ctx.setAttribute("JSP_PAGE_SERVED", "true");
76
77         return null;
78     }
79
80     String JavaDoc pathInfo = request.getPathInfo();
81     if (pathInfo == null || pathInfo.length() == 0) {
82         return null;
83     }
84
85     String JavaDoc servletPath = request.getServletPath();
86     int index = pathInfo.lastIndexOf('.');
87     // Set the extension so it can be mapped to a MIME type
88
String JavaDoc extnsn = pathInfo.substring(index + 1);
89     ctx.setAttribute(DownloadServlet.EXTENSION, extnsn); // NOI18N
90
InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(servletPath.substring(1)+pathInfo);
91
92     ctx.setAttribute("filePath", is);
93     ctx.setAttribute("JSP_PAGE_SERVED", "false");
94
95     // Return an InputStream to the tmpFile
96
return is;
97     }
98
99     /**
100      * <p> This method looks at the request information and determines if it
101      * thinks it is a jsp. Essentially it thinks anything that ends with
102      * "jsp" in is a jsp (this is not fool proof).</p>
103      */

104     public boolean isJSP(HttpServletRequest JavaDoc request) {
105     String JavaDoc uri = request.getRequestURI();
106     String JavaDoc pathInfo = request.getPathInfo();
107     if ((uri.indexOf("jsp") > -1) && pathInfo.endsWith(".jsp")) {
108         return true;
109     }
110     return false;
111     }
112
113     /**
114      * <p> This method may be used to clean up any temporary resources. It
115      * will be invoked after the <code>InputStream</code> has been
116      * completely read.</p>
117      */

118     public void cleanUp(DownloadServlet.Context ctx) {
119     // Get the File information
120
InputStream JavaDoc is =
121         (InputStream JavaDoc) ctx.getAttribute("filePath"); // NOI18N
122
// Close the InputStream
123
if (is != null) {
124         try {
125         is.close();
126         } catch (Exception JavaDoc ex) {
127         // Ignore...
128
}
129     }
130     ctx.removeAttribute("filePath"); // NOI18N
131
}
132
133     /**
134      * <p> This method is responsible for returning the last modified date of
135      * the content, or -1 if not applicable. This information will be
136      * used for caching.</p>
137      */

138     public long getLastModified(DownloadServlet.Context context) {
139     if (isJSP((HttpServletRequest JavaDoc) context.getServletRequest())) {
140         // Don't cache JSP's
141
return -1;
142     }
143
144     // This will enable caching on all files served through this code path
145
return DEFAULT_MODIFIED_DATE;
146     }
147
148     private void serveJSPPage(DownloadServlet.Context ctx) {
149     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)ctx.getServletRequest();
150     HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)ctx.getServletResponse();
151     String JavaDoc pathInfo = (String JavaDoc)request.getAttribute(PATH_INFO_ATTRIBUTE);
152     String JavaDoc servletPath = (String JavaDoc)request.getAttribute(SERVLET_PATH_ATTRIBUTE);
153
154     if(servletPath == null) {
155         servletPath = request.getServletPath();
156         pathInfo = request.getPathInfo();
157     }
158     String JavaDoc className = getClassName(servletPath, pathInfo);
159
160     try {
161         Class JavaDoc cls = Class.forName(className);
162         Object JavaDoc obj = cls.newInstance();
163         ((HttpServlet JavaDoc)obj).init(ctx.getServletConfig());
164         ((HttpServlet JavaDoc)obj).service(request, response);
165     } catch(Exception JavaDoc 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     /**
173      * This method takes the servlet path and the pathInfo (which should be
174      * the jspName) and generates the class name of the generated JSP. Both
175      * of these Strings are expected to have a leading '/'.
176      */

177     protected String JavaDoc getClassName(String JavaDoc servletPath, String JavaDoc jspName) {
178     return getClassName(servletPath+jspName);
179     }
180
181
182     /**
183      * This method takes the full jsp name and generates the class name for
184      * the generated JSP.
185      */

186     protected String JavaDoc getClassName(String JavaDoc fullPath) {
187     fullPath = fullPath.trim();
188     int lastSlash = fullPath.lastIndexOf('/');
189     if (lastSlash == -1) {
190         // Probably an error, but just in case we'll do this and let it
191
// fail later.
192
return JSP_CLASS_PREFIX+JspUtil.makeJavaIdentifier(fullPath);
193     }
194
195     // The packageName is the full path minus everything after last '/'
196
String JavaDoc packageName = fullPath.substring(0, ++lastSlash);
197
198     // The jspName is everything after the last '/'
199
String JavaDoc jspName = fullPath.substring(lastSlash);
200
201     // Make sure to get rid of any double slashes "//"
202
//This may never happen to our application.
203
for (int loc=packageName.indexOf("//"); loc != -1;
204         loc=packageName.indexOf("//")) {
205         packageName = packageName.replaceAll("//", "/");
206     }
207
208     // Get rid of leading '/'
209
if (packageName.startsWith("/")) {
210         packageName = packageName.substring(1);
211     }
212
213     // Iterate through each part of path and call makeJavaIdentifier
214
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(packageName, "/");
215     StringBuffer JavaDoc className = new StringBuffer JavaDoc(JSP_CLASS_PREFIX);
216     while (tok.hasMoreTokens()) {
217         // Convert .'s to _'s + other conversions
218
className.append(JspUtil.makeJavaIdentifier(tok.nextToken()));
219         className.append('.');
220     }
221
222     // Add on the jsp name
223
className.append(JspUtil.makeJavaIdentifier(jspName));
224
225     if (Util.isLoggableFINER()) {
226         Util.logFINER("CLASSNAME = "+className);
227     }
228
229     // Return the classname
230
return className.toString();
231     }
232
233     /**
234      * When an include() is called the PATH_INFO is set via a Request
235      * attribute named "javax.servlet.include.path_info". This constant is
236      * set to this name.
237      */

238     private static final String JavaDoc PATH_INFO_ATTRIBUTE =
239     "javax.servlet.include.path_info";
240
241     /**
242      * When an include() is called the SERVLET_PATH is set via a Request
243      * attribute named "javax.servlet.include.servlet_path". This constant is
244      * set to this name.
245      */

246     private static final String JavaDoc SERVLET_PATH_ATTRIBUTE =
247     "javax.servlet.include.servlet_path";
248
249     /**
250      * The base package where compiled JSP class files are located.
251      */

252     private static final String JavaDoc JSP_CLASS_PREFIX = "org.apache.jsp.";
253
254     /**
255      * This is the default "Last-Modified" Date. Its value is the
256      * <code>Long</code> representing the initialization time of this class.
257      */

258     static final long DEFAULT_MODIFIED_DATE = (new Date JavaDoc()).getTime();
259 }
260
Popular Tags