001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs;
030:
031: import java.io.IOException;
032: import java.net.SocketException;
033: import java.util.Iterator;
034: import java.util.logging.Logger;
035:
036: /**
037: * Facade to HttpStream to properly handle the close.
038: */
039: class HttpStreamWrapper extends StreamImpl {
040: private static final Logger log = Logger.getLogger(HttpStream.class
041: .getName());
042:
043: private HttpStream _stream;
044:
045: /**
046: * Create a new HTTP stream.
047: */
048: HttpStreamWrapper(HttpStream stream) throws IOException {
049: _stream = stream;
050: }
051:
052: /**
053: * Set if this should be an SSL connection.
054: */
055: public void setSSL(boolean isSSL) {
056: _stream.setSSL(isSSL);
057: }
058:
059: /**
060: * Set if this should be an SSL connection.
061: */
062: public boolean isSSL() {
063: return _stream.isSSL();
064: }
065:
066: /**
067: * Sets the method
068: */
069: public void setMethod(String method) {
070: _stream.setMethod(method);
071: }
072:
073: /**
074: * Sets true if we're only interested in the head.
075: */
076: public void setHead(boolean isHead) {
077: _stream.setHead(isHead);
078: }
079:
080: /**
081: * Returns the stream's host.
082: */
083: public String getHost() {
084: return _stream.getHost();
085: }
086:
087: /**
088: * Returns the stream's port.
089: */
090: public int getPort() {
091: return _stream.getPort();
092: }
093:
094: /**
095: * Returns a header from the response returned from the HTTP server.
096: *
097: * @param name name of the header
098: * @return the header value.
099: */
100: public Object getAttribute(String name) throws IOException {
101: if (_stream != null)
102: return _stream.getAttribute(name);
103: else
104: return null;
105: }
106:
107: /**
108: * Returns an iterator of the returned header names.
109: */
110: public Iterator getAttributeNames() throws IOException {
111: if (_stream != null)
112: return _stream.getAttributeNames();
113: else
114: return null;
115: }
116:
117: /**
118: * Sets a header for the request.
119: */
120: public void setAttribute(String name, Object value) {
121: if (_stream != null)
122: _stream.setAttribute(name, value);
123: }
124:
125: /**
126: * Remove a header for the request.
127: */
128: public void removeAttribute(String name) {
129: if (_stream != null)
130: _stream.removeAttribute(name);
131: }
132:
133: /**
134: * Sets the timeout.
135: */
136: public void setSocketTimeout(long timeout) throws SocketException {
137: if (_stream != null)
138: _stream.setSocketTimeout(timeout);
139: }
140:
141: /**
142: * The stream is always writable (?)
143: */
144: public boolean canWrite() {
145: if (_stream != null)
146: return _stream.canWrite();
147: else
148: return false;
149: }
150:
151: /**
152: * Writes a buffer to the underlying stream.
153: *
154: * @param buffer the byte array to write.
155: * @param offset the offset into the byte array.
156: * @param length the number of bytes to write.
157: * @param isEnd true when the write is flushing a close.
158: */
159: public void write(byte[] buf, int offset, int length, boolean isEnd)
160: throws IOException {
161: if (_stream != null)
162: _stream.write(buf, offset, length, isEnd);
163: }
164:
165: /**
166: * The stream is readable.
167: */
168: public boolean canRead() {
169: if (_stream != null)
170: return _stream.canRead();
171: else
172: return false;
173: }
174:
175: /**
176: * Read data from the connection. If the request hasn't yet been sent
177: * to the server, send it.
178: */
179: public int read(byte[] buf, int offset, int length)
180: throws IOException {
181: if (_stream != null)
182: return _stream.read(buf, offset, length);
183: else
184: return -1;
185: }
186:
187: /**
188: * Returns the bytes still available.
189: */
190: public int getAvailable() throws IOException {
191: if (_stream != null)
192: return _stream.getAvailable();
193: else
194: return -1;
195: }
196:
197: /**
198: * Close the connection.
199: */
200: public void close() throws IOException {
201: HttpStream stream = _stream;
202: _stream = null;
203:
204: if (stream != null)
205: stream.close();
206: }
207: }
|