Skip to main content
summaryrefslogtreecommitdiffstats
blob: 98a0377369d05f3dd71f2af36457243df1bd544b (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
/*******************************************************************************
 * 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.net.URI;
import java.net.URLConnection;

public class ContentLengthLier extends BasicResourceDelivery {

	double keepFactor;

	/**
	 * The ContentLengthLier sets the content length to a percentage of the original length.
	 * Values between 0 and 200 can be used (to lie about files being both smaller < 100, or larger > 100).
	 * 
	 * @param theAlias
	 * @param thePath
	 * @param keepPercent - how much to lie between 0 and 200 (inclusive)
	 */
	public ContentLengthLier(String theAlias, URI thePath, int keepPercent) {
		super(theAlias, thePath);
		if (keepPercent < 0 || keepPercent > 200)
			throw new IllegalArgumentException("keepPercent must be between 0 and 200 - was:" + Integer.valueOf(keepPercent)); //$NON-NLS-1$
		keepFactor = keepPercent / 100.0;
	}

	private static final long serialVersionUID = 1L;

	protected int getContentLength(URLConnection conn) {
		int contentLength = conn.getContentLength();
		return (contentLength >= 0) ? (int) (contentLength * keepFactor) : contentLength;

	}

}

Back to the top