/*
* Copyright (c) xlightweb.org, 2006 - 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.BufferUnderflowException;
import java.util.logging.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.xlightweb.client.HttpClientConnection;
import org.xlightweb.server.HttpServer;
/**
*
* @author grro@xlightweb.org
*/
public class MultipartDataRecordTest {
@Test
public void testSimple() throws Exception {
HttpServer server = new HttpServer(new RequestHandler());
server.start();
HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
IHttpResponse resp = con.call(new GetRequest("/"));
NonBlockingBodyDataSource body = resp.getNonBlockingBody();
BodyDataHandler dh = new BodyDataHandler();
body.setDataHandler(dh);
do {
QAUtil.sleep(200);
} while (dh.getRecords() < 2);
Assert.assertNull(dh.getException());
do {
QAUtil.sleep(200);
} while (!body.isComplete());
Logger LOG = Logger.getLogger("org.xlightweb.Test");
LOG.fine("closing con");
con.close();
LOG.fine("closing server");
server.close();
}
private static final class BodyDataHandler implements IBodyDataHandler {
private IOException exception = null;
private int records = 0;
public boolean onData(NonBlockingBodyDataSource nbc) {
try {
////////////
// "transaction" start
//
// mark the read position
nbc.markReadPosition();
// try to read the header data (BufferUnderflowException can been thrown by any read method)
byte recordType = nbc.readByte();
int version = nbc.readInt();
String data = nbc.readStringByDelimiter("\r\n");
// got the complete header (BufferUnderflowException hasn't been thrown) -> remove read mark
nbc.removeReadMark();
//
// "transaction" end
///////////////
if (version == 1) {
handleRecord(recordType, data);
// ..
} else {
}
} catch (BufferUnderflowException bue) {
// reset to origin read position
nbc.resetToReadMark();
} catch (IOException ioe) {
exception = ioe;
nbc.destroy();
}
return true;
}
private void handleRecord(byte recordType, String data) {
System.out.println("[" + + recordType + "] " + data);
records++;
}
int getRecords() {
return records;
}
IOException getException() {
return exception;
}
}
@Test
public void testSimple2() throws Exception {
HttpServer server = new HttpServer(new RequestHandler());
server.start();
HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
IHttpResponse resp = con.call(new GetRequest("/"));
NonBlockingBodyDataSource body = resp.getNonBlockingBody();
BodyDataHandler2 dh = new BodyDataHandler2();
body.setDataHandler(dh);
do {
QAUtil.sleep(200);
} while (dh.getRecords() < 2);
Assert.assertNull(dh.getException());
con.close();
server.close();
}
private static final class BodyDataHandler2 implements IBodyDataHandler {
private IOException exception = null;
private int records = 0;
public boolean onData(NonBlockingBodyDataSource nbc) throws BufferUnderflowException {
try {
////////////
// "transaction" start
//
// reset to origin read position
nbc.resetToReadMark();
// mark the read position
nbc.markReadPosition();
// try to read the header data (BufferUnderflowException can been thrown by any read method)
byte recordType = nbc.readByte();
int version = nbc.readInt();
String data = nbc.readStringByDelimiter("\r\n");
// got the complete header (BufferUnderflowException hasn't been thrown) -> remove read mark
nbc.removeReadMark();
//
// "transaction" end
///////////////
if (version == 1) {
handleRecord(recordType, data);
// ..
} else {
}
} catch (IOException ioe) {
exception = ioe;
nbc.destroy();
}
return true;
}
private void handleRecord(byte recordType, String data) {
System.out.println("[" + + recordType + "] " + data);
records++;
}
int getRecords() {
return records;
}
IOException getException() {
return exception;
}
}
private static final class RequestHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
BodyDataSink dataSink = exchange.send(new HttpResponseHeader(200));
dataSink.write((byte) 5);
QAUtil.sleep(200);
dataSink.write(1);
QAUtil.sleep(200);
dataSink.write("test123456\r\n");
QAUtil.sleep(200);
dataSink.write((byte) 9);
QAUtil.sleep(200);
dataSink.write(1);
QAUtil.sleep(200);
dataSink.write("999555\r\n");
QAUtil.sleep(200);
dataSink.close();
}
}
}