Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fa5a167484ea37047930aed51be2e0110f43cbe (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
/*******************************************************************************
 * Copyright (c) 2009, 2018 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;

import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.osgi.service.http.HttpContext;

/**
 * The AlwaysFailContext can be used to add basic authentication to a path. This
 * implementation will always fail (easier to test failure logic) this way.
 */
public class AlwaysFailContext implements HttpContext {
	private final HttpContext theDefaultContext;

	public AlwaysFailContext(HttpContext defaultContext) {
		theDefaultContext = defaultContext;
	}

	@Override
	public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) {
		// always fail
		response.setHeader("WWW-Authenticate", "BASIC realm=\"p2 Http Testing Server (Aladdin, open sesame)\""); //$NON-NLS-1$//$NON-NLS-2$
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		return false;

	}

	@Override
	public String getMimeType(String name) {
		return theDefaultContext.getMimeType(name);
	}

	@Override
	public URL getResource(String name) {
		return theDefaultContext.getResource(name);
	}

}

Back to the top