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.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                      // ignore
70                  }
71              }
72  
73              return;
74          }
75  
76          String path = req.getPathInfo();
77  
78          // ignore the servlet's path here, since the servlet path is really only to provide a
79          // binding for the servlet.
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 }