Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9359eacb0937f27ba8c64e60d3e72cda4d2b73b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 * Copyright (c) 2009, Cloudsmith Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.p2.testserver.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URLConnection;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.equinox.p2.testserver.LinearChange;

/**
 * The ChopAndDelay deliver the content chopped up in smaller packets and adds delay
 * between packets.
 * 
 */
public class ChopAndDelay extends BasicResourceDelivery {

	int chopFactor;
	private LinearChange delayFunction;
	private long msDelay;

	/**
	 * Create a file molester that turns content into gibberish.
	 * 
	 * @param theAlias the path this servlet is registered under
	 * @param thePath the path to use as root for the alias
	 * @param chopFactor - a value between 1 and 12 where 1 is one byte, and 12 is 4k bytes at a time.
	 * @param delayFunction - function returning a series of delay values
	 */
	public ChopAndDelay(String theAlias, URI thePath, int chopFactor, LinearChange delayFunction) {
		super(theAlias, thePath);
		if (chopFactor < 1 || chopFactor > 12)
			throw new IllegalArgumentException("chopFactor must be between 1 and 12 (inclusive) - was:" + Integer.valueOf(chopFactor)); //$NON-NLS-1$
		this.chopFactor = chopFactor;
		if (msDelay < 0)
			throw new IllegalArgumentException("msDelay must be >= 0 - was:" + Integer.valueOf(chopFactor)); //$NON-NLS-1$
		this.delayFunction = delayFunction;
		msDelay = 0L;
		this.delayFunction = delayFunction;
		if (this.delayFunction.hasNext())
			msDelay = delayFunction.next();
	}

	private static final long serialVersionUID = 1L;

	protected void deliver(URLConnection conn, InputStream in, String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
		// chop up all files
		doChop(conn, in, filename, request, response);
	}

	protected void doChop(URLConnection conn, InputStream in, String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
		LinearChange delayer = delayFunction.fork();
		int contentlength = conn.getContentLength();
		if (contentlength >= 0) {
			response.setContentLength(contentlength);

			String mimeType = computeMimeType(filename, conn);
			response.setContentType(mimeType);

			// We want to use a writer if we are sending text
			if (mimeType.startsWith("text/")) //$NON-NLS-1$
			{
				PrintWriter writer = response.getWriter();

				writer.flush(); /* write the headers and unbuffer the output */

				BufferedReader reader = new BufferedReader(new InputStreamReader(in));

				int chunksize = 1 << chopFactor;
				char buffer[] = new char[4096];
				int read;
				while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
					int nChunks = read / chunksize + (read % chunksize > 0 ? 1 : 0);
					for (int i = 0; i < nChunks; i++) {
						writer.write(buffer, i * chunksize, Math.min(chunksize, read - i * chunksize));
						writer.flush();
						if (msDelay > 0)
							try {
								Thread.sleep(msDelay);
							} catch (InterruptedException e) {
								// ignore
							}
						if (delayer.hasNext())
							msDelay = delayer.next();
					}
				}
			} else {
				ServletOutputStream out = response.getOutputStream();

				out.flush(); /* write the headers and unbuffer the output */

				int chunksize = 1 << chopFactor;
				byte buffer[] = new byte[4096];
				int read;
				while ((read = in.read(buffer, 0, buffer.length)) != -1) {
					int nChunks = read / chunksize + (read % chunksize > 0 ? 1 : 0);
					for (int i = 0; i < nChunks; i++) {
						out.write(buffer, i * chunksize, Math.min(chunksize, read - i * chunksize));
						out.flush();
						if (msDelay > 0)
							try {
								Thread.sleep(msDelay);
							} catch (InterruptedException e) {
								// ignore
							}
						if (delayer.hasNext())
							msDelay = delayer.next();
					}
				}
			}
		}
	}

}

Back to the top