axishttpresponseimpl.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 248 行

JAVA
248
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.transport.http.server;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import org.apache.axis2.transport.OutTransportInfo;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.protocol.HttpProcessor;

public class AxisHttpResponseImpl implements AxisHttpResponse, OutTransportInfo {

    private final HttpResponse response;
    private final AxisHttpConnection conn;
    private final HttpProcessor httpproc;
    private final HttpContext context;
    
    private AutoCommitOutputStream outstream;
    private String contentType;
    
    private volatile boolean commited;
    
    public AxisHttpResponseImpl(
            final AxisHttpConnection conn,
            final HttpResponse response, 
            final HttpProcessor httpproc,
            final HttpContext context) {
        super();
        if (response == null) {
            throw new IllegalArgumentException("HTTP response may not be null");
        }
        if (conn == null) {
            throw new IllegalArgumentException("HTTP connection may not be null");
        }
        if (httpproc == null) {
            throw new IllegalArgumentException("HTTP processor may not be null");
        }
        if (context == null) {
            throw new IllegalArgumentException("HTTP context may not be null");
        }
        this.response = response;
        this.conn = conn;
        this.httpproc = httpproc;
        this.context = context;
    }

    private void assertNotCommitted() {
        if (this.commited) {
            throw new IllegalStateException("Response already committed");
        }
    }
    
    public boolean isCommitted() {
        return this.commited;
    }
    
    public void commit() throws IOException, HttpException {
        if (this.commited) {
            return;
        }
        this.commited = true;
        
        this.context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, this.conn);
        this.context.setAttribute(HttpExecutionContext.HTTP_RESPONSE, this.response);
        
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setChunked(true);
        entity.setContentType(this.contentType);
        
        this.response.setEntity(entity);
        
        this.httpproc.process(this.response, this.context);
        this.conn.sendResponse(this.response);
    }
    
    public OutputStream getOutputStream() {
        if (this.outstream == null) {
            this.outstream = new AutoCommitOutputStream();
        }
        return this.outstream;
    }

    public void sendError(int sc, final String msg) {
        assertNotCommitted();
        HttpVersion ver = this.response.getHttpVersion();
        this.response.setStatusLine(ver, sc, msg);
    }

    public void sendError(int sc) {
        assertNotCommitted();
        this.response.setStatusCode(sc);
    }

    public void setStatus(int sc) {
        assertNotCommitted();
        this.response.setStatusCode(sc);
    }

    public void setContentType(final String contentType) {
        assertNotCommitted();
        this.contentType = contentType;
    }

    public HttpVersion getHttpVersion() {
        return this.response.getHttpVersion();
    }

    public void addHeader(final Header header) {
        assertNotCommitted();
        this.response.addHeader(header);
    }

    public void addHeader(final String name, final String value) {
        assertNotCommitted();
        this.response.addHeader(name, value);
    }

    public boolean containsHeader(final String name) {
        return this.response.containsHeader(name);
    }

    public Header[] getAllHeaders() {
        return this.response.getAllHeaders();
    }

    public Header getFirstHeader(final String name) {
        return this.response.getFirstHeader(name);
    }

    public Header[] getHeaders(String name) {
        return this.response.getHeaders(name);
    }

    public Header getLastHeader(final String name) {
        return this.response.getLastHeader(name);
    }

    public Iterator headerIterator() {
        return this.response.headerIterator();
    }

    public void removeHeader(final Header header) {
        assertNotCommitted();
        this.response.removeHeader(header);
    }

    public void removeHeaders(final String name) {
        assertNotCommitted();
        this.response.removeHeaders(name);
    }

    public void setHeader(final Header header) {
        assertNotCommitted();
        this.response.setHeader(header);
    }

    public void setHeader(final String name, final String value) {
        assertNotCommitted();
        this.response.setHeader(name, value);
    }

    public void setHeaders(Header[] headers) {
        assertNotCommitted();
        this.response.setHeaders(headers);
    }

    public HttpParams getParams() {
        return this.response.getParams();
    }

    public void setParams(final HttpParams params) {
        this.response.setParams(params);
    }
    
    class AutoCommitOutputStream extends OutputStream {

        private OutputStream out;
        
        public AutoCommitOutputStream() {
            super();
        }

        private void ensureCommitted() throws IOException {
            try {
                commit();
            } catch (HttpException ex) {
                throw new IOException("HTTP protocol exception: " + ex.getMessage());
            }
            if (this.out == null) {
                this.out = conn.getOutputStream();
            }
        }
        
        public void close() throws IOException {
            ensureCommitted();
            this.out.close();
        }

        public void write(final byte[] b, int off, int len) throws IOException {
            ensureCommitted();
            this.out.write(b, off, len);
        }

        public void write(final byte[] b) throws IOException {
            ensureCommitted();
            this.out.write(b);
        }

        public void write(int b) throws IOException {
            ensureCommitted();
            this.out.write(b);
        }
        
        public void flush() throws IOException {
            ensureCommitted();
            this.out.flush();
        }

    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?