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