diff options
author | Simon Kaegi | 2006-10-10 22:16:50 +0000 |
---|---|---|
committer | Simon Kaegi | 2006-10-10 22:16:50 +0000 |
commit | 9a363290e2ed3688a05b96583d3d7d979acbe0a0 (patch) | |
tree | 2edd8c1ee2072680d447a617700b5edfab7b0b31 /bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http | |
parent | 4c7eeb9bb6e478d6c744df2b800c4ba0eca02fe8 (diff) | |
download | rt.equinox.bundles-9a363290e2ed3688a05b96583d3d7d979acbe0a0.tar.gz rt.equinox.bundles-9a363290e2ed3688a05b96583d3d7d979acbe0a0.tar.xz rt.equinox.bundles-9a363290e2ed3688a05b96583d3d7d979acbe0a0.zip |
[bug 158171] Adding support to allow contribution of the HttpContext implementation. Use the "class" attribute to specify a class name instantiated via createExecutableExtension.
Diffstat (limited to 'bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http')
2 files changed, 167 insertions, 92 deletions
diff --git a/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/HttpContextManager.java b/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/HttpContextManager.java index 48fdbb462..f10e23e7e 100644 --- a/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/HttpContextManager.java +++ b/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/HttpContextManager.java @@ -17,27 +17,29 @@ 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 { +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 Map contextsMap = new HashMap(); - + private PackageAdmin packageAdmin; HttpService httpService; - - ExtensionPointTracker tracker; - - PackageAdmin packageAdmin; + private ExtensionPointTracker tracker; public HttpContextManager(HttpService httpService, PackageAdmin packageAdmin, IExtensionRegistry registry) { this.httpService = httpService; this.packageAdmin = packageAdmin; - tracker = new ExtensionPointTracker(registry, HTTPCONTEXTS_EXTENSION_POINT, null); + tracker = new ExtensionPointTracker(registry, HTTPCONTEXTS_EXTENSION_POINT, this); } public HttpContext getDefaultHttpContext(String bundleName) { @@ -47,7 +49,7 @@ public class HttpContextManager { public synchronized HttpContext getHttpContext(String httpContextName) { HttpContext context = (HttpContext) contextsMap.get(httpContextName); if (context == null) { - context = new HttpContextImpl(httpContextName); + context = new NamedHttpContextImpl(); contextsMap.put(httpContextName, context); } return context; @@ -76,16 +78,72 @@ public class HttpContextManager { } return null; } + + public void added(IExtension extension) { + IConfigurationElement[] elements = extension.getConfigurationElements(); + if (elements.length != 1 || !HTTPCONTEXT.equals(elements[0].getName())) + return; + + IConfigurationElement httpContextElement = elements[0]; + String httpContextName = httpContextElement.getAttribute(NAME); + if (httpContextName == null) + return; + + 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); + context = new DefaultHttpContextImpl(b, path); + } + + NamedHttpContextImpl namedContext = (NamedHttpContextImpl) getHttpContext(httpContextName); + namedContext.addHttpContext(extension, context); + } + + public void removed(IExtension extension) { + IConfigurationElement[] elements = extension.getConfigurationElements(); + if (elements.length != 1 || !HTTPCONTEXT.equals(elements[0].getName())) + return; + + IConfigurationElement httpContextElement = elements[0]; + String httpContextName = httpContextElement.getAttribute(NAME); + if (httpContextName == null) + return; + + NamedHttpContextImpl namedContext = (NamedHttpContextImpl) getHttpContext(httpContextName); + namedContext.removeHttpContext(extension); + } private class DefaultHttpContextImpl implements HttpContext { private Bundle bundle; private HttpContext delegate; + private String bundlePath; public DefaultHttpContextImpl(Bundle bundle) { this.bundle = bundle; delegate = httpService.createDefaultHttpContext(); } + public DefaultHttpContextImpl(Bundle b, String bundlePath) { + 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; + } + public String getMimeType(String arg0) { return delegate.getMimeType(arg0); } @@ -95,11 +153,19 @@ public class HttpContextManager { } public URL getResource(String resourceName) { - return bundle.getEntry(resourceName); + if (bundlePath == null) + return bundle.getEntry(resourceName); + + return bundle.getEntry(bundlePath + resourceName); } public Set getResourcePaths(String path) { - Enumeration entryPaths = bundle.getEntryPaths(path); + Enumeration entryPaths = null; + if (bundlePath == null) + entryPaths = bundle.getEntryPaths(path); + else + entryPaths = bundle.getEntryPaths(bundlePath + path); + if (entryPaths == null) return null; @@ -109,86 +175,4 @@ public class HttpContextManager { return result; } } - - private class HttpContextImpl implements HttpContext { - - private static final String PATH = "path"; //$NON-NLS-1$ - - private static final String HTTPCONTEXT = "httpcontext"; //$NON-NLS-1$ - - private static final String NAME = "name"; //$NON-NLS-1$ - - private HttpContext delegate; - - private String contextName; - - public HttpContextImpl(String contextName) { - this.contextName = contextName; - delegate = httpService.createDefaultHttpContext(); - } - - public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { - return delegate.handleSecurity(request, response); - } - - public URL getResource(String resourceName) { - IExtension[] extensions = tracker.getExtensions(); - for (int i = 0; i < extensions.length; i++) { - IConfigurationElement[] elements = extensions[i].getConfigurationElements(); - if (elements.length != 1 || !HTTPCONTEXT.equals(elements[0].getName())) - continue; - - IConfigurationElement httpContextElement = elements[0]; - String httpContextName = httpContextElement.getAttribute(NAME); - String path = httpContextElement.getAttribute(PATH); - - if (httpContextName.equals(contextName)) { - Bundle b = getBundle(extensions[i].getContributor().getName()); - if (path.endsWith("/")) { //$NON-NLS-1$ - path = path.substring(0, path.length() - 1); - } - - URL url = b.getEntry(path + resourceName); - if (url != null) { - return url; - } - } - } - return null; - } - - public Set getResourcePaths(String resourcePath) { - Set result = null; - IExtension[] extensions = tracker.getExtensions(); - for (int i = 0; i < extensions.length; i++) { - IConfigurationElement[] elements = extensions[i].getConfigurationElements(); - if (elements.length != 1 || !HTTPCONTEXT.equals(elements[0].getName())) - continue; - - IConfigurationElement httpContextElement = elements[0]; - String httpContextName = httpContextElement.getAttribute(NAME); - String path = httpContextElement.getAttribute(PATH); - - if (httpContextName.equals(contextName)) { - Bundle b = getBundle(extensions[i].getContributor().getName()); - if (path.endsWith("/")) { //$NON-NLS-1$ - path = path.substring(0, path.length() - 1); - } - - Enumeration entryPaths = b.getEntryPaths(path + resourcePath); - if (entryPaths != null) { - if (result == null) - result = new HashSet(); - while (entryPaths.hasMoreElements()) - result.add(entryPaths.nextElement()); - } - } - } - return result; - } - - public String getMimeType(String name) { - return delegate.getMimeType(name); - } - } } diff --git a/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/NamedHttpContextImpl.java b/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/NamedHttpContextImpl.java new file mode 100644 index 000000000..ba99f3aeb --- /dev/null +++ b/bundles/org.eclipse.equinox.http.registry/src/org/eclipse/equinox/http/registry/internal/NamedHttpContextImpl.java @@ -0,0 +1,91 @@ +package org.eclipse.equinox.http.registry.internal; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.eclipse.core.runtime.IExtension; +import org.osgi.service.http.HttpContext; + +public class NamedHttpContextImpl implements HttpContext { + + private Map httpContexts = new HashMap(); + private List snapshot = null; + + public String getMimeType(String name) { + List contexts = getSnapshot(); + for (Iterator it = contexts.iterator();it.hasNext();) { + HttpContext context = (HttpContext) it.next(); + String mimeType = context.getMimeType(name); + if (mimeType != null) + return mimeType; + } + return null; + } + + public URL getResource(String name) { + List contexts = getSnapshot(); + for (Iterator it = contexts.iterator();it.hasNext();) { + HttpContext context = (HttpContext) it.next(); + URL resourceURL = context.getResource(name); + if (resourceURL != null) + return resourceURL; + } + return null; + } + + public boolean handleSecurity(HttpServletRequest req, HttpServletResponse resp) throws IOException { + List contexts = getSnapshot(); + for (Iterator it = contexts.iterator();it.hasNext();) { + HttpContext context = (HttpContext) it.next(); + if (context.handleSecurity(req, resp) == false) + return false; + } + return true; + } + + public Set getResourcePaths(String name) { + if (name == null || !name.startsWith("/")) //$NON-NLS-1$ + return null; + + Set result = null; + List contexts = getSnapshot(); + for (Iterator it = contexts.iterator();it.hasNext();) { + HttpContext context = (HttpContext) it.next(); + try { + Method getResourcePathsMethod = context.getClass().getMethod("getResourcePaths", new Class[] {String.class}); //$NON-NLS-1$ + if (!getResourcePathsMethod.isAccessible()) + getResourcePathsMethod.setAccessible(true); + Set resourcePaths = (Set) getResourcePathsMethod.invoke(context, new Object[] {name}); + if (resourcePaths != null) { + if (result == null) + result = new HashSet(); + result.addAll(resourcePaths); + } + } catch (Exception e) { + // ignore + } + } + return result; + } + + public synchronized void addHttpContext(IExtension extension, HttpContext context) { + httpContexts.put(extension, context); + snapshot = null; + } + + public synchronized void removeHttpContext(IExtension extension) { + httpContexts.remove(extension); + snapshot = null; + } + + private synchronized List getSnapshot() { + if (snapshot == null) + snapshot = new ArrayList(httpContexts.values()); + + return snapshot; + } + +} |