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 {
46 this.latencyMs = latencyMs;
47 }
48
49 @Override
50 protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
51 throws ServletException, IOException
52 {
53 if ( latencyMs < 0 )
54 {
55 System.out.println( "Starting infinite wait." );
56 synchronized ( this )
57 {
58 try
59 {
60 wait();
61 }
62 catch ( InterruptedException e )
63 {
64 }
65 }
66
67 return;
68 }
69
70 String path = req.getPathInfo();
71
72
73
74 String realPath = getServletContext().getRealPath( path );
75 File f = new File( realPath );
76
77 FileInputStream in = null;
78 long total = 0;
79 long start = System.currentTimeMillis();
80 try
81 {
82 in = new FileInputStream( f );
83 OutputStream out = resp.getOutputStream();
84
85 System.out.println( "Starting high-latency transfer. This should take about "
86 + ( ( f.length() / BUFFER_SIZE * latencyMs / 1000 ) + ( latencyMs / 1000 ) ) + " seconds." );
87
88 int read = -1;
89 byte[] buf = new byte[BUFFER_SIZE];
90 while ( ( read = in.read( buf ) ) > -1 )
91 {
92 try
93 {
94 Thread.sleep( latencyMs );
95 }
96 catch ( InterruptedException e )
97 {
98 e.printStackTrace();
99 }
100
101 System.out.println( "Writing bytes " + total + "-" + ( total + read - 1 ) + " of " + f.length()
102 + ". Elapsed time so far: " + ( ( System.currentTimeMillis() - start ) / 1000 ) + " seconds" );
103
104 out.write( buf, 0, read );
105
106 total += read;
107 }
108 }
109 finally
110 {
111 IOUtil.close( in );
112 }
113
114 System.out.println( "High-latency transfer done in " + ( System.currentTimeMillis() - start ) + "ms" );
115 }
116
117 }