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

org.xsocket.connection.http.Response 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.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;







/**
 * http response representation
 *
 * @author [email protected]
 */
public class Response extends AbstractMessage  {
	
	private ResponseHeader responseHeader = null; 

	
	

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

	

	/**
	 * constructor 
	 * 
	 * @param contentType    the content type 
	 * @param body           the body 
	 */
	public Response(String contentType, String body) {
		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 Response(String contentType, ReadableByteChannel body) throws IOException {
		responseHeader = new ResponseHeader(200, contentType);
		
		NonBlockingBodyDataSource dataSource = new NonBlockingBodyDataSource(body, responseHeader.getCharacterEncoding());
		if (dataSource.available() > 0) {
			setBodyDataSource(dataSource);
		}
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type
	 * @param body         the body
	 */
	public Response(int status, String contentType, String body)  {
		responseHeader = new ResponseHeader(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 Response(String contentType, byte[] body)  {
		this(200, contentType, body);
	}	

	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type
	 * @param body         the body
	 */
	public Response(int status, String contentType, byte[] body)  {
		responseHeader = new ResponseHeader(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 Response(String contentType, ByteBuffer[] body) {
		this(200, contentType, body);
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param contentType  the content type 
	 * @param body         the body 
	 */
	public Response(int status, String contentType, ByteBuffer[] body)  {
		responseHeader = new ResponseHeader(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 Response(ResponseHeader responseHeader, String body) {		
		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 Response(ResponseHeader responseHeader, byte[] body) {
		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 ResponseHeader getResponseHeader() {
		return responseHeader;
	}
	
	
	/**
	 * return the connection header
	 * 
	 * @return  the connection header
	 */
	public final String getConnection() {
		return responseHeader.getConnection();
	}
	
	
	/**
	 * returns the keep alive header
	 * 
	 * @return the keep alive header
	 */
	public final String getKeepAlive() {
		return responseHeader.getKeepAlive();
	}

	/**
	 * sets the keep alive header 
	 * 
	 * @param keepAlive  the keep alive header
	 */
	public final void setKeepAlive(String keepAlive) {
		responseHeader.setKeepAlive(keepAlive);
	}  
	
	/**
	 * {@inheritDoc}
	 */
	AbstractMessageHeader getMessageHeader() {
		return getResponseHeader();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy