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

org.xsocket.connection.http.BoundBodyDataSink 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.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.logging.Level;
import java.util.logging.Logger;


import org.xsocket.MaxWriteSizeExceededException;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IConnection.FlushMode;
import org.xsocket.connection.http.AbstractHttpMessage.BodyType;




/**
 * 
 * plain implementation of a body data sink
 *  
 * @author [email protected]
 */
final class BoundBodyDataSink extends BodyDataSink {

	private static final Logger LOG = Logger.getLogger(BoundBodyDataSink.class.getName());
	
	
	private AbstractHttpConnection httpConnection = null;
	private INonBlockingConnection tcpConnection = null;
	private AbstractMessageHeader header = null;
	private boolean isHeaderWritten = false;
		
	private int contentLength = 0;
	private int written = 0;
	
	
	/**
	 * constructor
	 * 
	 * @param httpConnection   the http connection
	 * @param header           the message header
	 * @param encoding         the encoding to use
	 * @throws IOException if the content length has not be set
	 */
	public BoundBodyDataSink(AbstractHttpConnection httpConnection, AbstractMessageHeader header, String encoding) throws IOException {
		super(httpConnection);
		
		this.httpConnection = httpConnection;
		this.tcpConnection = httpConnection.getUnderlyingConnection();
		this.header = header;
		
		setEncoding(encoding);
		
		if (header.getContentLength() < 0) {
			throw new IOException("Content Length has to be set");
		}
		
		contentLength = header.getContentLength();
	}
	
	
	@Override
	BodyType getBodyType() {
		return BodyType.BOUND;
	}
	
	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onWriteDataInserted() throws IOException, ClosedChannelException {
		if (isAutoflush()) {
			flush();
		}
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void flush() throws IOException {
		removeWriteMark();
		
		int dataWritten = 0;
		
		if (!isHeaderWritten) {
			isHeaderWritten = true;
			
			if (LOG.isLoggable(Level.FINER)) {
				LOG.finer("[" + httpConnection.getId() + "] sending " + header.toString());
			}
			
			dataWritten = header.writeTo(tcpConnection);
			dataWritten += writeBody();
			
		} else {
			dataWritten = writeBody();
		}

		
		// data to write? -> flushing
		if (dataWritten > 0) {
			try {
				tcpConnection.setFlushmode(this.getFlushmode());
				tcpConnection.flush();
			} finally {
				tcpConnection.setFlushmode(FlushMode.ASYNC);
			}
		}
	}
	
	
	private int writeBody() throws IOException {

		int dataWritten = 0;
		
		ByteBuffer[] body = drainWriteQueue();
		if (body != null) {

			dataWritten = (int) tcpConnection.write(body);
			written += dataWritten;
		}

		if (written > contentLength) {
			throw new MaxWriteSizeExceededException();
		}
		
		return dataWritten;
	}
	
	
	
	@Override
	void finalizeBody() throws IOException {

		flush();
		
		if (written != contentLength) {
			httpConnection.destroy();
			throw new IOException("content size is " + written + " byte, but content-length field declares " + contentLength + " bytes"); 
		}
		
		if (LOG.isLoggable(Level.FINE)) {
			LOG.fine("[" + getHttpConnection().getId()+ "] closing (all data " + contentLength + " written)");
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return printWriteBuffer(getEncoding());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy