All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xsocket.connection.http.HttpResponse Maven / Gradle / Ivy

There is a newer version: 2.0-beta-1
Show newest version
/*
 *  Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
 * The latest copy of this software may be found on http://www.xsocket.org/
 */
package org.xsocket.connection.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;







/**
 * http response representation
 *
 * @author [email protected]
 */
public class HttpResponse extends AbstractHttpMessage  {
	
	private HttpResponseHeader responseHeader = null; 

	
	

	/**
	 * constructor 
	 * 
	 * @param responseHeader   the response header
	 */
	public HttpResponse(HttpResponseHeader responseHeader) {
		this.responseHeader = responseHeader;
	}	
	
	
	
	/**
	 * constructor 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 */
	public HttpResponse(HttpResponseHeader responseHeader, NonBlockingBodyDataSource bodyDataSource) {
		this.responseHeader = responseHeader;
		setBodyDataSource(bodyDataSource);
	}	

	
	/**
	 * constructor 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 */
	public HttpResponse(String contentType, NonBlockingBodyDataSource bodyDataSource) {
		responseHeader = new HttpResponseHeader(200, contentType);
		setBodyDataSource(bodyDataSource);
	}	

	
	/**
	 * constructor 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 */
	public HttpResponse(String contentType, BlockingBodyDataSource bodyDataSource) {
		responseHeader = new HttpResponseHeader(200, contentType);
		setBodyDataSource(bodyDataSource.getUnderliyingBodyDataSource());
	}	
	

	/**
	 * constructor 
	 * 
	 * @param contentType    the content type 
	 * @param body           the body 
	 */
	public HttpResponse(String contentType, String body) throws IOException {
		this(200, contentType, body);
	}	
	
	
	
	/**
	 * constructor 
	 * 
	 * @param contentType    the content type
	 * @param body           the body 
	 * @throws IOException if an error occurs by reading the body
	 */
	public HttpResponse(String contentType, FileChannel body) throws IOException {
		this(200, contentType, body);
	}	
	

	/**
	 * constructor 
	 * 
	 * @param contentType    the content type
	 * @param body           the body 
	 * @throws IOException if an error occurs by reading the body
	 */
	public HttpResponse(String contentType, InputStream body) throws IOException {
		responseHeader = new HttpResponseHeader(200, contentType);
		setBodyDataSource(Channels.newChannel(body));		
	}	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 */
	public HttpResponse(int status)  {
		responseHeader = new HttpResponseHeader(status);
	}	

	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type
	 * @param body         the body
	 */
	public HttpResponse(int status, String contentType, String body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		
		try {
			setBodyDataSource(body, responseHeader.getCharacterEncoding());
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}
	}	



	/**
	 * constructor
	 *  
	 * @param contentType    the content type
	 * @param body           the body
	 */
	public HttpResponse(String contentType, byte[] body) throws IOException {
		this(200, contentType, body);
	}	

	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type
	 * @param body         the body
	 */
	public HttpResponse(int status, String contentType, FileChannel body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		responseHeader.setContentLength((int) body.size());
		
		setBodyDataSource(body);		
	}	


	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param contentLength  the content length
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, int contentLength, ReadableByteChannel body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		responseHeader.setContentLength(contentLength);
		
		setBodyDataSource(body);		
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param contentLength  the content length
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, int contentLength, NonBlockingBodyDataSource body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		responseHeader.setContentLength(contentLength);
		
		setBodyDataSource(body);		
	}	
	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param contentLength  the content length
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, int contentLength, BlockingBodyDataSource body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		responseHeader.setContentLength(contentLength);
		
		setBodyDataSource(body.getUnderliyingBodyDataSource());		
	}	

	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, ReadableByteChannel body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		setBodyDataSource(body);		
	}	
	
	
	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, NonBlockingBodyDataSource body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		setBodyDataSource(body);		
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param body           the body
	 */
	public HttpResponse(int status, String contentType, BlockingBodyDataSource body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		setBodyDataSource(body.getUnderliyingBodyDataSource());		
	}	
	
	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type
	 * @param body         the body
	 */
	public HttpResponse(int status, String contentType, byte[] body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		
		setBodyDataSource(body, responseHeader.getCharacterEncoding());
	}	


	/**
	 * constructor 
	 * 
	 * @param contentType    the content type
	 * @param body           the body 
	 */
	public HttpResponse(String contentType, ByteBuffer[] body) throws IOException {
		this(200, contentType, body);
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type 
	 * @param body         the body 
	 */
	public HttpResponse(int status, String contentType, ByteBuffer[] body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);

		try {
			setBodyDataSource(body, responseHeader.getCharacterEncoding());
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}			
	}	

		
	/**
	 * constructor 
	 * 
	 * @param responseHeader   the response header 
	 * @param body             the body 
	 */
	public HttpResponse(HttpResponseHeader responseHeader, String body) throws IOException {		
		this.responseHeader = responseHeader;
		
		try {
			setBodyDataSource(body, responseHeader.getCharacterEncoding());
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}
	}

	
	/**
	 * constructor 
	 * 
	 * @param responseHeader   the response header
	 * @param body             the body 
	 */
	public HttpResponse(HttpResponseHeader responseHeader, byte[] body) throws IOException {
		this.responseHeader = responseHeader;
		
		try {
			setBodyDataSource(body, responseHeader.getCharacterEncoding());
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}			
	}

			

	/**
	 * returns the status 
	 * 
	 * @return the status 
	 */
	public final int getStatus() {
		return responseHeader.getStatus();
	}
	
	
	/**
	 * returns the reason
	 * 
	 * @return the reason 
	 */
	public final String getReason() {
		return responseHeader.getReason();
	}
	
	
	/**
	 * returns the protocol 
	 * 
	 * @return the protocol
	 */
	public final String getProtocol() {
		return responseHeader.getProtocol();
	}
	
	
	/**
	 * returns the response header
	 *  
	 * @return the response header
	 */
	public final HttpResponseHeader getResponseHeader() {
		return responseHeader;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	AbstractMessageHeader getMessageHeader() {
		return getResponseHeader();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy