001package org.apache.maven.wagon.tck.http.fixture;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.IOException;
025import java.io.OutputStream;
026
027import javax.servlet.ServletException;
028import javax.servlet.http.HttpServlet;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031
032import org.apache.log4j.Logger;
033import org.codehaus.plexus.util.IOUtil;
034
035public class LatencyServlet
036    extends HttpServlet
037{
038    private static Logger logger = Logger.getLogger( LatencyServlet.class );
039
040    private static final long serialVersionUID = 1L;
041
042    private static final int BUFFER_SIZE = 32;
043
044    private final int latencyMs;
045
046    public LatencyServlet( final int latencyMs )
047    {
048        this.latencyMs = latencyMs;
049    }
050
051    @Override
052    protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
053        throws ServletException, IOException
054    {
055        if ( latencyMs < 0 )
056        {
057            logger.info( "Starting infinite wait." );
058            synchronized ( this )
059            {
060                try
061                {
062                    wait();
063                }
064                catch ( InterruptedException e )
065                {
066                }
067            }
068
069            return;
070        }
071
072        String path = req.getPathInfo();
073
074        // ignore the servlet's path here, since the servlet path is really only to provide a
075        // binding for the servlet.
076        String realPath = getServletContext().getRealPath( path );
077        File f = new File( realPath );
078
079        FileInputStream in = null;
080        long total = 0;
081        long start = System.currentTimeMillis();
082        try
083        {
084            in = new FileInputStream( f );
085            OutputStream out = resp.getOutputStream();
086
087            logger.info( "Starting high-latency transfer. This should take about "
088                + ( ( f.length() / BUFFER_SIZE * latencyMs / 1000 ) + ( latencyMs / 1000 ) ) + " seconds." );
089
090            int read = -1;
091            byte[] buf = new byte[BUFFER_SIZE];
092            while ( ( read = in.read( buf ) ) > -1 )
093            {
094                try
095                {
096                    Thread.sleep( latencyMs );
097                }
098                catch ( InterruptedException e )
099                {
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}