Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c2b86e38e367a587b526609eb859adc4b1e4f655 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
 * Copyright (c) 2007 Cognos Incorporated, IBM Corporation.
 * 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
 *******************************************************************************/

package org.eclipse.equinox.http.registry.internal;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.http.registry.internal.ExtensionPointTracker.Listener;
import org.osgi.framework.Bundle;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.packageadmin.PackageAdmin;

public class HttpContextManager implements Listener {

	private static final String HTTPCONTEXTS_EXTENSION_POINT = "org.eclipse.equinox.http.registry.httpcontexts"; //$NON-NLS-1$
	private static final String HTTPCONTEXT = "httpcontext"; //$NON-NLS-1$
	private static final String NAME = "name"; //$NON-NLS-1$
	private static final String CLASS = "class"; //$NON-NLS-1$
	private static final String PATH = "path"; //$NON-NLS-1$
	private static final String MIMEMAPPING = "mime-mapping"; //$NON-NLS-1$
	private static final String MIMEEXTENSION = "extension"; //$NON-NLS-1$
	private static final String MIMETYPE = "mime-type"; //$NON-NLS-1$

	private Map contextsMap = new HashMap();
	private PackageAdmin packageAdmin;
	HttpService httpService;
	private ExtensionPointTracker tracker;

	public HttpContextManager(HttpService httpService, PackageAdmin packageAdmin, IExtensionRegistry registry) {
		this.httpService = httpService;
		this.packageAdmin = packageAdmin;
		tracker = new ExtensionPointTracker(registry, HTTPCONTEXTS_EXTENSION_POINT, this);
	}

	public HttpContext getDefaultHttpContext(String bundleName) {
		return new DefaultHttpContextImpl(getBundle(bundleName));
	}

	public synchronized HttpContext getHttpContext(String httpContextName) {
		HttpContext context = (HttpContext) contextsMap.get(httpContextName);
		if (context == null) {
			context = new NamedHttpContextImpl();
			contextsMap.put(httpContextName, context);
		}
		return context;
	}

	public void start() {
		tracker.open();
	}

	public void stop() {
		tracker.close();
		synchronized (this) {
			contextsMap.clear();
		}
	}

	private Bundle getBundle(String symbolicName) {
		Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
		if (bundles == null)
			return null;
		//Return the first bundle that is not installed or uninstalled
		for (int i = 0; i < bundles.length; i++) {
			if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
				return bundles[i];
			}
		}
		return null;
	}

	public void added(IExtension extension) {
		IConfigurationElement[] elements = extension.getConfigurationElements();
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement httpContextElement = elements[i];
			if (!HTTPCONTEXT.equals(httpContextElement.getName()))
				continue;

			String httpContextName = httpContextElement.getAttribute(NAME);
			if (httpContextName == null)
				continue;

			HttpContext context = null;
			String clazz = httpContextElement.getAttribute(CLASS);
			if (clazz != null) {
				try {
					context = (HttpContext) httpContextElement.createExecutableExtension(CLASS);
				} catch (CoreException e) {
					// log it.
					e.printStackTrace();
				}
			} else {
				Bundle b = getBundle(extension.getContributor().getName());
				String path = httpContextElement.getAttribute(PATH);
				Properties mimeMappings = null;
				IConfigurationElement[] mimeMappingElements = httpContextElement.getChildren(MIMEMAPPING);
				if (mimeMappingElements.length > 0) {
					mimeMappings = new Properties();
					for (int j = 0; j < mimeMappingElements.length; j++) {
						IConfigurationElement urlMappingElement = mimeMappingElements[i];
						String mimeExtension = urlMappingElement.getAttribute(MIMEEXTENSION);
						String mimeType = urlMappingElement.getAttribute(MIMETYPE);
						mimeMappings.put(mimeExtension, mimeType);
					}
				}				
				context = new DefaultHttpContextImpl(b, path, mimeMappings);
			}

			NamedHttpContextImpl namedContext = (NamedHttpContextImpl) getHttpContext(httpContextName);
			namedContext.addHttpContext(httpContextElement, context);
		}
	}

	public void removed(IExtension extension) {
		IConfigurationElement[] elements = extension.getConfigurationElements();
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement httpContextElement = elements[i];
			if (!HTTPCONTEXT.equals(httpContextElement.getName()))
				continue;
			String httpContextName = httpContextElement.getAttribute(NAME);
			if (httpContextName == null)
				continue;

			NamedHttpContextImpl namedContext = (NamedHttpContextImpl) getHttpContext(httpContextName);
			namedContext.removeHttpContext(httpContextElement);
		}
	}

	private class DefaultHttpContextImpl implements HttpContext {
		private Bundle bundle;
		private HttpContext delegate;
		private String bundlePath;
		private Properties mimeMappings;

		public DefaultHttpContextImpl(Bundle bundle) {
			this.bundle = bundle;
			delegate = httpService.createDefaultHttpContext();
		}

		public DefaultHttpContextImpl(Bundle b, String bundlePath, Properties mimeMappings) {
			this(b);
			if (bundlePath != null) {
				if (bundlePath.endsWith("/")) //$NON-NLS-1$
					bundlePath = bundlePath.substring(0, bundlePath.length() - 1);

				if (bundlePath.length() == 0)
					bundlePath = null;
			}
			this.bundlePath = bundlePath;
			this.mimeMappings = mimeMappings;
		}

		public String getMimeType(String name) {
			if (mimeMappings != null) {
				int dotIndex = name.lastIndexOf('.');
				if (dotIndex != -1) {
					String mimeExtension = name.substring(dotIndex + 1);
					String mimeType = mimeMappings.getProperty(mimeExtension);
					if (mimeType != null)
						return mimeType;
				}
			}			
			return delegate.getMimeType(name);
		}

		public boolean handleSecurity(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
			return delegate.handleSecurity(arg0, arg1);
		}

		public URL getResource(String resourceName) {
			if (bundlePath != null)
				resourceName = bundlePath + resourceName;

			int lastSlash = resourceName.lastIndexOf('/');
			if (lastSlash == -1)
				return null;

			String path = resourceName.substring(0, lastSlash);
			if (path.length() == 0)
				path = "/"; //$NON-NLS-1$
			String file = resourceName.substring(lastSlash + 1);
			Enumeration entryPaths = bundle.findEntries(path, file, false);

			if (entryPaths != null && entryPaths.hasMoreElements())
				return (URL) entryPaths.nextElement();

			return null;
		}

		public Set getResourcePaths(String path) {
			if (bundlePath != null)
				path = bundlePath + path;

			Enumeration entryPaths = bundle.findEntries(path, null, false);
			if (entryPaths == null)
				return null;

			Set result = new HashSet();
			while (entryPaths.hasMoreElements()) {
				URL entryURL = (URL) entryPaths.nextElement();
				String entryPath = entryURL.getFile();

				if (bundlePath == null)
					result.add(entryPath);
				else
					result.add(entryPath.substring(bundlePath.length()));
			}
			return result;
		}
	}
}

Back to the top