001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.jsp;
006:
007: import java.io.FileReader;
008: import java.io.FileNotFoundException;
009: import java.io.IOException;
010: import java.io.PrintWriter;
011:
012: import java.lang.IllegalArgumentException;
013:
014: import java.util.Enumeration;
015: import java.util.Hashtable;
016: import java.util.logging.LogRecord;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019:
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.ServletException;
022: import javax.servlet.ServletResponse;
023: import javax.servlet.ServletRequest;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.servlet.http.HttpUtils;
028:
029: import com.sun.portal.providers.context.ProviderContext;
030:
031: import com.sun.portal.desktop.context.ProviderContextThreadLocalizer;
032: import com.sun.portal.providers.ProviderException;
033: import com.sun.portal.log.common.PortalLogger;
034:
035: /*
036: * The JspRequestDispatcher represents the RequestDispatcher for the
037: * JSPProvider JspServletEnvironment. It implements the include and
038: * forward functionality.
039: *
040: **/
041:
042: class JspRequestDispatcher implements RequestDispatcher {
043:
044: private JspServletEnvironment jspEnv;
045: private ProviderContext providerContext;
046: private String path;
047: private String baseuri;
048: private static Logger logger = PortalLogger
049: .getLogger(JspRequestDispatcher.class);
050:
051: /*
052: * Constructor
053: *
054: * @param path The path of the requested resource. Format:
055: * /resource
056: * /resource?qstring
057: * @param jspEnv The JspServletEnvironment of the parent resource
058: */
059: public JspRequestDispatcher(String path,
060: JspServletEnvironment jspEnv) {
061: this .path = path;
062: this .baseuri = path;
063: this .jspEnv = jspEnv;
064: this .providerContext = ProviderContextThreadLocalizer.get();
065:
066: int index = path.indexOf('?');
067:
068: if (index != -1) {
069: baseuri = path.substring(0, index);
070: }
071: }
072:
073: /*
074: * Include a server resource.
075: *
076: * If the resource is a local reference to a JSP, e.g. "/foo.jsp",
077: * then the request is processed within the JSPProvider's
078: * ServletEnvironment and the resource is retrieved from the same
079: * area that the JSPProvider looks for files.
080: *
081: * If the resource is a local reference to a non-JSP file,
082: * e.g. "/foo.html", then content of the static file is retrieved and
083: * returned as is.
084: *
085: *
086: * @param request ServletRequest object
087: * @param response ServletResponse object
088: */
089: public void include(ServletRequest request, ServletResponse response)
090: throws ServletException, IOException {
091:
092: if ((path.length() == 0) || (path == null)) {
093: return;
094: }
095:
096: StringBuffer icontent = new StringBuffer();
097:
098: try {
099: // JSP Server Resource
100: //
101: if (baseuri.endsWith(".jsp")) {
102: icontent = getJspResource(request, response);
103:
104: // Static Server Resource
105: //
106: } else {
107: icontent = getStaticResource();
108: }
109: } catch (ServletException se) {
110: if (logger.isLoggable(Level.INFO)) {
111: LogRecord record = new LogRecord(Level.INFO,
112: "PSDT_CSPPJ0010");
113: record.setLoggerName(logger.getName());
114: record.setParameters(new Object[] { baseuri });
115: record.setThrown(se);
116: logger.log(record);
117: }
118: throw se;
119: }
120:
121: if (icontent != null) {
122: PrintWriter pw = response.getWriter();
123: pw.print(icontent.toString());
124: pw.flush();
125: }
126: }
127:
128: /*
129: * Forward to a server resource.
130: *
131: * If the resource is a local reference to a JSP, e.g. "/foo.jsp",
132: * then the request is processed within the JSPProvider's
133: * ServletEnvironment and the resource is retrieved from the same
134: * area that the JSPProvider looks for files.
135: *
136: * If the resource is a local reference to a non-JSP file,
137: * e.g. "/foo.html", then content of the static file is retrieved and
138: * returned as is.
139: *
140: *
141: * @param request ServletRequest object
142: * @param response ServletResponse object
143: */
144: public void forward(ServletRequest request, ServletResponse response)
145: throws ServletException, IOException {
146:
147: include(request, response);
148: }
149:
150: /**
151: * Retrieve JSP Resource from server
152: *
153: * URI Format: /foo.jsp
154: * URI Format: /foo.jsp?parm1=value1
155: * URI Format: /foo.jsp?parm1=value1&parm2=value2
156: *
157: * @return StringBuffer content from JSP server resource
158: */
159: private StringBuffer getJspResource(ServletRequest request,
160: ServletResponse response) throws ServletException {
161:
162: Hashtable parameters = getRequestParameters(request);
163: HttpServletRequest hrequest = (HttpServletRequest) request;
164: HttpServletResponse hresponse = (HttpServletResponse) response;
165: StringBuffer buffer = new StringBuffer();
166:
167: JSPProvider jp = (JSPProvider) hrequest
168: .getAttribute(JspFactoryImpl.providerKey);
169:
170: if (jp != null) {
171: try {
172: buffer = jp.includeJspPage(path, parameters, hrequest,
173: hresponse);
174: } catch (ProviderException pe) {
175: logger.log(Level.FINE, "PSDT_CSPPJ0013", new Object[] {
176: path, parameters });
177: throw new ServletException("Problem processing JSP: "
178: + path, pe);
179: } catch (Exception e) {
180: logger.log(Level.FINE, "PSDT_CSPPJ0013", new Object[] {
181: path, parameters });
182: throw new ServletException("Problem processing JSP: "
183: + path, e);
184: }
185: }
186:
187: return buffer;
188: }
189:
190: /**
191: * Retrieve Static resource from server
192: *
193: * URI Format: /foo.html
194: * URI Format: /foo.html?qstring
195: *
196: * @return StringBuffer content from static server resource
197: */
198:
199: private StringBuffer getStaticResource() throws ServletException {
200:
201: StringBuffer buffer = new StringBuffer();
202:
203: try {
204: FileReader fr = new FileReader(jspEnv.getRealPath(path));
205: char c[] = new char[4096];
206: int read = 0;
207:
208: while ((read = fr.read(c)) != -1) {
209: buffer.append(c, 0, read);
210: }
211:
212: fr.close();
213:
214: } catch (FileNotFoundException fnf) {
215: if (logger.isLoggable(Level.FINE)) {
216: LogRecord record = new LogRecord(Level.FINE,
217: "PSDT_CSPPJ0014");
218: record.setLoggerName(logger.getName());
219: record.setParameters(new Object[] { path });
220: record.setThrown(fnf);
221: logger.log(record);
222: }
223: throw new ServletException("File Not Found: " + path, fnf);
224: } catch (IOException ioe) {
225: if (logger.isLoggable(Level.FINE)) {
226: LogRecord record = new LogRecord(Level.FINE,
227: "PSDT_CSPPJ0014");
228: record.setLoggerName(logger.getName());
229: record.setParameters(new Object[] { path });
230: record.setThrown(ioe);
231: logger.log(record);
232: }
233: throw new ServletException("Problem processing file: "
234: + path, ioe);
235: } catch (Exception e) {
236: if (logger.isLoggable(Level.FINE)) {
237: LogRecord record = new LogRecord(Level.FINE,
238: "PSDT_CSPPJ0014");
239: record.setLoggerName(logger.getName());
240: record.setParameters(new Object[] { path });
241: record.setThrown(e);
242: logger.log(record);
243: }
244: throw new ServletException("Problem processing file: "
245: + path, e);
246: }
247:
248: return buffer;
249: }
250:
251: /**
252: * Retrieve parameters from both the ServletRequest and the
253: * query string.
254: *
255: * @return Hashtable of request parameters
256: */
257: private Hashtable getRequestParameters(ServletRequest req)
258: throws ServletException {
259:
260: Hashtable parameters = new Hashtable();
261: int index = path.indexOf('?');
262: String qstring = null;
263:
264: if (index != -1) {
265: qstring = path.substring(index + 1);
266:
267: try {
268: parameters = HttpUtils.parseQueryString(qstring);
269: } catch (IllegalArgumentException e) {
270: if (logger.isLoggable(Level.FINE)) {
271: LogRecord record = new LogRecord(Level.FINE,
272: "PSDT_CSPPJ0015");
273: record.setLoggerName(logger.getName());
274: record.setParameters(new Object[] { path });
275: record.setThrown(e);
276: logger.log(record);
277: }
278: throw new ServletException(
279: "Problem parsing query string:" + qstring, e);
280: } catch (Exception e) {
281: if (logger.isLoggable(Level.FINE)) {
282: LogRecord record = new LogRecord(Level.FINE,
283: "PSDT_CSPPJ0015");
284: record.setLoggerName(logger.getName());
285: record.setParameters(new Object[] { path });
286: record.setThrown(e);
287: logger.log(record);
288: }
289: throw new ServletException(
290: "Problem parsing query string:" + qstring, e);
291: }
292:
293: }
294:
295: for (Enumeration pe = req.getParameterNames(); pe
296: .hasMoreElements();) {
297: String pname = (String) pe.nextElement();
298: parameters.put(pname, req.getParameterValues(pname));
299: }
300:
301: return parameters;
302: }
303:
304: }
|