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