001: /*
002: * $Id: HeaderResponse.java,v 1.9 2005/11/30 11:27:21 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/rproxy/connectionhandler/HeaderResponse.java,v $
004: * $Log: HeaderResponse.java,v $
005: * Revision 1.9 2005/11/30 11:27:21 ss150821
006: * 6356996 - Srap Code base needs to save files in the unix file format and not windows
007: *
008: * Revision 1.8 2005/02/24 07:36:44 ss150821
009: * RFE 6223490 - SRA Should use JDK based logging
010: *
011: * Revision 1.7 2005/02/23 09:15:07 ss150821
012: * RFE 6223490 - SRA Should use JDK based logging
013: *
014: * Revision 1.6 2003/09/01 12:11:07 mm132998
015: * Bug ID : 4911706 , 4801456
016: *
017: * Revision 1.5 2002/10/17 12:38:31 bv131302
018: * bringing some early changes of appserver_branch into the tip
019: *
020: * Revision 1.4 2002/09/02 06:37:43 bv131302
021: * CRT#1935 - content-lenth setting
022: *
023: * Revision 1.3 2002/08/21 07:36:19 bv131302
024: * Fixes for 4681457, 4703696, 4713296, 4718232, 4726004, 4726008
025: *
026: * Revision 1.2 2002/08/16 12:19:54 bv131302
027: * Hana CRT#1884 - RProxy perf issues
028: *
029: * Revision 1.1 2002/06/14 09:53:53 rt130506
030: * SRAP rebranding
031: *
032: * Revision 1.11 2002/06/12 07:55:58 bv131302
033: * more rebranding - filenames
034: *
035: * Revision 1.10 2002/06/11 16:02:03 bv131302
036: * new branded
037: *
038: * Revision 1.9 2002/06/02 09:52:47 ss133690
039: * CRT 1281 Bug 4618920 Option to make Gateway PDC work with other authentication mechanism (Authentication chain)
040: *
041: * Revision 1.8 2002/05/31 11:41:39 ss133690
042: * CRT 1269 Bug 4618938 Option to disable browser caching
043: *
044: * Revision 1.7 2002/05/15 12:13:02 mm132998
045: * Bug ID : # 4665772 , CRT : # 770 , Desc : Charset Migration to iDSAME.
046: *
047: * Revision 1.6 2002/04/10 13:06:15 mm132998
048: * Bug ID : # 4665772 , CRT : # 770 , Desc : Charset Migration to iDSAME.
049: *
050: * Revision 1.5 2002/03/20 04:35:24 mm132998
051: * Bug ID # 4655269 CRT : # 602 Desc : Support for HTTP persistent connection at gateway
052: *
053: * Revision 1.4 2002/02/27 13:27:38 mm132998
054: * Bug ID # 4643783 , CRT : 378 , Desc : If connection is denied , the portal must display a customizable error message.
055: *
056: *
057: */
058: /*
059: * HeaderResponse.java
060: *
061: * $Author: ss150821 $
062: *
063: * $Date: 2005/11/30 11:27:21 $ $Revision: 1.9 $
064: *
065: * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
066: *
067: * Developed by SunPS and SunIR
068: */
069:
070: package com.sun.portal.rproxy.connectionhandler;
071:
072: import java.io.BufferedInputStream;
073: import java.io.ByteArrayInputStream;
074: import java.io.ByteArrayOutputStream;
075: import java.io.DataOutputStream;
076: import java.io.IOException;
077: import java.net.URLEncoder;
078: import java.text.SimpleDateFormat;
079: import java.util.Date;
080: import java.util.HashMap;
081: import java.util.Map;
082:
083: import com.sun.portal.rproxy.configservlet.client.UserProfile;
084: import com.sun.portal.util.GWLocale;
085:
086: /**
087: * This represents an abstract header response.
088: *
089: * @author Carl Limsico
090: */
091: public abstract class HeaderResponse implements Response {
092: protected final String crlf = "\r\n";
093:
094: protected SimpleDateFormat df = new SimpleDateFormat(
095: "EEE, dd MMM yyyy HH:mm:ss zzz");
096:
097: private String statusCode;
098:
099: private String statusText;
100:
101: private byte responseContent[];
102:
103: private Map _headerLines;
104:
105: private static final String CONTENT_ENCODING = "html";
106:
107: private static final String CONTENT_TYPE = "text";
108:
109: private static final String SERVER = "sun.net";
110:
111: private static final String HTTP_VERSION = "HTTP/1.0";
112:
113: private static final String SUBST_STRING = "$$";
114:
115: private static final String space = " ";
116:
117: private static String defaultLocale;
118:
119: private static String defaultCharset;
120:
121: // iDSAME charset migration
122: private static final String HTMLCHARSET_ATTRIBUTE = "HTML-CharSets";
123:
124: private String language = null;
125:
126: // EOC : iDSAME charset migration
127:
128: private String userLocale;
129:
130: private String userCharset;
131:
132: // Lihue KeepAlive
133: protected String keepAliveToken = null;
134:
135: // End of Code : Lihue KeepAlive
136:
137: /*
138: * PRD 3.7 (4338513) Authentication chaining will work with Certificate
139: * authentication.
140: */
141: protected String _cookie = null;
142: // End of code change for PRD 3.7
143:
144: static {
145: // defaultLocale =
146: // com.sun.portal.rproxy.configservlet.client.UserProfile.getString("locale",
147: // "en_US");
148: // defaultCharset =
149: // com.sun.portal.rproxy.configservlet.client.UserProfile.getString("HTMLcharset",
150: // "ISO-8859-1");
151: // iDSAME charset migration
152: defaultLocale = "en_US";
153: defaultCharset = "ISO-8859-1";
154: // EOC : iDSAME charset migration
155: }
156:
157: public HeaderResponse(String field, String message,
158: String statusCode, String statusText,
159: com.iplanet.sso.SSOToken token) {
160: getLocaleCharset(token);
161:
162: StringBuffer contents = new StringBuffer(GWLocale.getString(
163: field, userLocale));
164:
165: _headerLines = new HashMap();
166:
167: if (message.length() > 0) {
168: contents.append(" ");
169: contents.append(message);
170: }
171:
172: responseContent = contents.toString().getBytes();
173: this .statusCode = statusCode;
174: this .statusText = statusText;
175:
176: _headerLines.put("ndate", "Date: " + df.format(new Date()));
177: _headerLines.put("pragma", "Pragma: no-cache");
178: _headerLines.put("server", "Server: sun.net");
179: _headerLines.put("allow", "Allow: GET");
180: _headerLines.put("content-length", "Content-Length: "
181: + responseContent.length);
182: _headerLines.put("content-type",
183: "Content-Type: text/html;charset=" + userCharset);
184: }
185:
186: // Lihue PRD : 8.2
187: public HeaderResponse(String message, String statusCode,
188: String statusText, com.iplanet.sso.SSOToken token) {
189: this (message, statusCode, statusText, token, null);
190: }
191:
192: public HeaderResponse(String message, String statusCode,
193: String statusText, com.iplanet.sso.SSOToken token,
194: String language) {
195: this .language = language;
196: getLocaleCharset(token);
197:
198: _headerLines = new HashMap();
199:
200: responseContent = message.getBytes();
201: this .statusCode = statusCode;
202: this .statusText = statusText;
203:
204: _headerLines.put("ndate", "Date: " + df.format(new Date()));
205: _headerLines.put("pragma", "Pragma: no-cache");
206: _headerLines.put("server", "Server: sun.net");
207: _headerLines.put("allow", "Allow: GET");
208: _headerLines.put("content-length", "Content-Length: "
209: + responseContent.length);
210: _headerLines.put("content-type",
211: "Content-Type: text/html;charset=" + userCharset);
212: }
213:
214: // EOC : Lihue PRD : 8.2
215:
216: public HeaderResponse(String message, String statusCode,
217: String statusText) {
218: getLocaleCharset(null);
219: _headerLines = new HashMap();
220:
221: responseContent = message.getBytes();
222: this .statusCode = statusCode;
223: this .statusText = statusText;
224:
225: _headerLines.put("ndate", "Date: " + df.format(new Date()));
226: _headerLines.put("pragma", "Pragma: no-cache");
227: _headerLines.put("server", "Server: sun.net");
228: _headerLines.put("allow", "Allow: GET");
229: _headerLines.put("content-length", "Content-Length: "
230: + responseContent.length);
231: _headerLines.put("content-type",
232: "Content-Type: text/html;charset=" + userCharset);
233: }
234:
235: /**
236: * Substitute SUBST_STRING with string sub
237: *
238: */
239: public void substituteContent(String sub) {
240: String s = new String(responseContent);
241: StringBuffer sb = new StringBuffer();
242:
243: int i = s.indexOf(SUBST_STRING);
244:
245: if (i != -1) {
246: sb.append(s.substring(0, i));
247: sb.append(sub);
248: sb.append(s.substring(i + 2));
249:
250: responseContent = sb.toString().getBytes();
251: }
252:
253: _headerLines.put("content-length", "Content-Length: "
254: + getContentLength());
255: }
256:
257: public String getHTTPVersion() {
258: return HTTP_VERSION;
259: }
260:
261: public String getStatusCode() {
262: return statusCode;
263: }
264:
265: public String getStatusText() {
266: return statusText;
267: }
268:
269: public String getContentType() {
270: return CONTENT_TYPE + "/" + CONTENT_ENCODING;
271: }
272:
273: public String getContentEncoding() {
274: return CONTENT_ENCODING;
275: }
276:
277: public String getResponseHeader(String headerLine) {
278: return (String) _headerLines.get(headerLine.toLowerCase());
279: }
280:
281: /*
282: * PRD 3.7 (4338513) Authentication chaining will work with Certificate
283: * authentication.
284: */
285: public void setResponseHeader(String header, String value) {
286: if (header != null) {
287: _headerLines.remove(header.toLowerCase());
288: _headerLines.put(header.toLowerCase(), value);
289: if (header.toLowerCase().startsWith("set-cookie"))
290: _cookie = value;
291: }
292: }
293:
294: // End of code change for PRD 3.7
295:
296: public void setContentLength(int i) {
297: _headerLines.remove("content-length");
298: _headerLines.put("content-length", "Content-Length: " + i
299: + crlf);
300: }
301:
302: public void setConnectionClose() {
303: _headerLines.remove("Connection");
304: // Lihue KeepAlive
305: keepAliveToken = "Connection: close\r\n";
306: _headerLines.put("Connection", "Connection: close");
307: }
308:
309: public void setConnectionAlive(String token) {
310: String header = getResponseHeader("Connection");
311: if (header != null) {
312: _headerLines.remove("Connection");
313: }
314: header = getResponseHeader("Keep-Alive");
315: if (header != null) {
316: _headerLines.remove("Keep-Alive");
317: }
318: keepAliveToken = "Keep-Alive: " + token;
319: _headerLines.put("Keep-Alive", keepAliveToken);
320: _headerLines.put("Connection", "Connection: Keep-Alive");
321: keepAliveToken = "Connection: Keep-Alive\r\n" + keepAliveToken
322: + "\r\n";
323:
324: }
325:
326: // End of Code : Lihue KeepAlive
327: public void setLocation(String location) {
328: }
329:
330: public int getContentLength() {
331: String header = (String) _headerLines.get("content-length");
332: if (null == header) {
333: return responseContent.length;
334: }
335:
336: int start = header.indexOf(':');
337: if (start == -1) {
338: return responseContent.length;
339: } else {
340: if (header.endsWith(crlf)) {
341: return Integer.parseInt(header.substring(start + 1,
342: header.length() - 2).trim());
343: } else {
344: return Integer.parseInt(header.substring(start + 1,
345: header.length()).trim());
346: }
347: }
348: }
349:
350: public byte[] getHeaderBytes() {
351: ByteArrayOutputStream outBAOS = new ByteArrayOutputStream();
352:
353: try {
354: DataOutputStream out = new DataOutputStream(outBAOS);
355: StringBuffer strBuffer = new StringBuffer();
356: strBuffer.append(getHTTPVersion());
357: strBuffer.append(space);
358: strBuffer.append(getStatusCode());
359: strBuffer.append(space);
360: strBuffer.append(getStatusText());
361: strBuffer.append(crlf);
362: strBuffer.append("Date: ");
363: strBuffer.append(df.format(new Date()));
364: strBuffer.append(crlf);
365: strBuffer.append("Pragma: no-cache");
366: strBuffer.append(crlf);
367: strBuffer.append("Server: ");
368: strBuffer.append(SERVER);
369: strBuffer.append(crlf);
370: strBuffer.append("Allow: GET");
371: strBuffer.append(crlf);
372: strBuffer.append("Content-Length: ");
373: strBuffer.append(getContentLength());
374: strBuffer.append(crlf);
375: strBuffer.append("Content-Type: ");
376: strBuffer.append(getContentType());
377: /*
378: * strBuffer.append("/"); strBuffer.append(getContentEncoding());
379: */
380: strBuffer.append(";charset=");
381: strBuffer.append(userCharset);
382: strBuffer.append(crlf);
383: out.writeBytes(strBuffer.toString());
384: /*
385: * out.writeBytes(getHTTPVersion() + " " + getStatusCode() + " " +
386: * getStatusText() + crlf + "Date: " + df.format(new Date()) + crlf +
387: * "Pragma: no-cache" + crlf + "Server: " + SERVER + crlf + "Allow:
388: * GET" + crlf + "Content-Length: " + getContentLength() + crlf +
389: * "Content-Type: " + getContentType() + "/" + getContentEncoding() +
390: * ";charset=" + userCharset // Lihue KeepAlive + crlf); //+ crlf +
391: * crlf);
392: */
393: if (keepAliveToken != null) {
394: // In HTTP/1.1 by default all requests are Keep-Alive
395: // and so we explictly specify whether we are keeping
396: // the connection alive or not so that it also works
397: // with HTTP/1.0
398: // - Mridul
399: out.writeBytes(keepAliveToken);
400: }
401:
402: /*
403: * PRD 3.7 (4338513) Authentication chaining will work with
404: * Certificate authentication.
405: */
406: if (_cookie != null) {
407: out.writeBytes(_cookie + crlf);
408: }
409: // End of code change for PRD 3.7
410:
411: out.writeBytes(crlf);
412: // End of Code : Lihue KeepAlive
413: } catch (IOException ex) {
414: }
415: return outBAOS.toByteArray();
416: }
417:
418: public BufferedInputStream getContentStream() {
419: ByteArrayOutputStream outBAOS = new ByteArrayOutputStream();
420: try {
421: DataOutputStream out = new DataOutputStream(outBAOS);
422: out.write(responseContent);
423: } catch (IOException ex) {
424: }
425: return new BufferedInputStream(new ByteArrayInputStream(outBAOS
426: .toByteArray()));
427: }
428:
429: public String toString() {
430: return new String(getHeaderBytes());
431: }
432:
433: public void setStatusText(String st) {
434: statusText = st;
435: }
436:
437: private void getLocaleCharset(com.iplanet.sso.SSOToken token) {
438: if (token == null) {
439: // iDSAME charset migration
440: // userLocale = defaultLocale;
441: // userCharset = defaultCharset;
442:
443: // As per Tom Mueller's mail , in case the user does not have a
444: // session
445: // then we have to pick it up from "Accept-Language:" which is
446: // better than
447: // just using defaultLocale and defaultCharset
448: // - Mridul
449: if (language != null) {
450: language = language
451: .substring(language.indexOf(':') + 1).trim();
452: userLocale = language;
453: userCharset = getHTMLCharset(userLocale);
454: if (userCharset == null) {
455: userLocale = defaultLocale;
456: userCharset = defaultCharset;
457: }
458: } else {
459: userLocale = defaultLocale;
460: userCharset = defaultCharset;
461: }
462: return;
463: }
464:
465: try {
466: // Mridul - iDSAME migration
467: // String sid =
468: // java.net.URLDecoder.decode(token.getTokenID().toString());
469: String sid = token.getTokenID().toString();
470: // UserProfile _userProfile = new UserProfile(sid);
471: // Userprofile expects a URL Encoded session id string.
472: UserProfile _userProfile = new UserProfile(URLEncoder
473: .encode(sid));
474: // UserProfile _userProfile = new UserProfile("this-should-be-sid");
475: // userLocale = _userProfile.getString("iwtUser-locale", "en_US");
476: userLocale = _userProfile.getString("preferredlocale",
477: "en_US");
478: // iDSAME charset migration
479: // userCharset = _userProfile.getString("iwtUser-HTMLcharset",
480: // "ISO-8859-1");
481: userCharset = getHTMLCharset(userLocale);
482: if (userCharset == null) {
483: userLocale = defaultLocale;
484: userCharset = defaultCharset;
485: }
486: // EOC : iDSAME charset migration
487: // EOC : Mridul - iDSAME migration
488: } catch (Exception ex) {
489: userLocale = defaultLocale;
490: userCharset = defaultCharset;
491: }
492: }
493:
494: // iDSAME charset migration
495: /*
496: * When this methd returns null , it is assumed that the particular loclae
497: * is not supported - hence , revert back to gateway locale and charset
498: */
499: private String getHTMLCharset(String locale) {
500: String charsetString = GWLocale.getString(
501: HTMLCHARSET_ATTRIBUTE, locale);
502: if (charsetString == null
503: || charsetString.equals(HTMLCHARSET_ATTRIBUTE)) {
504: charsetString = null;
505: }
506: return charsetString;
507: }
508:
509: // EOC : iDSAME charset migration
510:
511: public void setSocket(CachedSocket sock) {
512: }
513:
514: public void closeSocket() {
515: }
516:
517: /**
518: * Bug 4618938 When "gateway.allow.client.caching" is set to false in
519: * Platform.conf the client cache will be disabled.
520: */
521: public void setClientCaching() {
522: }
523: // End of change of code for the bug 4618938
524:
525: }
|