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

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

The 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.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;





/**
 * Implementation base for the messages
 * 
 * @author [email protected]
 */
abstract class AbstractHttpMessage  {
	
	enum BodyType { BOUND, CONNECTION_TERMINATED, CHUNKED, NO_BODY };
	
	private NonBlockingBodyDataSource bodyDataSource = null;


	abstract IHttpHeader getMessageHeader();

	
	
	/**
	 * sets the body data source 
	 * 
	 * @param body  the body data source 
	 */
	final void setBodyDataSource(NonBlockingBodyDataSource body) {
		this.bodyDataSource = body;
	}
	

	/**
	 * sets the body data source 
	 * 
	 * @param body       the body data source 
	 * @param encoding   the encoding
	 * @throws UnsupportedEncodingException  if the given encoding is not supported 
	 */
	final void setBodyDataSource(String body, String encoding) throws IOException, UnsupportedEncodingException {
		byte[] bytes = body.getBytes(encoding);
		setBodyDataSource(bytes, encoding);
	}

	
	/**
	 *	sets the body data source 
	 * 
	 * @param body       the body data source 
	 */
	final void setBodyDataSource(String body) throws IOException {
		if (body != null) {
			try {
				byte[] bytes = body.getBytes(getMessageHeader().getCharacterEncoding());
				setBodyDataSource(bytes, getMessageHeader().getCharacterEncoding());
			} catch (UnsupportedEncodingException use) {
				throw new RuntimeException(use.toString());
			}
		}
	}

	
	
	/**
	 *	sets the body data source 
	 * 
	 * @param body       the body data source 
	 */
	final void setBodyDataSource(FileChannel body)  throws IOException, UnsupportedEncodingException {

		setBodyDataSource(new ByteBuffer[] { body.map(MapMode.READ_ONLY, 0, body.size()) }, getMessageHeader().getCharacterEncoding());
	}
	
	/**
	 *	sets the body data source 
	 * 
	 * @param body       the body data source 
	 */
	final void setBodyDataSource(ReadableByteChannel body)  throws IOException, UnsupportedEncodingException {
		if (body != null) {
			setBodyDataSource(new StreamingBodyDatasource(getMessageHeader().getCharacterEncoding(), body));
		}
	}

	
	/**
	 * sets the body data source 
	 * 
	 * @param body       the body data source 
	 * @throws UnsupportedEncodingException  if the given encoding is not supported 
	 */
	final void setBodyDataSource(byte[] body) throws IOException, UnsupportedEncodingException {
		setBodyDataSource(body, null);
	}
	
	/**
	 * sets the body data source 
	 * 
	 * @param body       the body data source 
	 * @param encoding   the encoding
	 * @throws UnsupportedEncodingException  if the given encoding is not supported 
	 */
	final void setBodyDataSource(byte[] body, String encoding) throws IOException, UnsupportedEncodingException {
		setBodyDataSource(new ByteBuffer[] { ByteBuffer.wrap(body)}, encoding);
	}
	
	
	/**
	 * sets the body data source 
	 * 
	 * @param body       the body data source 
	 * @param encoding   the encoding
	 * @throws UnsupportedEncodingException  if the given encoding is not supported 
	 */
	final void setBodyDataSource(ByteBuffer[] body, String encoding) throws IOException, UnsupportedEncodingException {
		setBodyDataSource(new NonBlockingBodyDataSource(body, encoding));
		
		if (getMessageHeader().getContentType() != null) {
			String contentType = getMessageHeader().getContentType();
			String headerDefinedEncoding = getMessageHeader().getCharacterEncoding();
			
			if (!headerDefinedEncoding.equalsIgnoreCase(encoding)) {
				int pos = contentType.indexOf(";");
				if (encoding != null) {
					getMessageHeader().setContentType(contentType.substring(0, pos) + "; charset=" + encoding);	
				} else {
					getMessageHeader().setContentType(contentType.substring(0, pos));
				}
			}
			
		} else {
			getMessageHeader().setContentType("text/plain; charset=" + encoding);
		}
		
		if (!hasChunkedBody(getMessageHeader())) {
			getMessageHeader().setContentLength(getNonBlockingBody().available());
		}
	}

	
	private boolean hasChunkedBody(IHttpHeader messageHeader) {
		String transferEncoding = messageHeader.getTransferEncoding();
		if (transferEncoding == null) {
			return false;
		} else {
			return (transferEncoding.equalsIgnoreCase("chunked"));
		}
	}

	

	/**
	 * returns the non-blocking body 
	 * 
	 * @return the body or null if message is bodyless
	 */
	public final NonBlockingBodyDataSource getNonBlockingBody() throws IOException {
		return bodyDataSource;
	}

	
	/**
	 * returns the blocking body 
	 * 
	 * @return the body or null if message is bodyless
	 */
	public final BlockingBodyDataSource getBlockingBody() throws IOException {
		if (bodyDataSource == null) {
			return null;
		}
		return new BlockingBodyDataSource(bodyDataSource);
	}

	
	/**
	 * returns true if the message has a body
	 * 
	 * @return  true, if the message has a body
	 */
	public final boolean hasBody() {
		return bodyDataSource != null;
	}
	

	/**
	 * adds a raw header line 
	 * 
	 * @param line  the headerline
	 */
	public void addHeaderLine(String line) {
		getMessageHeader().addHeaderLine(line);
	}
	
	

	/**
	 * {@inheritDoc}
	 */
	public final void addHeader(String headername, String headervalue) {
		getMessageHeader().addHeader(headername, headervalue);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public final void setHeader(String headername, String headervalue) {
		getMessageHeader().setHeader(headername, headervalue);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public final void removeHeader(String headername) {
		getMessageHeader().removeHeader(headername);
	}
	
	

	/**
	 * {@inheritDoc}
	 */
	public void removeHopByHopHeaders() {
		getMessageHeader().removeHopByHopHeaders();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void removeHopByHopHeaders(String... headernames) {
		getMessageHeader().removeHopByHopHeaders(headernames);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public final boolean containsHeader(String headername) {
		return getMessageHeader().containsHeader(headername);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final boolean containsHeaderValue(String headername, String headervalue) {
		return getMessageHeader().containsHeaderValue(headername, headervalue);
	}

	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public final Enumeration getHeaderNames() {
		return getMessageHeader().getHeaderNames();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final Set getHeaderNameSet() {
		return getMessageHeader().getHeaderNameSet();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final String getHeader(String headername) {
		return getMessageHeader().getHeader(headername);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public final List getHeaderList(String headername) {
		return getMessageHeader().getHeaderList(headername);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public final Enumeration getHeaders(String headername) {
		return getMessageHeader().getHeaders(headername);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void setContentType(String type) {
		getMessageHeader().setContentType(type);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getContentType() {
		return getMessageHeader().getContentType();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public String getCharacterEncoding() {
		return getMessageHeader().getCharacterEncoding();
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void setContentLength(int length) {
		getMessageHeader().setContentLength(length);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public int getContentLength() {
		return getMessageHeader().getContentLength();
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void setTransferEncoding(String transferEncoding) {
		getMessageHeader().setTransferEncoding(transferEncoding);
		removeHeader("Content-length");
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public String getTransferEncoding() {
		return getMessageHeader().getTransferEncoding();
	}
	
	
	BodyType getBodyType() throws IOException {
		if (getNonBlockingBody() != null) {
			return getNonBlockingBody().getBodyType();
		} else {
			return BodyType.NO_BODY;
		}
	}
	

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		if (bodyDataSource == null) {
			return getMessageHeader().toString();
		} else {
			return getMessageHeader().toString() + bodyDataSource.toString();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy