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.rproxy.rewriter.util.http;
006:
007: import com.sun.portal.rewriter.util.Constants;
008: import com.sun.portal.log.common.PortalLogger;
009: import com.sun.portal.rewriter.util.uri.PageSpec;
010: import com.sun.portal.rproxy.connectionhandler.HTTPResponse;
011: import com.sun.portal.rproxy.connectionhandler.Response;
012: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
013: import com.sun.portal.rproxy.monitoring.util.RProxyEvent;
014:
015: import java.io.BufferedInputStream;
016: import java.io.ByteArrayOutputStream;
017: import java.io.IOException;
018:
019: public class HTTPData {
020: private static final String CONTENT_LENGTH_PATTERN = "Content-length";
021:
022: private final Response response;
023: private final PageSpec pageSpec;
024: private final ContentInfo contentInfo;
025:
026: private byte[] contentBytes;
027: private String rawEncodedString;
028: private String rawEncodedStringLowerCase;
029:
030: private String pageContent;
031:
032: public HTTPData(PageSpec aPageSpec, Response aResponse)
033: throws IOException {
034: response = aResponse;
035: pageSpec = aPageSpec;
036: contentInfo = new ContentInfo();
037: MIMEAndEncodingParser.parse(this );
038: }//constructor
039:
040: public String getContentType() {
041: return response.getContentType();
042: }//getContentType()
043:
044: public ContentInfo getContentInfo() {
045: return contentInfo;
046: }//getConentInfo()
047:
048: public boolean isStreamRead() {
049: return (contentBytes != null) ? true : false;
050: }//isStreamRead()
051:
052: public byte[] getContentBytes() throws IOException {
053: if (contentBytes == null) {
054: contentBytes = readFullContent(response);
055: MonitoringSubsystem.handleEvent(RProxyEvent.RESP_READ,
056: new Long(contentBytes.length));
057: }
058:
059: return contentBytes;
060: }//getContentBytes()
061:
062: public String getRawEncodedString() throws IOException {
063: if (rawEncodedString == null && (getContentBytes() != null)) {
064: rawEncodedString = new String(getContentBytes());
065: }
066: return rawEncodedString;
067: }//getRawEncodedString()
068:
069: public String getRawEncodedStringLowerCase() throws IOException {
070: if (rawEncodedStringLowerCase == null) {
071: rawEncodedStringLowerCase = getRawEncodedString()
072: .toLowerCase();
073: }
074:
075: return rawEncodedStringLowerCase;
076: }//getRawEncodedStringLowerCase()
077:
078: public PageSpec getPageSpec() {
079: return pageSpec;
080: }//getPageSpec()
081:
082: //Note: Make sure this method is called only once that too after write encoding
083: //as this var is intialized only once..
084: public String getPageContent() throws IOException {
085: String lEncoding = contentInfo.getEncoding();
086:
087: if (lEncoding == null || lEncoding == Constants.SYSTEM_ENCODING) {
088: contentInfo.setEncoding(Constants.SYSTEM_ENCODING);
089: return getRawEncodedString();
090: }
091:
092: if (pageContent == null && getContentBytes() != null) {
093: pageContent = new String(getContentBytes(), contentInfo
094: .getEncoding());
095: }
096:
097: return pageContent;
098: }//getPageContent()
099:
100: private static byte[] readFullContent(Response resp)
101: throws IOException {
102: String lContentLength = resp
103: .getResponseHeader(CONTENT_LENGTH_PATTERN);
104: BufferedInputStream buffIn = resp.getContentStream();
105: if (resp instanceof HTTPResponse) {
106: HTTPResponse lHTTPResponse = (HTTPResponse) resp;
107: String te = lHTTPResponse
108: .getResponseHeader("Transfer-Encoding");
109: if (te != null) {
110: int idx = te.indexOf(':');
111: if (te.substring(idx + 1).trim().equalsIgnoreCase(
112: "chunked")) {
113: byte[] contentBuf = ChunkedContent
114: .readChunkedContent(buffIn);
115: lHTTPResponse.removeHeader("Transfer-Encoding");// Remove "chunked" from Transfer-Encoding
116: return contentBuf;
117: }
118: }
119: }
120:
121: if (buffIn == null) {
122: return null;
123: }
124:
125: if (lContentLength != null) {
126: int length = Integer.parseInt(lContentLength.substring(
127: lContentLength.indexOf(':') + 1).trim());
128:
129: byte[] byteContent = new byte[length];
130: int read = 0, totalRead = 0;
131: while (totalRead < length) {
132: read = buffIn.read(byteContent, totalRead, length
133: - totalRead);
134: if (read == -1) {
135: //BugNo:4880860
136: //if ( totalRead < length )
137: //{
138: //continue;
139: //}
140:
141: break;
142: } else {
143: totalRead += read;
144: }
145: }
146:
147: return byteContent;
148: } else {
149: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
150: byte content[] = new byte[2048];
151: int numbytes;
152:
153: while (true) {
154: numbytes = buffIn.read(content);
155: if (numbytes == -1) {
156: break;
157: }
158:
159: bOut.write(content, 0, numbytes);
160: }//while loop
161:
162: return bOut.toByteArray();
163: }//if/else
164: }//readFullContent()
165:
166: }//class HTTPData
|