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