1 11 package org.eclipse.equinox.http.registry.internal; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.util.*; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 import org.osgi.framework.Bundle; 19 import org.osgi.service.http.HttpContext; 20 21 public class DefaultRegistryHttpContext implements HttpContext { 22 private HttpContext delegate; 23 private List resourceMappings; 24 private Properties mimeMappings; 25 26 public DefaultRegistryHttpContext(HttpContext delegate) { 27 this.delegate = delegate; 28 } 29 30 public void addResourceMapping(Bundle contributingBundle, String path) { 31 if (resourceMappings == null) 32 resourceMappings = new ArrayList(); 33 34 resourceMappings.add(new ResourceMapping(contributingBundle, path)); 35 } 36 37 public void addMimeMapping(String mimeExtension, String mimeType) { 38 if (mimeMappings == null) 39 mimeMappings = new Properties(); 40 41 mimeMappings.put(mimeExtension, mimeType); 42 } 43 44 public String getMimeType(String name) { 45 if (mimeMappings != null) { 46 int dotIndex = name.lastIndexOf('.'); 47 if (dotIndex != -1) { 48 String mimeExtension = name.substring(dotIndex + 1); 49 String mimeType = mimeMappings.getProperty(mimeExtension); 50 if (mimeType != null) 51 return mimeType; 52 } 53 } 54 return delegate.getMimeType(name); 55 } 56 57 public boolean handleSecurity(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException { 58 return delegate.handleSecurity(arg0, arg1); 59 } 60 61 public URL getResource(String name) { 62 if (resourceMappings == null) 63 return null; 64 65 for (Iterator it = resourceMappings.iterator(); it.hasNext();) { 66 ResourceMapping mapping = (ResourceMapping) it.next(); 67 URL resourceURL = mapping.getResource(name); 68 if (resourceURL != null) 69 return resourceURL; 70 } 71 return null; 72 } 73 74 public Set getResourcePaths(String path) { 75 if (resourceMappings == null || path == null || !path.startsWith("/")) return null; 77 78 Set result = null; 79 for (Iterator it = resourceMappings.iterator(); it.hasNext();) { 80 ResourceMapping mapping = (ResourceMapping) it.next(); 81 Set resourcePaths = mapping.getResourcePaths(path); 82 if (resourcePaths != null) { 83 if (result == null) 84 result = new HashSet(); 85 result.addAll(resourcePaths); 86 } 87 } 88 return result; 89 } 90 91 public static class ResourceMapping { 92 private Bundle bundle; 93 private String bundlePath; 94 95 public ResourceMapping(Bundle bundle, String path) { 96 this.bundle = bundle; 97 if (path != null) { 98 if (path.endsWith("/")) path = path.substring(0, path.length() - 1); 100 101 if (path.length() == 0) 102 path = null; 103 } 104 this.bundlePath = path; 105 } 106 107 public URL getResource(String resourceName) { 108 if (bundlePath != null) 109 resourceName = bundlePath + resourceName; 110 111 int lastSlash = resourceName.lastIndexOf('/'); 112 if (lastSlash == -1) 113 return null; 114 115 String path = resourceName.substring(0, lastSlash); 116 if (path.length() == 0) 117 path = "/"; String file = resourceName.substring(lastSlash + 1); 119 Enumeration entryPaths = bundle.findEntries(path, file, false); 120 121 if (entryPaths != null && entryPaths.hasMoreElements()) 122 return (URL ) entryPaths.nextElement(); 123 124 return null; 125 } 126 127 public Set getResourcePaths(String path) { 128 if (bundlePath != null) 129 path = bundlePath + path; 130 131 Enumeration entryPaths = bundle.findEntries(path, null, false); 132 if (entryPaths == null) 133 return null; 134 135 Set result = new HashSet(); 136 while (entryPaths.hasMoreElements()) { 137 URL entryURL = (URL ) entryPaths.nextElement(); 138 String entryPath = entryURL.getFile(); 139 140 if (bundlePath == null) 141 result.add(entryPath); 142 else 143 result.add(entryPath.substring(bundlePath.length())); 144 } 145 return result; 146 } 147 } 148 } | Popular Tags |