Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3a548523023be0fbc9dda7bf8331e4fbc7623147 (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
/*******************************************************************************
 * 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;

/**
 * The FileMolester will keep a certain amount of the file from the beginning, and return
 * garbage/gibberish for the rest of the file's content. The garbage is produced by shifting every
 * byte 1 bit to the left.
 * 
 * The idea for keeping some percentage is to support keeping xml headers / doctype etc.
 */
public class FileMolester extends BasicResourceDelivery {

	int keepLength;

	/**
	 * 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 keepLength - how many bytes in the beginning that will be unmolested
	 */
	public FileMolester(String theAlias, URI thePath, int keepLength) {
		super(theAlias, thePath);
		if (keepLength < 0)
			throw new IllegalArgumentException("keepLength must be >= 0 - was:" + Integer.valueOf(keepLength)); //$NON-NLS-1$
		this.keepLength = keepLength;
	}

	private static final long serialVersionUID = 1L;

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

	protected void doMolest(URLConnection conn, InputStream in, String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
		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));

				char buffer[] = new char[4096];
				int read;
				int written = 0;
				while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
					if (written + read > keepLength)
						molest(buffer, written, read);
					writer.write(buffer, 0, read);
					written += read;
				}
			} else {
				ServletOutputStream out = response.getOutputStream();

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

				byte buffer[] = new byte[4096];
				int read;
				int written = 0;
				while ((read = in.read(buffer, 0, buffer.length)) != -1) {
					if (written + read > keepLength)
						molest(buffer, written, read);
					out.write(buffer, 0, read);
					written += read;
				}
			}
		}
	}

	/** Molest a char buffer */
	private void molest(char[] buffer, int written, int read) {
		int start = keepLength - written;
		if (start > read)
			return;
		for (int i = start < 0 ? 0 : start; i < read; i++)
			buffer[i] = (char) (buffer[i] << 1);
	}

	/** Molest a byte buffer */
	private void molest(byte[] buffer, int written, int read) {
		int start = keepLength - written;
		if (start > read)
			return;
		for (int i = start < 0 ? 0 : start; i < read; i++)
			buffer[i] = (byte) (buffer[i] << 1);
	}
}

Back to the top