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