View Javadoc
1   package org.apache.maven.wagon.tck.http.fixture;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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                      // ignore
72                  }
73              }
74  
75              return;
76          }
77  
78          String path = req.getPathInfo();
79  
80          // ignore the servlet's path here, since the servlet path is really only to provide a
81          // binding for the servlet.
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 }