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  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          // ignore the servlet's path here, since the servlet path is really only to provide a
75          // binding for the servlet.
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 }