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

org.xlightweb.HttpResponse Maven / Gradle / Ivy

Go to download

xLightweb is a lightweight, high performance, scalable web network library

There is a newer version: 2.13.2
Show newest version
/*
 *  Copyright (c) xlightweb.org, 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.xlightweb.org/
 */
package org.xlightweb;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


import org.xsocket.DataConverter;







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

	

	/**
	 * constructor 
	 * 
	 * @param responseHeader   the response header
	 */
	public HttpResponse(IHttpResponseHeader responseHeader) {
		this.responseHeader = responseHeader;
	}	
	
	
	
	/**
	 * constructor 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 * 
	 * @throws IOException if an io exception occurs
	 */
	public HttpResponse(IHttpResponseHeader responseHeader, NonBlockingBodyDataSource bodyDataSource) throws IOException {
		this.responseHeader = responseHeader;
		setBodyDataSource(bodyDataSource);
	}	

	
	/**
	 * constructor 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 * 
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(String contentType, NonBlockingBodyDataSource bodyDataSource) throws IOException {
		responseHeader = new HttpResponseHeader(200, contentType);
		setBodyDataSource(bodyDataSource);
	}	

	
	/**
	 * constructor. The status will be set to 200 
	 * 
	 * @param responseHeader      the response header
	 * @param bodyDataSource      the response body
	 * 
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(String contentType, BlockingBodyDataSource bodyDataSource) throws IOException {
		responseHeader = new HttpResponseHeader(200, contentType);
		setBodyDataSource(bodyDataSource.getUnderliyingBodyDataSource());
	}	
	

	/**
	 * constructor. The status will be set to 200
	 * 
	 * @param contentType    the content type 
	 * @param body           the body
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(String contentType, String body) throws IOException {
		this(200, contentType, body);
	}	

	
	
	/**
	 * constructor. The status will be set to 200
	 * 
	 * @param body           the body
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(String body) throws IOException {
		this(new HttpResponseHeader(200), body);
	}	


	/**
	 * constructor. The status will be set to 200
	 * 
	 * @param file           the file
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(File file) throws IOException {
		this(new HttpResponseHeader(200), new NonBlockingBodyDataSource("ISO-8859-1"));

		setContentTypeByFileExtension(this, file);
		setContentLength((int) file.length());
		removeHeader("Transfer-Encoding");
		
		RandomAccessFile raf = new RandomAccessFile(file, "r");
		FileChannel fc = raf.getChannel();

		ByteBuffer buffer = ByteBuffer.allocate(getContentLength());
		fc.read(buffer);
		fc.close();
		raf.close();
		buffer.flip();
		
		getNonBlockingBody().append(buffer);
		getNonBlockingBody().setComplete(true);
	}	
	

	 
	/**
	 * 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
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(int status, String contentType, String body) throws IOException {
		this(status, contentType, new ByteBuffer[] { DataConverter.toByteBuffer(body, "UTF-8") }, "UTF-8");
	}	


	/**
	 * constructor 
	 * 
	 * @param status       the status 
	 * @param body         the body
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(int status, String body) throws IOException {
		this(new HttpResponseHeader(200), new ByteBuffer[] { DataConverter.toByteBuffer(body, "ISO-8859-1 ")});
	}	

	/**
	 * constructor. The status will be set to 200
	 *  
	 * @param contentType    the content type
	 * @param body           the body
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(String contentType, byte[] body) throws IOException {
		this(200, contentType, body);
	}	



	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param contentLength  the content length
	 * @param body           the body
	 * @throws IOException if an io exception occurs 
	 */
	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
	 * @throws IOException if an io exception occurs 
	 */
	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
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(int status, String contentType, NonBlockingBodyDataSource body) throws IOException {
		if (body == null) {
			throw new NullPointerException("body has to be set");
		}
		responseHeader = new HttpResponseHeader(status, contentType);
		setBodyDataSource(body);		
	}	
	
	
	/**
	 * constructor 
	 * 
	 * @param status         the status 
	 * @param contentType    the content type
	 * @param body           the body
	 * @throws IOException if an io exception occurs 
	 */
	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
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(int status, String contentType, byte[] body) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType);
		responseHeader.setContentLength(body.length);
		
		setBodyDataSource(body, responseHeader.getCharacterEncoding());
	}	

	

	/**
	 * constructor. The status will be set to 200 
	 * 
	 * @param contentType    the content type
	 * @param body           the body
	 * @throws IOException if an io exception occurs  
	 */
	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 
	 * @throws IOException if an io exception occurs 
	 */
	public HttpResponse(int status, String contentType, ByteBuffer[] body) throws IOException {
		this(status, contentType, body, "UTF-8");			
	}	

	
	/**
	 * constructor 
	 * 
	 * 
	 * @param status        the status 
	 * @param contentType   the content type
	 * @param body          the body 
	 * @param encoding      the encoding
	 * @throws IOException if an io exception occurs
	 */
	HttpResponse(int status, String contentType, ByteBuffer[] body, String encoding) throws IOException {
		responseHeader = new HttpResponseHeader(status, contentType + "; charset=" + encoding);
		
		try {
			setBodyDataSource(body, encoding);
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}
		
		responseHeader.setContentLength(getNonBlockingBody().available());
	}	
	
		
	/**
	 * constructor 
	 * 
	 * @param responseHeader   the response header 
	 * @param body             the body
	 * @throws IOException if an io exception occurs  
	 */
	public HttpResponse(IHttpResponseHeader 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
	 * @throws IOException if an io exception occurs  
	 */
	public HttpResponse(IHttpResponseHeader responseHeader, byte[] 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
	 * @throws IOException if an io exception occurs  
	 */
	public HttpResponse(IHttpResponseHeader responseHeader, ByteBuffer[] body) throws IOException {
		this.responseHeader = responseHeader;
		
		try {
			setBodyDataSource(body, responseHeader.getCharacterEncoding());
		} catch (UnsupportedEncodingException use) {
			throw new RuntimeException(use.toString());
		}			
	}

			
	/**
	 * {@inheritDoc}
	 */
	public final int getStatus() {
		return responseHeader.getStatus();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final void setStatus(int status) {
		responseHeader.setStatus(status);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final String getReason() {
		return responseHeader.getReason();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void setReason(String reason) {
		responseHeader.setReason(reason);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public String getProtocol() {
		return responseHeader.getProtocol();
	}

	
	/**
	 * {@inheritDoc}
	 */
	public String getProtocolVersion() {
		return responseHeader.getProtocolVersion();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final void setProtocol(String protocol) {
		responseHeader.setProtocol(protocol);
	}
	

	/**
	 * returns the response header
	 *  
	 * @return the response header
	 */
	public final IHttpResponseHeader getResponseHeader() {
		return responseHeader;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public IHttpHeader getMessageHeader() {
		return getResponseHeader();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy