13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www;
27 import jdk.internal.util.StaticProperty;
28
29 import java.io.*;
30 import java.net.FileNameMap;
31 import java.util.Hashtable;
32 import java.util.Enumeration;
33 import java.util.Properties;
34 import java.util.StringTokenizer;
35
36 public class MimeTable implements FileNameMap {
37 /** Keyed by content type, returns MimeEntries */
38 private Hashtable<String, MimeEntry> entries
39 = new Hashtable<String, MimeEntry>();
40
41 /** Keyed by file extension (with the .), returns MimeEntries */
42 private Hashtable<String, MimeEntry> extensionMap
43 = new Hashtable<String, MimeEntry>();
44
45 // Will be reset if in the platform-specific data file
46 private static String tempFileTemplate;
47
48 static {
49 java.security.AccessController.doPrivileged(
50 new java.security.PrivilegedAction<Void>() {
51 public Void run() {
52 tempFileTemplate =
150 MimeEntry entry = entries.get(type);
151 if (entry == null) {
152 // try a wildcard lookup
153 Enumeration<MimeEntry> e = entries.elements();
154 while (e.hasMoreElements()) {
155 MimeEntry wild = e.nextElement();
156 if (wild.matches(type)) {
157 return wild;
158 }
159 }
160 }
161
162 return entry;
163 }
164
165 /**
166 * Locate a MimeEntry by the file extension that has been associated
167 * with it. Parses general file names, and URLs.
168 */
169 public MimeEntry findByFileName(String fname) {
170 String ext = "";
171
172 int i = fname.lastIndexOf('#');
173
174 if (i > 0) {
175 fname = fname.substring(0, i - 1);
176 }
177
178 i = fname.lastIndexOf('.');
179 // REMIND: OS specific delimters appear here
180 i = Math.max(i, fname.lastIndexOf('/'));
181 i = Math.max(i, fname.lastIndexOf('?'));
182
183 if (i != -1 && fname.charAt(i) == '.') {
184 ext = fname.substring(i).toLowerCase();
185 }
186
187 return findByExt(ext);
188 }
189
190 /**
191 * Locate a MimeEntry by the file extension that has been associated
192 * with it.
193 */
194 public synchronized MimeEntry findByExt(String fileExtension) {
195 return extensionMap.get(fileExtension);
196 }
197
198 public synchronized MimeEntry findByDescription(String description) {
199 Enumeration<MimeEntry> e = elements();
200 while (e.hasMoreElements()) {
201 MimeEntry entry = e.nextElement();
202 if (description.equals(entry.getDescription())) {
203 return entry;
204 }
205 }
206
207 // We failed, now try treating description as type
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www;
27 import jdk.internal.util.StaticProperty;
28
29 import java.io.*;
30 import java.net.FileNameMap;
31 import java.util.Hashtable;
32 import java.util.Enumeration;
33 import java.util.Locale;
34 import java.util.Properties;
35 import java.util.StringTokenizer;
36
37 public class MimeTable implements FileNameMap {
38 /** Keyed by content type, returns MimeEntries */
39 private Hashtable<String, MimeEntry> entries
40 = new Hashtable<String, MimeEntry>();
41
42 /** Keyed by file extension (with the .), returns MimeEntries */
43 private Hashtable<String, MimeEntry> extensionMap
44 = new Hashtable<String, MimeEntry>();
45
46 // Will be reset if in the platform-specific data file
47 private static String tempFileTemplate;
48
49 static {
50 java.security.AccessController.doPrivileged(
51 new java.security.PrivilegedAction<Void>() {
52 public Void run() {
53 tempFileTemplate =
151 MimeEntry entry = entries.get(type);
152 if (entry == null) {
153 // try a wildcard lookup
154 Enumeration<MimeEntry> e = entries.elements();
155 while (e.hasMoreElements()) {
156 MimeEntry wild = e.nextElement();
157 if (wild.matches(type)) {
158 return wild;
159 }
160 }
161 }
162
163 return entry;
164 }
165
166 /**
167 * Locate a MimeEntry by the file extension that has been associated
168 * with it. Parses general file names, and URLs.
169 */
170 public MimeEntry findByFileName(String fname) {
171 String ext = getExtension(fname);
172 return findByExt(ext);
173 }
174
175 private static String getExtension(String fname) {
176 int fragmentIndex = fname.lastIndexOf('#');
177 if (fragmentIndex < 0) {
178 fragmentIndex = fname.length();
179 }
180
181 int queryIndex = fname.lastIndexOf('?', fragmentIndex);
182 if (queryIndex < 0) {
183 queryIndex = fragmentIndex;
184 }
185
186 int slashIndex = fname.lastIndexOf('/', queryIndex);
187 if (slashIndex < 0) {
188 slashIndex = 0;
189 }
190 fname = fname.substring(slashIndex, queryIndex);
191
192 int dotIndex = fname.lastIndexOf('.', queryIndex);
193 String result;
194 if (dotIndex >= 0) {
195 result = fname.substring(dotIndex); // include the '.'
196 } else {
197 result = ""; // or: result = "." + fname
198 }
199 return result.toLowerCase(Locale.ROOT);
200 }
201
202 /**
203 * Locate a MimeEntry by the file extension that has been associated
204 * with it.
205 */
206 public synchronized MimeEntry findByExt(String fileExtension) {
207 return extensionMap.get(fileExtension);
208 }
209
210 public synchronized MimeEntry findByDescription(String description) {
211 Enumeration<MimeEntry> e = elements();
212 while (e.hasMoreElements()) {
213 MimeEntry entry = e.nextElement();
214 if (description.equals(entry.getDescription())) {
215 return entry;
216 }
217 }
218
219 // We failed, now try treating description as type
|