001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.http;
022:
023: import java.io.IOException;
024: import java.nio.BufferUnderflowException;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import org.xsocket.connection.IDataHandler;
029: import org.xsocket.connection.IDisconnectHandler;
030: import org.xsocket.connection.INonBlockingConnection;
031: import org.xsocket.connection.http.AbstractHttpMessage.BodyType;
032:
033: /**
034: *
035: * plain implementation of a body data source
036: *
037: *
038: * @author grro@xsocket.org
039: */
040: class ConnectionTerminatedBodyDataSource extends
041: NonBlockingBodyDataSource implements IDataHandler,
042: IDisconnectHandler {
043:
044: private static final Logger LOG = Logger
045: .getLogger(ConnectionTerminatedBodyDataSource.class
046: .getName());
047:
048: /**
049: * constructor
050: *
051: * @param httpConnection the http connection
052: * @param header the message header
053: * @param encoding the encoding to use
054: */
055: public ConnectionTerminatedBodyDataSource(
056: AbstractHttpConnection httpConnection,
057: AbstractMessageHeader header, String encoding)
058: throws IOException {
059: super (encoding, httpConnection);
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: @Override
066: BodyType getBodyType() {
067: return BodyType.CONNECTION_TERMINATED;
068: }
069:
070: /**
071: * {@inheritDoc}
072: */
073: public boolean onData(INonBlockingConnection connection)
074: throws IOException, BufferUnderflowException {
075: if (LOG.isLoggable(Level.FINE)) {
076: LOG.fine("[" + connection.getId()
077: + "] reading received data into body");
078: }
079:
080: try {
081:
082: int available = connection.available();
083:
084: if (available > 0) {
085: append(connection.readByteBufferByLength(available));
086:
087: } else if (available == -1) {
088: getHttpConnection().removeBodyParser();
089: setComplete(true);
090: }
091:
092: } catch (BufferUnderflowException bue) {
093: throw bue;
094:
095: } catch (IOException ioe) {
096: if (LOG.isLoggable(Level.FINE)) {
097: LOG.fine("error occured by reading plain body "
098: + ioe.toString());
099: }
100:
101: getHttpConnection().destroy();
102: setComplete(true);
103: return true;
104: }
105:
106: return true;
107: }
108:
109: public boolean onDisconnect(INonBlockingConnection connection)
110: throws IOException {
111:
112: setComplete(true);
113: onUnderlyingHttpConnectionClosed();
114: return true;
115: }
116: }
|