Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a2495974f44cda7f7a94e694e07ab017c373ae8 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 Cognos Incorporated, IBM Corporation 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:
 *     Cognos Incorporated - initial API and implementation
 *     IBM Corporation - bug fixes and enhancements
 *******************************************************************************/
package org.eclipse.equinox.http.servlet.internal;

import java.io.IOException;
import java.util.Set;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.osgi.framework.Bundle;
import org.osgi.service.http.HttpContext;

//This class wraps the servlet object registered in the HttpService.registerServlet call, to manage the context classloader when handleRequests are being asked.
//It is also responsible to ensure that a given servlet has only been registered once. 
public class ServletRegistration extends Registration {

	private Servlet servlet; //The actual servlet object registered against the http service. All requests will eventually be delegated to it.
	private HttpContext httpContext; //The context used during the registration of the servlet
	private Set servlets; //All the servlets registered against the instance of the proxy servlet that "ownes" self.
	private ClassLoader registeredContextClassLoader;
	private ProxyContext proxyContext;

	public ServletRegistration(Servlet servlet, ProxyContext proxyContext, HttpContext context, Bundle bundle, Set servlets) {
		this.servlet = servlet;
		this.servlets = servlets;
		this.httpContext = context;
		this.proxyContext = proxyContext;
		registeredContextClassLoader = Thread.currentThread().getContextClassLoader();
	}

	public void destroy() {
		ClassLoader original = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(registeredContextClassLoader);
			super.destroy();
			servlet.destroy();
		} finally {
			Thread.currentThread().setContextClassLoader(original);
		}		
	}

	public void close() {		
		servlets.remove(servlet);
		proxyContext.destroyContextAttributes(httpContext);
	}

	//Delegate the init call to the actual servlet
	public void init(ServletConfig servletConfig) throws ServletException {
		boolean initialized = false;
		proxyContext.createContextAttributes(httpContext);
		try {
			ClassLoader original = Thread.currentThread().getContextClassLoader();
			try {
				Thread.currentThread().setContextClassLoader(registeredContextClassLoader);
				servlet.init(servletConfig);
			} finally {
				Thread.currentThread().setContextClassLoader(original);
			}
			servlets.add(servlet);
			initialized = true;
		} finally {
			if (! initialized)
				proxyContext.destroyContextAttributes(httpContext);
		}
	}

	public void checkServletRegistration() throws ServletException {
		if (servlets.contains(servlet)) {
			throw new ServletException("This servlet has already been registered at a different alias."); //$NON-NLS-1$
		}
	}

	//Delegate the handling of the request to the actual servlet
	public boolean handleRequest(HttpServletRequest req, HttpServletResponse resp, String alias) throws IOException, ServletException {
		HttpServletRequest wrappedRequest = new HttpServletRequestAdaptor(req, alias, servlet);

		if (httpContext.handleSecurity(wrappedRequest, resp)) {
			ClassLoader original = Thread.currentThread().getContextClassLoader();
			try {
				Thread.currentThread().setContextClassLoader(registeredContextClassLoader);
				servlet.service(wrappedRequest, resp);
			} finally {
				Thread.currentThread().setContextClassLoader(original);
			}
		}
		return true;
	}
}

Back to the top