/*
* Copyright (c) xlightweb.org, 2008 - 2009. 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.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xsocket.DataConverter;
import org.xsocket.MaxWriteSizeExceededException;
import org.xsocket.connection.IWriteCompletionHandler;
/**
* full message body writer
*
* @author grro@xlightweb.org
*/
final class FullMessageWriter extends SimpleMessageWriter {
private static final Logger LOG = Logger.getLogger(FullMessageWriter.class.getName());
private final int contentLength;
private int written = 0;
/**
* constructor
*
* @param httpConnection the http connection
* @param header the header
* @param contentLength the content length
* @throws IOException if an exception occurs
*/
public FullMessageWriter(AbstractHttpConnection httpConnection, boolean destroyOnClose, IHttpHeader header, int contentLength) throws IOException {
super(httpConnection, destroyOnClose, header);
this.contentLength = contentLength;
header.setContentLength(contentLength);
}
/**
* {@inheritDoc}
*/
@Override
protected int writeBody(AbstractHttpConnection con, ByteBuffer[] bodyData , IWriteCompletionHandler completionHandler) throws IOException {
written += super.writeBody(con, bodyData, completionHandler);
if (written > contentLength) {
throw new MaxWriteSizeExceededException();
}
return written;
}
/**
* {@inheritDoc}
*/
@Override
void onClose(boolean destroyOnClose) throws IOException {
try {
if (written != contentLength) {
IOException ioe = new IOException("content size is " + written + " byte, but content-length field declares " + contentLength + " bytes");
if (getConnection() != null) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("[" + getId()+ "] error occured by closing data sink " + ioe.getMessage());
}
getConnection().destroy();
}
throw ioe;
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine(DataConverter.toFormatedBytesSize(written) + " body data written");
}
if (destroyOnClose) {
getConnection().destroy();
}
} catch (IOException ioe) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("[" + getId()+ "] error occured by closing full message writer. Destroying connection. reason " + DataConverter.toString(ioe));
}
getConnection().destroy();
throw ioe;
}
}
}