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