Coverage Report - org.apache.maven.wagon.tck.http.fixture.LatencyServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
LatencyServlet
0%
0/34
0%
0/4
0
 
 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  0
     {
 46  0
         this.latencyMs = latencyMs;
 47  0
     }
 48  
 
 49  
     @Override
 50  
     protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
 51  
         throws ServletException, IOException
 52  
     {
 53  0
         if ( latencyMs < 0 )
 54  
         {
 55  0
             System.out.println( "Starting infinite wait." );
 56  0
             synchronized ( this )
 57  
             {
 58  
                 try
 59  
                 {
 60  0
                     wait();
 61  
                 }
 62  0
                 catch ( InterruptedException e )
 63  
                 {
 64  0
                 }
 65  0
             }
 66  
 
 67  0
             return;
 68  
         }
 69  
 
 70  0
         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  0
         String realPath = getServletContext().getRealPath( path );
 75  0
         File f = new File( realPath );
 76  
 
 77  0
         FileInputStream in = null;
 78  0
         long total = 0;
 79  0
         long start = System.currentTimeMillis();
 80  
         try
 81  
         {
 82  0
             in = new FileInputStream( f );
 83  0
             OutputStream out = resp.getOutputStream();
 84  
 
 85  0
             System.out.println( "Starting high-latency transfer. This should take about "
 86  
                 + ( ( f.length() / BUFFER_SIZE * latencyMs / 1000 ) + ( latencyMs / 1000 ) ) + " seconds." );
 87  
 
 88  0
             int read = -1;
 89  0
             byte[] buf = new byte[BUFFER_SIZE];
 90  0
             while ( ( read = in.read( buf ) ) > -1 )
 91  
             {
 92  
                 try
 93  
                 {
 94  0
                     Thread.sleep( latencyMs );
 95  
                 }
 96  0
                 catch ( InterruptedException e )
 97  
                 {
 98  0
                     e.printStackTrace();
 99  0
                 }
 100  
 
 101  0
                 System.out.println( "Writing bytes " + total + "-" + ( total + read - 1 ) + " of " + f.length()
 102  
                     + ". Elapsed time so far: " + ( ( System.currentTimeMillis() - start ) / 1000 ) + " seconds" );
 103  
 
 104  0
                 out.write( buf, 0, read );
 105  
 
 106  0
                 total += read;
 107  
             }
 108  
         }
 109  
         finally
 110  
         {
 111  0
             IOUtil.close( in );
 112  0
         }
 113  
 
 114  0
         System.out.println( "High-latency transfer done in " + ( System.currentTimeMillis() - start ) + "ms" );
 115  0
     }
 116  
 
 117  
 }