This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
/
HttpTestServer.java
142 lines (126 loc) · 3.91 KB
/
HttpTestServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ch.qos.logback.ext.loggly;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerServer;
import org.simpleframework.transport.Server;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
/**
* HTTP test server that tracks the number of requests received
*/
public class HttpTestServer implements Container {
static private final int MAX_RXBUF_SIZE = 2048;
private int port;
private Connection connection;
private Server server;
AtomicInteger numRequests;
/**
* Initializes the HTTP server
* @param port
*/
public HttpTestServer(int port) {
this.port = port;
numRequests = new AtomicInteger(0);
}
/**
* Opens the HTTP server
* @throws IOException
*/
public void start() throws IOException {
stop();
this.server = new ContainerServer(this);
this.connection = new SocketConnection(server);
SocketAddress address = new InetSocketAddress(this.port);
this.connection.connect(address);
}
/**
* Closes the HTTP server
*/
public void stop() {
if (this.connection != null) {
try {
this.connection.close();
} catch (IOException e) {
}
}
}
/**
* Gets the number of requests received
* @return the request count
*/
public int requestCount() {
return numRequests.get();
}
/**
* Resets the request count
*/
public void clearRequests() {
numRequests.set(0);
}
/**
* Waits indefinitely for the specified number of requests to be received
* @param count the number of received requests to wait for
*/
public void waitForRequests(int count) {
waitForRequests(count, 0, 0);
}
/**
* Waits for the specified number of requests to be received
* @param count the number of received requests to wait for
* @param interval the wait time between polls, checking the receive count;
* use 0 to wait indefinitely
* @param maxPolls the maximum number of polls; use 0 for no limit
*/
public void waitForRequests(int count, long interval, int maxPolls) {
synchronized (this) {
while (requestCount() < count) {
System.out.println("requests " + requestCount() + "/" + count);
if (maxPolls > 0 && maxPolls-- > 0) {
break;
}
try {
this.wait(interval);
} catch (InterruptedException e) {
}
}
}
}
/**
* Handles incoming HTTP requests by responding with the
* message index and size of the received message.
* @see org.simpleframework.http.core.Container#handle(org.simpleframework.http.Request, org.simpleframework.http.Response)
*/
@Override
public void handle(Request request, Response response) {
try {
PrintStream body = response.getPrintStream();
long time = System.currentTimeMillis();
response.setValue("Content-Type", "text/html");
response.setValue("Server", "HttpTestServer/1.0 (Simple 4.0)");
response.setDate("Date", time);
response.setDate("Last-Modified", time);
ByteBuffer buf = ByteBuffer.allocate(MAX_RXBUF_SIZE);
int len = request.getByteChannel().read(buf);
int count = numRequests.incrementAndGet();
// warn if RX buffer exceeded (message truncated)
String warning = "";
if (len > MAX_RXBUF_SIZE) {
warning = "(" + (len - MAX_RXBUF_SIZE) + " bytes truncated)";
}
body.println("Request #" + count + "\n" + len + " bytes read" + warning);
body.close();
synchronized (this) {
notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}