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