1
1
package fi .helsinki .cs .tmc .testing ;
2
2
3
3
import java .io .IOException ;
4
- import java .io .InterruptedIOException ;
5
4
import java .net .InetSocketAddress ;
6
5
import java .net .ServerSocket ;
7
6
import java .net .Socket ;
8
7
import java .net .SocketException ;
9
8
import java .util .concurrent .Semaphore ;
10
9
import org .apache .http .ConnectionClosedException ;
11
10
import org .apache .http .HttpResponseInterceptor ;
12
- import org .apache .http .impl .DefaultConnectionReuseStrategy ;
13
- import org .apache .http .impl .DefaultHttpResponseFactory ;
14
- import org .apache .http .impl .DefaultHttpServerConnection ;
15
- import org .apache .http .params .SyncBasicHttpParams ;
11
+ import org .apache .http .impl .DefaultBHttpServerConnection ;
16
12
import org .apache .http .protocol .BasicHttpContext ;
17
13
import org .apache .http .protocol .HttpContext ;
18
14
import org .apache .http .protocol .HttpProcessor ;
19
15
import org .apache .http .protocol .HttpRequestHandler ;
20
- import org .apache .http .protocol .HttpRequestHandlerRegistry ;
21
- import org .apache .http .protocol .HttpRequestHandlerResolver ;
16
+ import org .apache .http .protocol .HttpRequestHandlerMapper ;
22
17
import org .apache .http .protocol .HttpService ;
23
18
import org .apache .http .protocol .ImmutableHttpProcessor ;
24
19
import org .apache .http .protocol .ResponseConnControl ;
25
20
import org .apache .http .protocol .ResponseContent ;
26
21
import org .apache .http .protocol .ResponseDate ;
27
22
import org .apache .http .protocol .ResponseServer ;
23
+ import org .apache .http .protocol .UriHttpRequestHandlerMapper ;
28
24
import org .openide .util .Exceptions ;
29
25
30
- /**
31
- * A HTTP server running on a random port in a single background thread.
32
- *
33
- * An exception in a request handler will cause the server to shut down.
34
- * The exception will be propagated when {@link #stop()} is called.
35
- */
36
26
public class AdHocHttpServer {
37
- private HttpRequestHandlerResolver handlers ;
38
-
27
+
28
+ /**
29
+ * A HTTP server running on a random port in a single background thread.
30
+ *
31
+ * An exception in a request handler will cause the server to shut down. The
32
+ * exception will be propagated when {@link #stop()} is called.
33
+ */
34
+ private HttpRequestHandlerMapper handlers ;
35
+
39
36
private ServerSocket serverSocket ;
40
37
private HttpService httpService ;
41
38
private Thread thread ;
42
-
39
+
43
40
private Exception inThreadException ; // Set by thread, read in stop()
44
41
private Semaphore requestCounter = new Semaphore (0 );
45
-
42
+
46
43
private volatile boolean debugEnabled = false ;
47
44
48
45
public AdHocHttpServer () {
49
- this .handlers = new HttpRequestHandlerRegistry ();
46
+ this .handlers = new UriHttpRequestHandlerMapper ();
50
47
}
51
-
48
+
52
49
public void enableDebug () {
53
50
debugEnabled = true ;
54
51
}
55
-
52
+
56
53
public void setHandler (HttpRequestHandler handler ) {
57
- HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry ();
54
+ UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper ();
58
55
registry .register ("*" , handler );
59
56
this .handlers = registry ;
60
57
}
61
-
58
+
62
59
public boolean isStarted () {
63
60
return thread != null ;
64
61
}
65
-
62
+
66
63
public int getPort () {
67
64
if (!isStarted ()) {
68
65
throw new IllegalStateException ("Server must be started first" );
69
66
}
70
67
return serverSocket .getLocalPort ();
71
68
}
72
-
69
+
73
70
public String getBaseUrl () {
74
71
return "http://localhost:" + getPort ();
75
72
}
76
-
73
+
77
74
public synchronized void start () throws IOException {
78
75
if (isStarted ()) {
79
76
throw new IllegalStateException ("Already started" );
80
77
}
81
-
78
+
82
79
setupServerSocket ();
83
80
setupHttpClientIncantations ();
84
81
startThread ();
@@ -91,22 +88,18 @@ private void setupServerSocket() throws IOException {
91
88
serverSocket .setReceiveBufferSize (64 );
92
89
serverSocket .bind (addr );
93
90
}
94
-
91
+
95
92
private void setupHttpClientIncantations () {
96
- HttpProcessor proc = new ImmutableHttpProcessor (new HttpResponseInterceptor [] {
93
+ HttpProcessor proc = new ImmutableHttpProcessor (new HttpResponseInterceptor []{
97
94
new ResponseDate (),
98
95
new ResponseServer (),
99
96
new ResponseContent (),
100
97
new ResponseConnControl ()
101
98
});
102
-
99
+
103
100
httpService = new HttpService (
104
101
proc ,
105
- new DefaultConnectionReuseStrategy (),
106
- new DefaultHttpResponseFactory (),
107
- handlers ,
108
- new SyncBasicHttpParams ()
109
- );
102
+ handlers );
110
103
}
111
104
112
105
private void startThread () {
@@ -153,8 +146,8 @@ public void run() {
153
146
}
154
147
155
148
debug ("Got connection" );
156
- DefaultHttpServerConnection conn = new DefaultHttpServerConnection ( );
157
- conn .bind (socket , httpService . getParams () );
149
+ DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection ( 5000 );
150
+ conn .bind (socket );
158
151
HttpContext ctx = new BasicHttpContext (null );
159
152
while (!Thread .currentThread ().isInterrupted () && conn .isOpen ()) {
160
153
httpService .handleRequest (conn , ctx );
@@ -163,9 +156,6 @@ public void run() {
163
156
debug ("Connection processed" );
164
157
} catch (ConnectionClosedException ex ) {
165
158
// No problem I think
166
- } catch (InterruptedIOException ex ) {
167
- debug ("InterruptedIOException: " + ex );
168
- break ;
169
159
} catch (Exception ex ) {
170
160
inThreadException = ex ;
171
161
debug ("Exception: " + ex );
0 commit comments