| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package net.sf.arestc.testserver; |
| 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 |
|
import java.io.IOException; |
| 49 |
|
import java.net.InetSocketAddress; |
| 50 |
|
import java.net.ServerSocket; |
| 51 |
|
import java.net.Socket; |
| 52 |
|
import java.net.SocketAddress; |
| 53 |
|
import java.util.Collections; |
| 54 |
|
import java.util.HashSet; |
| 55 |
|
import java.util.Set; |
| 56 |
|
import java.util.concurrent.atomic.AtomicInteger; |
| 57 |
|
|
| 58 |
|
import javax.net.ssl.SSLContext; |
| 59 |
|
import javax.net.ssl.SSLServerSocketFactory; |
| 60 |
|
|
| 61 |
|
import org.apache.http.ConnectionReuseStrategy; |
| 62 |
|
import org.apache.http.HttpException; |
| 63 |
|
import org.apache.http.HttpServerConnection; |
| 64 |
|
import org.apache.http.impl.DefaultConnectionReuseStrategy; |
| 65 |
|
import org.apache.http.impl.DefaultHttpResponseFactory; |
| 66 |
|
import org.apache.http.impl.DefaultHttpServerConnection; |
| 67 |
|
import org.apache.http.localserver.EchoHandler; |
| 68 |
|
import org.apache.http.localserver.RandomHandler; |
| 69 |
|
import org.apache.http.params.BasicHttpParams; |
| 70 |
|
import org.apache.http.params.CoreConnectionPNames; |
| 71 |
|
import org.apache.http.params.CoreProtocolPNames; |
| 72 |
|
import org.apache.http.params.HttpParams; |
| 73 |
|
import org.apache.http.protocol.BasicHttpContext; |
| 74 |
|
import org.apache.http.protocol.BasicHttpProcessor; |
| 75 |
|
import org.apache.http.protocol.HttpContext; |
| 76 |
|
import org.apache.http.protocol.HttpRequestHandler; |
| 77 |
|
import org.apache.http.protocol.HttpRequestHandlerRegistry; |
| 78 |
|
import org.apache.http.protocol.HttpService; |
| 79 |
|
import org.apache.http.protocol.ResponseConnControl; |
| 80 |
|
import org.apache.http.protocol.ResponseContent; |
| 81 |
|
import org.apache.http.protocol.ResponseDate; |
| 82 |
|
import org.apache.http.protocol.ResponseServer; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
|
|
|
| 41% |
Uncovered Elements: 62 (105) |
Complexity: 29 |
Complexity Density: 0.45 |
|
| 93 |
|
public class LocalTestServer { |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
@see |
| 100 |
|
|
|
|
|
| 52.9% |
Uncovered Elements: 16 (34) |
Complexity: 10 |
Complexity Density: 0.4 |
|
| 101 |
|
public class RequestListener implements Runnable { |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 8 |
Complexity Density: 0.89 |
|
| 106 |
|
public class Worker implements Runnable { |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
private final HttpService httpservice; |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
private final HttpServerConnection conn; |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
@param |
| 118 |
|
|
| 119 |
|
@param |
| 120 |
|
|
| 121 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 122 |
1
|
public Worker(final HttpService httpservice,... |
| 123 |
|
final HttpServerConnection conn) { |
| 124 |
|
|
| 125 |
1
|
this.httpservice = httpservice; |
| 126 |
1
|
this.conn = conn; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
@see |
| 133 |
|
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 7 |
Complexity Density: 1 |
|
| 134 |
1
|
public void run() {... |
| 135 |
1
|
final HttpContext context = new BasicHttpContext(null); |
| 136 |
1
|
try { |
| 137 |
11
|
while (servicedSocket != null && conn.isOpen() |
| 138 |
|
&& !Thread.interrupted()) { |
| 139 |
11
|
httpservice.handleRequest(conn, context); |
| 140 |
|
} |
| 141 |
|
} catch (final IOException ex) { |
| 142 |
|
|
| 143 |
|
} catch (final HttpException ex) { |
| 144 |
|
|
| 145 |
|
} finally { |
| 146 |
1
|
workerThreads.remove(Thread.currentThread()); |
| 147 |
1
|
try { |
| 148 |
1
|
conn.shutdown(); |
| 149 |
|
} catch (final IOException ignore) { |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
private final Set<Thread> workerThreads = Collections |
| 158 |
|
.synchronizedSet(new HashSet<Thread>()); |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
@throws |
| 164 |
|
|
| 165 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 166 |
2
|
protected void accept() throws IOException {... |
| 167 |
|
|
| 168 |
2
|
final Socket socket = servicedSocket.accept(); |
| 169 |
1
|
acceptedConnections.incrementAndGet(); |
| 170 |
1
|
final DefaultHttpServerConnection conn = new DefaultHttpServerConnection(); |
| 171 |
1
|
conn.bind(socket, serverParams); |
| 172 |
|
|
| 173 |
|
|
| 174 |
1
|
final HttpService httpService = new HttpService(httpProcessor, |
| 175 |
|
reuseStrategy, new DefaultHttpResponseFactory()); |
| 176 |
1
|
httpService.setParams(serverParams); |
| 177 |
1
|
httpService.setHandlerResolver(handlerRegistry); |
| 178 |
|
|
| 179 |
|
|
| 180 |
1
|
final Thread t = new Thread(new Worker(httpService, conn)); |
| 181 |
1
|
workerThreads.add(t); |
| 182 |
1
|
t.setDaemon(true); |
| 183 |
1
|
t.start(); |
| 184 |
|
|
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 190 |
0
|
protected void cleanup() {... |
| 191 |
0
|
final Thread[] threads = workerThreads.toArray(new Thread[0]); |
| 192 |
0
|
for (final Thread thread : threads) { |
| 193 |
0
|
if (thread != null) { |
| 194 |
0
|
thread.interrupt(); |
| 195 |
|
} |
| 196 |
|
} |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
@see |
| 203 |
|
|
|
|
|
| 35.7% |
Uncovered Elements: 9 (14) |
Complexity: 7 |
Complexity Density: 0.7 |
|
| 204 |
1
|
public void run() {... |
| 205 |
|
|
| 206 |
1
|
try { |
| 207 |
2
|
while (servicedSocket != null |
| 208 |
|
&& listenerThread == Thread.currentThread() |
| 209 |
|
&& !Thread.interrupted()) { |
| 210 |
2
|
try { |
| 211 |
2
|
accept(); |
| 212 |
|
} catch (final Exception e) { |
| 213 |
0
|
final ServerSocket ssock = servicedSocket; |
| 214 |
0
|
if (ssock != null && !ssock.isClosed()) { |
| 215 |
0
|
System.out.println(LocalTestServer.this.toString() |
| 216 |
|
+ " could not accept"); |
| 217 |
0
|
e.printStackTrace(System.out); |
| 218 |
|
} |
| 219 |
|
|
| 220 |
0
|
break; |
| 221 |
|
} |
| 222 |
|
} |
| 223 |
|
} finally { |
| 224 |
0
|
cleanup(); |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
public final static InetSocketAddress TEST_SERVER_ADDR = new InetSocketAddress( |
| 236 |
|
"localhost", |
| 237 |
|
9090); |
| 238 |
|
|
| 239 |
|
|
| 240 |
|
private final HttpRequestHandlerRegistry handlerRegistry; |
| 241 |
|
|
| 242 |
|
|
| 243 |
|
private final ConnectionReuseStrategy reuseStrategy; |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
private final BasicHttpProcessor httpProcessor; |
| 250 |
|
|
| 251 |
|
|
| 252 |
|
private final HttpParams serverParams; |
| 253 |
|
|
| 254 |
|
|
| 255 |
|
private final SSLContext sslcontext; |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
protected volatile ServerSocket servicedSocket; |
| 259 |
|
|
| 260 |
|
|
| 261 |
|
protected volatile Thread listenerThread; |
| 262 |
|
|
| 263 |
|
|
| 264 |
|
private final AtomicInteger acceptedConnections = new AtomicInteger( |
| 265 |
|
0); |
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
@param |
| 271 |
|
|
| 272 |
|
@link |
| 273 |
|
|
| 274 |
|
@param |
| 275 |
|
|
| 276 |
|
@link |
| 277 |
|
|
| 278 |
|
@param |
| 279 |
|
|
| 280 |
|
@link |
| 281 |
|
@param |
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
| 285 |
1
|
public LocalTestServer(final BasicHttpProcessor proc,... |
| 286 |
|
final ConnectionReuseStrategy reuseStrat, final HttpParams params, |
| 287 |
|
final SSLContext sslcontext) { |
| 288 |
1
|
super(); |
| 289 |
1
|
handlerRegistry = new HttpRequestHandlerRegistry(); |
| 290 |
1
|
reuseStrategy = reuseStrat != null ? reuseStrat |
| 291 |
|
: newConnectionReuseStrategy(); |
| 292 |
1
|
httpProcessor = proc != null ? proc : newProcessor(); |
| 293 |
1
|
serverParams = params != null ? params : newDefaultParams(); |
| 294 |
1
|
this.sslcontext = sslcontext; |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
|
|
| 299 |
|
|
| 300 |
|
@param |
| 301 |
|
|
| 302 |
|
@link |
| 303 |
|
|
| 304 |
|
@param |
| 305 |
|
|
| 306 |
|
@link |
| 307 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 308 |
1
|
public LocalTestServer(final BasicHttpProcessor proc,... |
| 309 |
|
final HttpParams params) { |
| 310 |
1
|
this(proc, null, params, null); |
| 311 |
|
} |
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
@param |
| 317 |
|
|
| 318 |
|
@throws |
| 319 |
|
|
| 320 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 321 |
0
|
public void awaitTermination(final long timeMs) throws InterruptedException {... |
| 322 |
0
|
if (listenerThread != null) { |
| 323 |
0
|
listenerThread.join(timeMs); |
| 324 |
|
} |
| 325 |
|
} |
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
@return |
| 331 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 332 |
0
|
public int getAcceptedConnectionCount() {... |
| 333 |
0
|
return acceptedConnections.get(); |
| 334 |
|
} |
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
|
@return |
| 340 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 341 |
0
|
public SocketAddress getServiceAddress() {... |
| 342 |
0
|
final ServerSocket ssock = servicedSocket; |
| 343 |
0
|
if (ssock == null) { |
| 344 |
0
|
throw new IllegalStateException("not running"); |
| 345 |
|
} |
| 346 |
|
|
| 347 |
0
|
return ssock.getLocalSocketAddress(); |
| 348 |
|
} |
| 349 |
|
|
| 350 |
|
|
| 351 |
|
|
| 352 |
|
|
| 353 |
|
@return |
| 354 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 355 |
0
|
public String getServiceHostName() {... |
| 356 |
0
|
final ServerSocket ssock = servicedSocket; |
| 357 |
0
|
if (ssock == null) { |
| 358 |
0
|
throw new IllegalStateException("not running"); |
| 359 |
|
} |
| 360 |
|
|
| 361 |
0
|
return ((InetSocketAddress) ssock.getLocalSocketAddress()) |
| 362 |
|
.getHostName(); |
| 363 |
|
} |
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
@return |
| 369 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 370 |
0
|
public int getServicePort() {... |
| 371 |
0
|
final ServerSocket ssock = servicedSocket; |
| 372 |
0
|
if (ssock == null) { |
| 373 |
0
|
throw new IllegalStateException("not running"); |
| 374 |
|
} |
| 375 |
|
|
| 376 |
0
|
return ssock.getLocalPort(); |
| 377 |
|
} |
| 378 |
|
|
| 379 |
|
|
| 380 |
|
|
| 381 |
|
|
| 382 |
|
@return |
| 383 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 384 |
1
|
protected ConnectionReuseStrategy newConnectionReuseStrategy() {... |
| 385 |
1
|
return new DefaultConnectionReuseStrategy(); |
| 386 |
|
} |
| 387 |
|
|
| 388 |
|
|
| 389 |
|
|
| 390 |
|
|
| 391 |
|
@return |
| 392 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 393 |
1
|
protected HttpParams newDefaultParams() {... |
| 394 |
1
|
final HttpParams params = new BasicHttpParams(); |
| 395 |
1
|
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000) |
| 396 |
|
.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, |
| 397 |
|
8 * 1024) |
| 398 |
|
.setBooleanParameter( |
| 399 |
|
CoreConnectionPNames.STALE_CONNECTION_CHECK, false) |
| 400 |
|
.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) |
| 401 |
|
.setParameter(CoreProtocolPNames.ORIGIN_SERVER, |
| 402 |
|
"LocalTestServer/1.1"); |
| 403 |
1
|
return params; |
| 404 |
|
} |
| 405 |
|
|
| 406 |
|
|
| 407 |
|
|
| 408 |
|
|
| 409 |
|
@return |
| 410 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 411 |
1
|
protected BasicHttpProcessor newProcessor() {... |
| 412 |
|
|
| 413 |
1
|
final BasicHttpProcessor httpproc = new BasicHttpProcessor(); |
| 414 |
1
|
httpproc.addInterceptor(new ResponseDate()); |
| 415 |
1
|
httpproc.addInterceptor(new ResponseServer()); |
| 416 |
1
|
httpproc.addInterceptor(new ResponseContent()); |
| 417 |
1
|
httpproc.addInterceptor(new ResponseConnControl()); |
| 418 |
|
|
| 419 |
1
|
return httpproc; |
| 420 |
|
} |
| 421 |
|
|
| 422 |
|
|
| 423 |
|
|
| 424 |
|
|
| 425 |
|
@param |
| 426 |
|
|
| 427 |
|
@param |
| 428 |
|
|
| 429 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 430 |
8
|
public void register(final String pattern, final HttpRequestHandler handler) {... |
| 431 |
8
|
handlerRegistry.register(pattern, handler); |
| 432 |
|
} |
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
|
|
| 437 |
|
@link |
| 438 |
|
|
| 439 |
|
|
| 440 |
|
|
| 441 |
|
|
| 442 |
|
@link |
| 443 |
|
@link |
| 444 |
|
|
| 445 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 446 |
1
|
public void registerDefaultHandlers() {... |
| 447 |
1
|
handlerRegistry.register("/echo/*", new EchoHandler()); |
| 448 |
1
|
handlerRegistry.register("/random/*", new RandomHandler()); |
| 449 |
|
} |
| 450 |
|
|
| 451 |
|
|
| 452 |
|
@link |
| 453 |
|
|
| 454 |
|
|
| 455 |
|
@throws |
| 456 |
|
|
| 457 |
|
|
|
|
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
| 458 |
1
|
public void start() throws Exception {... |
| 459 |
1
|
if (servicedSocket != null) { |
| 460 |
0
|
throw new IllegalStateException(toString() + " already running"); |
| 461 |
|
} |
| 462 |
|
|
| 463 |
1
|
ServerSocket ssock; |
| 464 |
1
|
if (sslcontext != null) { |
| 465 |
0
|
final SSLServerSocketFactory sf = sslcontext |
| 466 |
|
.getServerSocketFactory(); |
| 467 |
0
|
ssock = sf.createServerSocket(); |
| 468 |
|
} else { |
| 469 |
1
|
ssock = new ServerSocket(); |
| 470 |
|
} |
| 471 |
|
|
| 472 |
1
|
ssock.setReuseAddress(true); |
| 473 |
1
|
ssock.bind(TEST_SERVER_ADDR); |
| 474 |
1
|
servicedSocket = ssock; |
| 475 |
|
|
| 476 |
1
|
listenerThread = new Thread(new RequestListener()); |
| 477 |
1
|
listenerThread.setDaemon(false); |
| 478 |
1
|
listenerThread.start(); |
| 479 |
|
} |
| 480 |
|
|
| 481 |
|
|
| 482 |
|
|
| 483 |
|
|
| 484 |
|
@throws |
| 485 |
|
|
| 486 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 487 |
0
|
public void stop() throws Exception {... |
| 488 |
0
|
if (servicedSocket == null) { |
| 489 |
0
|
return; |
| 490 |
|
} |
| 491 |
|
|
| 492 |
0
|
try { |
| 493 |
0
|
servicedSocket.close(); |
| 494 |
|
} catch (final IOException iox) { |
| 495 |
0
|
System.out.println("error stopping " + this); |
| 496 |
0
|
iox.printStackTrace(System.out); |
| 497 |
|
} finally { |
| 498 |
0
|
servicedSocket = null; |
| 499 |
|
} |
| 500 |
|
|
| 501 |
0
|
if (listenerThread != null) { |
| 502 |
0
|
listenerThread.interrupt(); |
| 503 |
|
} |
| 504 |
|
} |
| 505 |
|
|
| 506 |
|
|
| 507 |
|
|
| 508 |
|
|
| 509 |
|
@see |
| 510 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 511 |
0
|
@Override... |
| 512 |
|
public String toString() { |
| 513 |
0
|
final ServerSocket ssock = servicedSocket; |
| 514 |
0
|
final StringBuffer sb = new StringBuffer(80); |
| 515 |
0
|
sb.append("LocalTestServer/"); |
| 516 |
0
|
if (ssock == null) { |
| 517 |
0
|
sb.append("stopped"); |
| 518 |
|
} else { |
| 519 |
0
|
sb.append(ssock.getLocalSocketAddress()); |
| 520 |
|
} |
| 521 |
0
|
return sb.toString(); |
| 522 |
|
} |
| 523 |
|
|
| 524 |
|
|
| 525 |
|
|
| 526 |
|
|
| 527 |
|
@param |
| 528 |
|
|
| 529 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 530 |
0
|
public void unregister(final String pattern) {... |
| 531 |
0
|
handlerRegistry.unregister(pattern); |
| 532 |
|
} |
| 533 |
|
|
| 534 |
|
} |
| 535 |
|
|