001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: import com.salmonllc.properties.Props;
023:
024: import java.lang.reflect.Method;
025: import java.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.*;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.servlet.http.*;
032:
033: /**
034: * This class is a wrapper used by internal processes in the framework and isn't intended to be used directly. For more information see HttpServletResponse in the JSDK documentation.
035: */
036:
037: public class HttpServletResponseWrapper implements HttpServletResponse {
038: HttpServletResponse _res;
039: ActionResponse _pres;
040: ActionRequest _preq;
041: HtmlPageBase _page;
042: boolean _redirectSent = false;
043:
044: /**
045: * HttpServletResponseWrapper constructor comment.
046: */
047: public HttpServletResponseWrapper(HttpServletResponse res,
048: HtmlPageBase page) {
049: super ();
050: _res = res;
051: _page = page;
052: }
053:
054: /**
055: * HttpServletResponseWrapper constructor comment.
056: */
057: public HttpServletResponseWrapper(ActionResponse res,
058: ActionRequest req, HtmlPageBase page) {
059: super ();
060: _pres = res;
061: _preq = req;
062: _page = page;
063: }
064:
065: /**
066: * addCookie method comment.
067: */
068: public void addCookie(Cookie arg1) {
069: if (_pres == null)
070: _res.addCookie(arg1);
071: }
072:
073: /**
074: * containsHeader method comment.
075: */
076: public boolean containsHeader(String arg1) {
077: if (_pres != null)
078: return false;
079: else
080: return _res.containsHeader(arg1);
081: }
082:
083: /**
084: * encodeRedirectUrl method comment.
085: */
086: /** @deprecated */
087: public String encodeRedirectUrl(String arg1) {
088: if (_pres != null)
089: return _pres.encodeURL(arg1);
090: else
091: return _res.encodeRedirectURL(arg1);
092: }
093:
094: /**
095: * encodeUrl method comment.
096: */
097: /** @deprecated */
098: public String encodeUrl(String arg1) {
099: if (_pres != null)
100: return _pres.encodeURL(arg1);
101: else
102: return _res.encodeURL(arg1);
103: }
104:
105: /**
106: * getCharacterEncoding method comment.
107: */
108: public String getCharacterEncoding() {
109: if (_pres != null)
110: return _preq.getCharacterEncoding();
111: else
112: return _res.getCharacterEncoding();
113: }
114:
115: /**
116: * getOutputStream method comment.
117: */
118: public javax.servlet.ServletOutputStream getOutputStream()
119: throws java.io.IOException {
120: if (_pres != null)
121: return null;
122: else
123: return _res.getOutputStream();
124: }
125:
126: /**
127: * This method was created in VisualAge.
128: */
129: public boolean getRedirectSent() {
130: return _redirectSent;
131: }
132:
133: /**
134: * getWriter method comment.
135: */
136: public java.io.PrintWriter getWriter() throws java.io.IOException {
137: if (_pres != null)
138: return null;
139: else
140: return _res.getWriter();
141: }
142:
143: /**
144: * This method was created in VisualAge.
145: * @return java.lang.String
146: * @param pathInfo java.lang.String
147: * @param restOfURL java.lang.String
148: */
149: private String parseURL(String servletPath, String pathInfo,
150: String restOfURL) {
151: if (restOfURL.startsWith("/")
152: && (!_page
153: .getPageProperties()
154: .getBooleanProperty(
155: Props.SYS_ABSOLUTE_URLS_TO_RELATIVE_ON_REDIRECT,
156: false))) {
157: return restOfURL;
158: }
159:
160: if (pathInfo == null)
161: pathInfo = "";
162:
163: String url = servletPath + pathInfo + "/../" + restOfURL;
164: Vector paths = new Vector();
165: StringTokenizer tok = new StringTokenizer(url, "/");
166: String token = null;
167: while (tok.hasMoreTokens()) {
168: token = tok.nextToken();
169: if (token.equals("..")) {
170: if (paths.size() > 0)
171: paths.setSize(paths.size() - 1);
172: } else {
173: paths.addElement(token);
174: }
175: }
176:
177: StringBuffer ret = new StringBuffer(url.length());
178: for (int i = 0; i < paths.size(); i++) {
179: token = (String) paths.elementAt(i);
180: ret.append('/');
181: ret.append(token);
182: }
183:
184: return ret.toString();
185: }
186:
187: /**
188: * sendError method comment.
189: */
190: public void sendError(int arg1) throws java.io.IOException {
191: if (_pres == null)
192: _res.sendError(arg1);
193: }
194:
195: /**
196: * sendError method comment.
197: */
198: public void sendError(int arg1, String arg2)
199: throws java.io.IOException {
200: if (_pres == null)
201: _res.sendError(arg1, arg2);
202: }
203:
204: /**
205: * sendRedirect method comment.
206: */
207: public void sendRedirect(String url) throws java.io.IOException {
208: sendRedirect(url, true);
209: }
210:
211: /**
212: * sendRedirect method comment.
213: */
214: public void sendRedirect(String url, boolean setFlag)
215: throws java.io.IOException {
216: //Following code has to be remain in active. If following "if" statement is commented out,
217: //it will generate problems in isRefferedByCurrentPage() method. Example;
218: //go to page "A" then page "B" which is the child of "A". Than go back to page "A" again. In that case
219: //isReferredByCurrentPage() method return true. Since the "_lastReferer" is not cleared, method
220: // execute the "...else referer = _lastReferer;" line, which makes the referer and the referred the same page.
221: if (_page instanceof HtmlPage && setFlag)
222: ((HtmlPage) _page).clearCurrentPageReferer();
223:
224: if (_pres != null) {
225: if (setFlag)
226: _redirectSent = true;
227: _pres.sendRedirect(url);
228: return;
229: }
230:
231: try {
232: new URL(url);
233: } catch (Exception e) {
234: //its a partial url, construct a full url
235: HttpServletRequest req = _page.getCurrentRequest();
236:
237: StringBuffer buf = new StringBuffer(_page.getServerURL());
238:
239: buf.append(parseURL(_page.getServletPath(), req
240: .getPathInfo(), url));
241:
242: url = buf.toString();
243: }
244:
245: if (setFlag)
246: _redirectSent = true;
247:
248: if (_page instanceof HtmlPage
249: && ((HtmlPage) _page).isWMLMaintained())
250: _res.sendRedirect(url);
251: else
252: _res.sendRedirect(encodeURL(url, _page.getCurrentRequest(),
253: this ));
254:
255: }
256:
257: /**
258: * setContentLength method comment.
259: */
260: public void setContentLength(int arg1) {
261: if (_pres == null)
262: _res.setContentLength(arg1);
263: }
264:
265: /**
266: * setContentType method comment.
267: */
268: public void setContentType(String arg1) {
269: if (_pres == null)
270: _res.setContentType(arg1);
271: }
272:
273: /**
274: * setDateHeader method comment.
275: */
276: public void setDateHeader(String arg1, long arg2) {
277: if (_pres == null)
278: _res.setDateHeader(arg1, arg2);
279: }
280:
281: /**
282: * setHeader method comment.
283: */
284: public void setHeader(String arg1, String arg2) {
285: if (_pres == null)
286: _res.setHeader(arg1, arg2);
287: }
288:
289: /**
290: * setIntHeader method comment.
291: */
292: public void setIntHeader(String arg1, int arg2) {
293: if (_pres == null)
294: _res.setIntHeader(arg1, arg2);
295: }
296:
297: /**
298: * This method was created in VisualAge.
299: * @param sent boolean
300: */
301: void setRedirectSent(boolean sent) {
302: _redirectSent = sent;
303: }
304:
305: /**
306: * This method was created in VisualAge.
307: * @param res javax.servlet.http.HttpServletResponse
308: */
309: void setResponse(HttpServletResponse res) {
310: _res = res;
311: _pres = null;
312: }
313:
314: void setResponse(ActionResponse res, ActionRequest req) {
315: _res = null;
316: _pres = res;
317: _preq = req;
318: }
319:
320: /**
321: * setStatus method comment.
322: */
323: public void setStatus(int arg1) {
324: if (_pres == null)
325: _res.setStatus(arg1);
326: }
327:
328: /**
329: * setStatus method comment.
330: */
331: /** @deprecated */
332: public void setStatus(int arg1, String arg2) {
333: if (_pres == null) {
334: try {
335: _res.sendError(arg1, arg2);
336: } catch (Exception e) {
337: }
338: }
339: }
340:
341: /**
342: * encodeRedirectUrl method comment.
343: */
344: public String encodeRedirectURL(String arg1) {
345: if (_pres != null)
346: return _pres.encodeURL(arg1);
347: else
348: return _res.encodeRedirectURL(arg1);
349: }
350:
351: /**
352: * encodeUrl method comment.
353: */
354: public String encodeURL(String arg1) {
355: if (_pres != null)
356: return _pres.encodeURL(arg1);
357: else
358: return _res.encodeURL(arg1);
359: }
360:
361: /**
362: * encodeUrl method, a bit more well behaved than some of the J2EE default implementations.
363: */
364: public static String encodeURL(String arg1, HttpServletRequest req,
365: HttpServletResponse res) {
366: if (doEncode(arg1, req))
367: return res.encodeURL(arg1);
368: else
369: return arg1;
370: }
371:
372: public void addIntHeader(String header, int value) {
373: if (_pres == null)
374: _res.addIntHeader(header, value);
375: }
376:
377: public void addHeader(String header, String value) {
378: if (_pres == null)
379: _res.addHeader(header, value);
380: }
381:
382: public void addDateHeader(String header, long value) {
383: if (_pres == null)
384: _res.addDateHeader(header, value);
385: }
386:
387: public void setLocale(Locale l) {
388: if (_pres == null)
389: _res.setLocale(l);
390: }
391:
392: public void setBufferSize(int size) {
393: if (_pres == null)
394: _res.setBufferSize(size);
395: }
396:
397: public void reset() {
398: if (_pres == null)
399: _res.reset();
400: }
401:
402: public boolean isCommitted() {
403: if (_pres == null)
404: return _res.isCommitted();
405: else
406: return false;
407: }
408:
409: public Locale getLocale() {
410: if (_pres == null)
411: return _res.getLocale();
412: else
413: return _preq.getLocale();
414: }
415:
416: public int getBufferSize() {
417: if (_pres == null)
418: return _res.getBufferSize();
419: else
420: return -1;
421: }
422:
423: public void flushBuffer() throws java.io.IOException {
424: if (_pres == null)
425: _res.flushBuffer();
426: }
427:
428: public void resetBuffer() {
429: }
430:
431: private static boolean doEncode(String loc, HttpServletRequest req) {
432: if (loc.startsWith("javascript"))
433: return false;
434: if (req.isRequestedSessionIdFromCookie())
435: return false;
436: URL url = toURL(loc, req);
437: if (!req.getScheme().equalsIgnoreCase(url.getProtocol()))
438: return false;
439: if (!req.getServerName().equalsIgnoreCase(url.getHost()))
440: return false;
441: int serverPort = req.getServerPort();
442: if (serverPort == -1) {
443: if ("https".equals(req.getScheme()))
444: serverPort = 443;
445: else
446: serverPort = 80;
447: }
448: int urlPort = url.getPort();
449: if (urlPort == -1) {
450: if ("https".equals(url.getProtocol()))
451: urlPort = 443;
452: else
453: urlPort = 80;
454: }
455: if (serverPort != urlPort)
456: return (false);
457: return true;
458: }
459:
460: private static URL toURL(String location, HttpServletRequest req) {
461: if (location == null)
462: return (null);
463:
464: URL url = null;
465: try {
466: url = new URL(location);
467: } catch (MalformedURLException e1) {
468: String requrl = HttpUtils.getRequestURL(req).toString();
469: try {
470: url = new URL(new URL(requrl), location);
471: } catch (MalformedURLException e2) {
472: throw new IllegalArgumentException(location);
473: }
474: }
475: return url;
476:
477: }
478:
479: /* (non-Javadoc)
480: * @see javax.servlet.ServletResponse#getContentType()
481: */
482: public String getContentType() {
483: try {
484: Method m = _res.getClass()
485: .getMethod("getContentType", null);
486: return (String) m.invoke(this , null);
487: } catch (Exception e) {
488: return null;
489: }
490: }
491:
492: /* (non-Javadoc)
493: * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
494: */
495: public void setCharacterEncoding(String arg0) {
496: Class c[] = { String.class };
497: Object o[] = { arg0 };
498: try {
499: Method m = _res.getClass().getMethod(
500: "setCharacterEncoding", c);
501: m.invoke(this , o);
502: } catch (Exception e) {
503: }
504: }
505:
506: }
|