1 | |
package org.apache.maven.wagon.tck.http.fixture; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.codehaus.plexus.util.IOUtil; |
23 | |
|
24 | |
import java.io.File; |
25 | |
import java.io.FileInputStream; |
26 | |
import java.io.IOException; |
27 | |
import java.io.OutputStream; |
28 | |
|
29 | |
import javax.servlet.ServletException; |
30 | |
import javax.servlet.http.HttpServlet; |
31 | |
import javax.servlet.http.HttpServletRequest; |
32 | |
import javax.servlet.http.HttpServletResponse; |
33 | |
|
34 | |
public class LatencyServlet |
35 | |
extends HttpServlet |
36 | |
{ |
37 | |
|
38 | |
private static final long serialVersionUID = 1L; |
39 | |
|
40 | |
private static final int BUFFER_SIZE = 32; |
41 | |
|
42 | |
private final int latencyMs; |
43 | |
|
44 | |
public LatencyServlet( final int latencyMs ) |
45 | 0 | { |
46 | 0 | this.latencyMs = latencyMs; |
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
protected void doGet( final HttpServletRequest req, final HttpServletResponse resp ) |
51 | |
throws ServletException, IOException |
52 | |
{ |
53 | 0 | if ( latencyMs < 0 ) |
54 | |
{ |
55 | 0 | System.out.println( "Starting infinite wait." ); |
56 | 0 | synchronized ( this ) |
57 | |
{ |
58 | |
try |
59 | |
{ |
60 | 0 | wait(); |
61 | |
} |
62 | 0 | catch ( InterruptedException e ) |
63 | |
{ |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | 0 | return; |
68 | |
} |
69 | |
|
70 | 0 | String path = req.getPathInfo(); |
71 | |
|
72 | |
|
73 | |
|
74 | 0 | String realPath = getServletContext().getRealPath( path ); |
75 | 0 | File f = new File( realPath ); |
76 | |
|
77 | 0 | FileInputStream in = null; |
78 | 0 | long total = 0; |
79 | 0 | long start = System.currentTimeMillis(); |
80 | |
try |
81 | |
{ |
82 | 0 | in = new FileInputStream( f ); |
83 | 0 | OutputStream out = resp.getOutputStream(); |
84 | |
|
85 | 0 | System.out.println( "Starting high-latency transfer. This should take about " |
86 | |
+ ( ( f.length() / BUFFER_SIZE * latencyMs / 1000 ) + ( latencyMs / 1000 ) ) + " seconds." ); |
87 | |
|
88 | 0 | int read = -1; |
89 | 0 | byte[] buf = new byte[BUFFER_SIZE]; |
90 | 0 | while ( ( read = in.read( buf ) ) > -1 ) |
91 | |
{ |
92 | |
try |
93 | |
{ |
94 | 0 | Thread.sleep( latencyMs ); |
95 | |
} |
96 | 0 | catch ( InterruptedException e ) |
97 | |
{ |
98 | 0 | e.printStackTrace(); |
99 | 0 | } |
100 | |
|
101 | 0 | System.out.println( "Writing bytes " + total + "-" + ( total + read - 1 ) + " of " + f.length() |
102 | |
+ ". Elapsed time so far: " + ( ( System.currentTimeMillis() - start ) / 1000 ) + " seconds" ); |
103 | |
|
104 | 0 | out.write( buf, 0, read ); |
105 | |
|
106 | 0 | total += read; |
107 | |
} |
108 | |
} |
109 | |
finally |
110 | |
{ |
111 | 0 | IOUtil.close( in ); |
112 | 0 | } |
113 | |
|
114 | 0 | System.out.println( "High-latency transfer done in " + ( System.currentTimeMillis() - start ) + "ms" ); |
115 | 0 | } |
116 | |
|
117 | |
} |