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
035/**
036 * 
037 */
038public class LatencyServlet
039    extends HttpServlet
040{
041    private static Logger logger = Logger.getLogger( LatencyServlet.class );
042
043    private static final long serialVersionUID = 1L;
044
045    private static final int BUFFER_SIZE = 32;
046
047    private final int latencyMs;
048
049    public LatencyServlet( final int latencyMs )
050    {
051        this.latencyMs = latencyMs;
052    }
053
054    @Override
055    protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
056        throws ServletException, IOException
057    {
058        if ( latencyMs < 0 )
059        {
060            logger.info( "Starting infinite wait." );
061            synchronized ( this )
062            {
063                try
064                {
065                    wait();
066                }
067                catch ( InterruptedException e )
068                {
069                    // ignore
070                }
071            }
072
073            return;
074        }
075
076        String path = req.getPathInfo();
077
078        // ignore the servlet's path here, since the servlet path is really only to provide a
079        // binding for the servlet.
080        String realPath = getServletContext().getRealPath( path );
081        File f = new File( realPath );
082
083        FileInputStream in = null;
084        long total = 0;
085        long start = System.currentTimeMillis();
086        try
087        {
088            in = new FileInputStream( f );
089            OutputStream out = resp.getOutputStream();
090
091            logger.info( "Starting high-latency transfer. This should take about "
092                + ( ( f.length() / BUFFER_SIZE * latencyMs / 1000 ) + ( latencyMs / 1000 ) ) + " seconds." );
093
094            int read;
095            byte[] buf = new byte[BUFFER_SIZE];
096            while ( ( read = in.read( buf ) ) > -1 )
097            {
098                try
099                {
100                    Thread.sleep( latencyMs );
101                }
102                catch ( InterruptedException e )
103                {
104                    e.printStackTrace();
105                }
106
107                logger.info( "Writing bytes " + total + "-" + ( total + read - 1 ) + " of " + f.length()
108                    + ". Elapsed time so far: " + ( ( System.currentTimeMillis() - start ) / 1000 ) + " seconds" );
109
110                out.write( buf, 0, read );
111
112                total += read;
113            }
114        }
115        finally
116        {
117            IOUtil.close( in );
118        }
119
120        logger.info( "High-latency transfer done in " + ( System.currentTimeMillis() - start ) + "ms" );
121    }
122
123}