KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > registry > internal > DefaultRegistryHttpContext


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.equinox.http.registry.internal;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.*;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc path) {
31         if (resourceMappings == null)
32             resourceMappings = new ArrayList();
33
34         resourceMappings.add(new ResourceMapping(contributingBundle, path));
35     }
36
37     public void addMimeMapping(String JavaDoc mimeExtension, String JavaDoc mimeType) {
38         if (mimeMappings == null)
39             mimeMappings = new Properties();
40
41         mimeMappings.put(mimeExtension, mimeType);
42     }
43
44     public String JavaDoc getMimeType(String JavaDoc name) {
45         if (mimeMappings != null) {
46             int dotIndex = name.lastIndexOf('.');
47             if (dotIndex != -1) {
48                 String JavaDoc mimeExtension = name.substring(dotIndex + 1);
49                 String JavaDoc 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 JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws IOException JavaDoc {
58         return delegate.handleSecurity(arg0, arg1);
59     }
60
61     public URL JavaDoc getResource(String JavaDoc 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 JavaDoc resourceURL = mapping.getResource(name);
68             if (resourceURL != null)
69                 return resourceURL;
70         }
71         return null;
72     }
73
74     public Set getResourcePaths(String JavaDoc path) {
75         if (resourceMappings == null || path == null || !path.startsWith("/")) //$NON-NLS-1$
76
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 JavaDoc bundlePath;
94
95         public ResourceMapping(Bundle bundle, String JavaDoc path) {
96             this.bundle = bundle;
97             if (path != null) {
98                 if (path.endsWith("/")) //$NON-NLS-1$
99
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 JavaDoc getResource(String JavaDoc 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 JavaDoc path = resourceName.substring(0, lastSlash);
116             if (path.length() == 0)
117                 path = "/"; //$NON-NLS-1$
118
String JavaDoc file = resourceName.substring(lastSlash + 1);
119             Enumeration entryPaths = bundle.findEntries(path, file, false);
120
121             if (entryPaths != null && entryPaths.hasMoreElements())
122                 return (URL JavaDoc) entryPaths.nextElement();
123
124             return null;
125         }
126
127         public Set getResourcePaths(String JavaDoc 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 JavaDoc entryURL = (URL JavaDoc) entryPaths.nextElement();
138                 String JavaDoc 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