001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.http.server;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.util.Iterator;
025:
026: import org.apache.axis2.transport.OutTransportInfo;
027: import org.apache.http.Header;
028: import org.apache.http.HttpException;
029: import org.apache.http.HttpResponse;
030: import org.apache.http.HttpVersion;
031: import org.apache.http.entity.BasicHttpEntity;
032: import org.apache.http.params.HttpParams;
033: import org.apache.http.protocol.HttpContext;
034: import org.apache.http.protocol.HttpExecutionContext;
035: import org.apache.http.protocol.HttpProcessor;
036:
037: public class AxisHttpResponseImpl implements AxisHttpResponse,
038: OutTransportInfo {
039:
040: private final HttpResponse response;
041: private final AxisHttpConnection conn;
042: private final HttpProcessor httpproc;
043: private final HttpContext context;
044:
045: private AutoCommitOutputStream outstream;
046: private String contentType;
047:
048: private volatile boolean commited;
049:
050: public AxisHttpResponseImpl(final AxisHttpConnection conn,
051: final HttpResponse response, final HttpProcessor httpproc,
052: final HttpContext context) {
053: super ();
054: if (response == null) {
055: throw new IllegalArgumentException(
056: "HTTP response may not be null");
057: }
058: if (conn == null) {
059: throw new IllegalArgumentException(
060: "HTTP connection may not be null");
061: }
062: if (httpproc == null) {
063: throw new IllegalArgumentException(
064: "HTTP processor may not be null");
065: }
066: if (context == null) {
067: throw new IllegalArgumentException(
068: "HTTP context may not be null");
069: }
070: this .response = response;
071: this .conn = conn;
072: this .httpproc = httpproc;
073: this .context = context;
074: }
075:
076: private void assertNotCommitted() {
077: if (this .commited) {
078: throw new IllegalStateException(
079: "Response already committed");
080: }
081: }
082:
083: public boolean isCommitted() {
084: return this .commited;
085: }
086:
087: public void commit() throws IOException, HttpException {
088: if (this .commited) {
089: return;
090: }
091: this .commited = true;
092:
093: this .context.setAttribute(HttpExecutionContext.HTTP_CONNECTION,
094: this .conn);
095: this .context.setAttribute(HttpExecutionContext.HTTP_RESPONSE,
096: this .response);
097:
098: BasicHttpEntity entity = new BasicHttpEntity();
099: entity.setChunked(true);
100: entity.setContentType(this .contentType);
101:
102: this .response.setEntity(entity);
103:
104: this .httpproc.process(this .response, this .context);
105: this .conn.sendResponse(this .response);
106: }
107:
108: public OutputStream getOutputStream() {
109: if (this .outstream == null) {
110: this .outstream = new AutoCommitOutputStream();
111: }
112: return this .outstream;
113: }
114:
115: public void sendError(int sc, final String msg) {
116: assertNotCommitted();
117: HttpVersion ver = this .response.getHttpVersion();
118: this .response.setStatusLine(ver, sc, msg);
119: }
120:
121: public void sendError(int sc) {
122: assertNotCommitted();
123: this .response.setStatusCode(sc);
124: }
125:
126: public void setStatus(int sc) {
127: assertNotCommitted();
128: this .response.setStatusCode(sc);
129: }
130:
131: public void setContentType(final String contentType) {
132: assertNotCommitted();
133: this .contentType = contentType;
134: }
135:
136: public HttpVersion getHttpVersion() {
137: return this .response.getHttpVersion();
138: }
139:
140: public void addHeader(final Header header) {
141: assertNotCommitted();
142: this .response.addHeader(header);
143: }
144:
145: public void addHeader(final String name, final String value) {
146: assertNotCommitted();
147: this .response.addHeader(name, value);
148: }
149:
150: public boolean containsHeader(final String name) {
151: return this .response.containsHeader(name);
152: }
153:
154: public Header[] getAllHeaders() {
155: return this .response.getAllHeaders();
156: }
157:
158: public Header getFirstHeader(final String name) {
159: return this .response.getFirstHeader(name);
160: }
161:
162: public Header[] getHeaders(String name) {
163: return this .response.getHeaders(name);
164: }
165:
166: public Header getLastHeader(final String name) {
167: return this .response.getLastHeader(name);
168: }
169:
170: public Iterator headerIterator() {
171: return this .response.headerIterator();
172: }
173:
174: public void removeHeader(final Header header) {
175: assertNotCommitted();
176: this .response.removeHeader(header);
177: }
178:
179: public void removeHeaders(final String name) {
180: assertNotCommitted();
181: this .response.removeHeaders(name);
182: }
183:
184: public void setHeader(final Header header) {
185: assertNotCommitted();
186: this .response.setHeader(header);
187: }
188:
189: public void setHeader(final String name, final String value) {
190: assertNotCommitted();
191: this .response.setHeader(name, value);
192: }
193:
194: public void setHeaders(Header[] headers) {
195: assertNotCommitted();
196: this .response.setHeaders(headers);
197: }
198:
199: public HttpParams getParams() {
200: return this .response.getParams();
201: }
202:
203: public void setParams(final HttpParams params) {
204: this .response.setParams(params);
205: }
206:
207: class AutoCommitOutputStream extends OutputStream {
208:
209: private OutputStream out;
210:
211: public AutoCommitOutputStream() {
212: super ();
213: }
214:
215: private void ensureCommitted() throws IOException {
216: try {
217: commit();
218: } catch (HttpException ex) {
219: throw new IOException("HTTP protocol exception: "
220: + ex.getMessage());
221: }
222: if (this .out == null) {
223: this .out = conn.getOutputStream();
224: }
225: }
226:
227: public void close() throws IOException {
228: ensureCommitted();
229: this .out.close();
230: }
231:
232: public void write(final byte[] b, int off, int len)
233: throws IOException {
234: ensureCommitted();
235: this .out.write(b, off, len);
236: }
237:
238: public void write(final byte[] b) throws IOException {
239: ensureCommitted();
240: this .out.write(b);
241: }
242:
243: public void write(int b) throws IOException {
244: ensureCommitted();
245: this .out.write(b);
246: }
247:
248: public void flush() throws IOException {
249: ensureCommitted();
250: this.out.flush();
251: }
252:
253: }
254:
255: }
|