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