Wed, 27 Apr 2016 15:32:20 -0700
Added tag jdk8u102-b05 for changeset 1f512419ae33
ohair@286 | 1 | /* |
mkos@494 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | |
ohair@286 | 27 | package javax.activation; |
ohair@286 | 28 | |
ohair@286 | 29 | import java.util.*; |
ohair@286 | 30 | import java.io.*; |
ohair@286 | 31 | import java.net.*; |
ohair@286 | 32 | import com.sun.activation.registries.MailcapFile; |
ohair@286 | 33 | import com.sun.activation.registries.LogSupport; |
ohair@286 | 34 | |
ohair@286 | 35 | /** |
ohair@286 | 36 | * MailcapCommandMap extends the CommandMap |
ohair@286 | 37 | * abstract class. It implements a CommandMap whose configuration |
ohair@286 | 38 | * is based on mailcap files |
ohair@286 | 39 | * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>). |
ohair@286 | 40 | * The MailcapCommandMap can be configured both programmatically |
ohair@286 | 41 | * and via configuration files. |
ohair@286 | 42 | * <p> |
ohair@286 | 43 | * <b>Mailcap file search order:</b><p> |
ohair@286 | 44 | * The MailcapCommandMap looks in various places in the user's |
ohair@286 | 45 | * system for mailcap file entries. When requests are made |
ohair@286 | 46 | * to search for commands in the MailcapCommandMap, it searches |
ohair@286 | 47 | * mailcap files in the following order: |
ohair@286 | 48 | * <p> |
ohair@286 | 49 | * <ol> |
ohair@286 | 50 | * <li> Programatically added entries to the MailcapCommandMap instance. |
ohair@286 | 51 | * <li> The file <code>.mailcap</code> in the user's home directory. |
ohair@286 | 52 | * <li> The file <<i>java.home</i>><code>/lib/mailcap</code>. |
ohair@286 | 53 | * <li> The file or resources named <code>META-INF/mailcap</code>. |
ohair@286 | 54 | * <li> The file or resource named <code>META-INF/mailcap.default</code> |
ohair@286 | 55 | * (usually found only in the <code>activation.jar</code> file). |
ohair@286 | 56 | * </ol> |
ohair@286 | 57 | * <p> |
ohair@286 | 58 | * <b>Mailcap file format:</b><p> |
ohair@286 | 59 | * |
ohair@286 | 60 | * Mailcap files must conform to the mailcap |
ohair@286 | 61 | * file specification (RFC 1524, <i>A User Agent Configuration Mechanism |
ohair@286 | 62 | * For Multimedia Mail Format Information</i>). |
ohair@286 | 63 | * The file format consists of entries corresponding to |
ohair@286 | 64 | * particular MIME types. In general, the specification |
ohair@286 | 65 | * specifies <i>applications</i> for clients to use when they |
ohair@286 | 66 | * themselves cannot operate on the specified MIME type. The |
ohair@286 | 67 | * MailcapCommandMap extends this specification by using a parameter mechanism |
ohair@286 | 68 | * in mailcap files that allows JavaBeans(tm) components to be specified as |
ohair@286 | 69 | * corresponding to particular commands for a MIME type.<p> |
ohair@286 | 70 | * |
ohair@286 | 71 | * When a mailcap file is |
ohair@286 | 72 | * parsed, the MailcapCommandMap recognizes certain parameter signatures, |
ohair@286 | 73 | * specifically those parameter names that begin with <code>x-java-</code>. |
ohair@286 | 74 | * The MailcapCommandMap uses this signature to find |
ohair@286 | 75 | * command entries for inclusion into its registries. |
ohair@286 | 76 | * Parameter names with the form <code>x-java-<name></code> |
ohair@286 | 77 | * are read by the MailcapCommandMap as identifying a command |
ohair@286 | 78 | * with the name <i>name</i>. When the <i>name</i> is <code> |
ohair@286 | 79 | * content-handler</code> the MailcapCommandMap recognizes the class |
ohair@286 | 80 | * signified by this parameter as a <i>DataContentHandler</i>. |
ohair@286 | 81 | * All other commands are handled generically regardless of command |
ohair@286 | 82 | * name. The command implementation is specified by a fully qualified |
ohair@286 | 83 | * class name of a JavaBean(tm) component. For example; a command for viewing |
ohair@286 | 84 | * some data can be specified as: <code>x-java-view=com.foo.ViewBean</code>.<p> |
ohair@286 | 85 | * |
ohair@286 | 86 | * When the command name is <code>fallback-entry</code>, the value of |
ohair@286 | 87 | * the command may be <code>true</code> or <code>false</code>. An |
ohair@286 | 88 | * entry for a MIME type that includes a parameter of |
ohair@286 | 89 | * <code>x-java-fallback-entry=true</code> defines fallback commands |
ohair@286 | 90 | * for that MIME type that will only be used if no non-fallback entry |
ohair@286 | 91 | * can be found. For example, an entry of the form <code>text/*; ; |
ohair@286 | 92 | * x-java-fallback-entry=true; x-java-view=com.sun.TextViewer</code> |
ohair@286 | 93 | * specifies a view command to be used for any text MIME type. This |
ohair@286 | 94 | * view command would only be used if a non-fallback view command for |
ohair@286 | 95 | * the MIME type could not be found.<p> |
ohair@286 | 96 | * |
ohair@286 | 97 | * MailcapCommandMap aware mailcap files have the |
ohair@286 | 98 | * following general form:<p> |
ohair@286 | 99 | * <code> |
ohair@286 | 100 | * # Comments begin with a '#' and continue to the end of the line.<br> |
ohair@286 | 101 | * <mime type>; ; <parameter list><br> |
ohair@286 | 102 | * # Where a parameter list consists of one or more parameters,<br> |
ohair@286 | 103 | * # where parameters look like: x-java-view=com.sun.TextViewer<br> |
ohair@286 | 104 | * # and a parameter list looks like: <br> |
ohair@286 | 105 | * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit |
ohair@286 | 106 | * <br> |
ohair@286 | 107 | * # Note that mailcap entries that do not contain 'x-java' parameters<br> |
ohair@286 | 108 | * # and comply to RFC 1524 are simply ignored:<br> |
ohair@286 | 109 | * image/gif; /usr/dt/bin/sdtimage %s<br> |
ohair@286 | 110 | * |
ohair@286 | 111 | * </code> |
ohair@286 | 112 | * <p> |
ohair@286 | 113 | * |
ohair@286 | 114 | * @author Bart Calder |
ohair@286 | 115 | * @author Bill Shannon |
ohair@286 | 116 | * |
ohair@286 | 117 | * @since 1.6 |
ohair@286 | 118 | */ |
ohair@286 | 119 | |
ohair@286 | 120 | public class MailcapCommandMap extends CommandMap { |
ohair@286 | 121 | /* |
ohair@286 | 122 | * We manage a collection of databases, searched in order. |
ohair@286 | 123 | */ |
ohair@286 | 124 | private MailcapFile[] DB; |
ohair@286 | 125 | private static final int PROG = 0; // programmatically added entries |
ohair@286 | 126 | |
ohair@286 | 127 | /** |
ohair@286 | 128 | * The default Constructor. |
ohair@286 | 129 | */ |
ohair@286 | 130 | public MailcapCommandMap() { |
ohair@286 | 131 | super(); |
ohair@286 | 132 | List dbv = new ArrayList(5); // usually 5 or less databases |
ohair@286 | 133 | MailcapFile mf = null; |
ohair@286 | 134 | dbv.add(null); // place holder for PROG entry |
ohair@286 | 135 | |
ohair@286 | 136 | LogSupport.log("MailcapCommandMap: load HOME"); |
ohair@286 | 137 | try { |
ohair@286 | 138 | String user_home = System.getProperty("user.home"); |
ohair@286 | 139 | |
ohair@286 | 140 | if (user_home != null) { |
ohair@286 | 141 | String path = user_home + File.separator + ".mailcap"; |
ohair@286 | 142 | mf = loadFile(path); |
ohair@286 | 143 | if (mf != null) |
ohair@286 | 144 | dbv.add(mf); |
ohair@286 | 145 | } |
ohair@286 | 146 | } catch (SecurityException ex) {} |
ohair@286 | 147 | |
ohair@286 | 148 | LogSupport.log("MailcapCommandMap: load SYS"); |
ohair@286 | 149 | try { |
ohair@286 | 150 | // check system's home |
ohair@286 | 151 | String system_mailcap = System.getProperty("java.home") + |
ohair@286 | 152 | File.separator + "lib" + File.separator + "mailcap"; |
ohair@286 | 153 | mf = loadFile(system_mailcap); |
ohair@286 | 154 | if (mf != null) |
ohair@286 | 155 | dbv.add(mf); |
ohair@286 | 156 | } catch (SecurityException ex) {} |
ohair@286 | 157 | |
ohair@286 | 158 | LogSupport.log("MailcapCommandMap: load JAR"); |
ohair@286 | 159 | // load from the app's jar file |
ohair@286 | 160 | loadAllResources(dbv, "META-INF/mailcap"); |
ohair@286 | 161 | |
ohair@286 | 162 | LogSupport.log("MailcapCommandMap: load DEF"); |
mkos@494 | 163 | mf = loadResource("/META-INF/mailcap.default"); |
ohair@286 | 164 | |
mkos@494 | 165 | if (mf != null) |
mkos@494 | 166 | dbv.add(mf); |
ohair@286 | 167 | |
ohair@286 | 168 | DB = new MailcapFile[dbv.size()]; |
ohair@286 | 169 | DB = (MailcapFile[])dbv.toArray(DB); |
ohair@286 | 170 | } |
ohair@286 | 171 | |
ohair@286 | 172 | /** |
ohair@286 | 173 | * Load from the named resource. |
ohair@286 | 174 | */ |
ohair@286 | 175 | private MailcapFile loadResource(String name) { |
ohair@286 | 176 | InputStream clis = null; |
ohair@286 | 177 | try { |
ohair@286 | 178 | clis = SecuritySupport.getResourceAsStream(this.getClass(), name); |
ohair@286 | 179 | if (clis != null) { |
ohair@286 | 180 | MailcapFile mf = new MailcapFile(clis); |
ohair@286 | 181 | if (LogSupport.isLoggable()) |
ohair@286 | 182 | LogSupport.log("MailcapCommandMap: successfully loaded " + |
ohair@286 | 183 | "mailcap file: " + name); |
ohair@286 | 184 | return mf; |
ohair@286 | 185 | } else { |
ohair@286 | 186 | if (LogSupport.isLoggable()) |
ohair@286 | 187 | LogSupport.log("MailcapCommandMap: not loading " + |
ohair@286 | 188 | "mailcap file: " + name); |
ohair@286 | 189 | } |
ohair@286 | 190 | } catch (IOException e) { |
ohair@286 | 191 | if (LogSupport.isLoggable()) |
ohair@286 | 192 | LogSupport.log("MailcapCommandMap: can't load " + name, e); |
ohair@286 | 193 | } catch (SecurityException sex) { |
ohair@286 | 194 | if (LogSupport.isLoggable()) |
ohair@286 | 195 | LogSupport.log("MailcapCommandMap: can't load " + name, sex); |
ohair@286 | 196 | } finally { |
ohair@286 | 197 | try { |
ohair@286 | 198 | if (clis != null) |
ohair@286 | 199 | clis.close(); |
ohair@286 | 200 | } catch (IOException ex) { } // ignore it |
ohair@286 | 201 | } |
ohair@286 | 202 | return null; |
ohair@286 | 203 | } |
ohair@286 | 204 | |
ohair@286 | 205 | /** |
ohair@286 | 206 | * Load all of the named resource. |
ohair@286 | 207 | */ |
ohair@286 | 208 | private void loadAllResources(List v, String name) { |
ohair@286 | 209 | boolean anyLoaded = false; |
ohair@286 | 210 | try { |
ohair@286 | 211 | URL[] urls; |
ohair@286 | 212 | ClassLoader cld = null; |
ohair@286 | 213 | // First try the "application's" class loader. |
ohair@286 | 214 | cld = SecuritySupport.getContextClassLoader(); |
ohair@286 | 215 | if (cld == null) |
ohair@286 | 216 | cld = this.getClass().getClassLoader(); |
ohair@286 | 217 | if (cld != null) |
ohair@286 | 218 | urls = SecuritySupport.getResources(cld, name); |
ohair@286 | 219 | else |
ohair@286 | 220 | urls = SecuritySupport.getSystemResources(name); |
ohair@286 | 221 | if (urls != null) { |
ohair@286 | 222 | if (LogSupport.isLoggable()) |
ohair@286 | 223 | LogSupport.log("MailcapCommandMap: getResources"); |
ohair@286 | 224 | for (int i = 0; i < urls.length; i++) { |
ohair@286 | 225 | URL url = urls[i]; |
ohair@286 | 226 | InputStream clis = null; |
ohair@286 | 227 | if (LogSupport.isLoggable()) |
ohair@286 | 228 | LogSupport.log("MailcapCommandMap: URL " + url); |
ohair@286 | 229 | try { |
ohair@286 | 230 | clis = SecuritySupport.openStream(url); |
ohair@286 | 231 | if (clis != null) { |
ohair@286 | 232 | v.add(new MailcapFile(clis)); |
ohair@286 | 233 | anyLoaded = true; |
ohair@286 | 234 | if (LogSupport.isLoggable()) |
ohair@286 | 235 | LogSupport.log("MailcapCommandMap: " + |
ohair@286 | 236 | "successfully loaded " + |
ohair@286 | 237 | "mailcap file from URL: " + |
ohair@286 | 238 | url); |
ohair@286 | 239 | } else { |
ohair@286 | 240 | if (LogSupport.isLoggable()) |
ohair@286 | 241 | LogSupport.log("MailcapCommandMap: " + |
ohair@286 | 242 | "not loading mailcap " + |
ohair@286 | 243 | "file from URL: " + url); |
ohair@286 | 244 | } |
ohair@286 | 245 | } catch (IOException ioex) { |
ohair@286 | 246 | if (LogSupport.isLoggable()) |
ohair@286 | 247 | LogSupport.log("MailcapCommandMap: can't load " + |
ohair@286 | 248 | url, ioex); |
ohair@286 | 249 | } catch (SecurityException sex) { |
ohair@286 | 250 | if (LogSupport.isLoggable()) |
ohair@286 | 251 | LogSupport.log("MailcapCommandMap: can't load " + |
ohair@286 | 252 | url, sex); |
ohair@286 | 253 | } finally { |
ohair@286 | 254 | try { |
ohair@286 | 255 | if (clis != null) |
ohair@286 | 256 | clis.close(); |
ohair@286 | 257 | } catch (IOException cex) { } |
ohair@286 | 258 | } |
ohair@286 | 259 | } |
ohair@286 | 260 | } |
ohair@286 | 261 | } catch (Exception ex) { |
ohair@286 | 262 | if (LogSupport.isLoggable()) |
ohair@286 | 263 | LogSupport.log("MailcapCommandMap: can't load " + name, ex); |
ohair@286 | 264 | } |
ohair@286 | 265 | |
ohair@286 | 266 | // if failed to load anything, fall back to old technique, just in case |
ohair@286 | 267 | if (!anyLoaded) { |
ohair@286 | 268 | if (LogSupport.isLoggable()) |
ohair@286 | 269 | LogSupport.log("MailcapCommandMap: !anyLoaded"); |
ohair@286 | 270 | MailcapFile mf = loadResource("/" + name); |
ohair@286 | 271 | if (mf != null) |
ohair@286 | 272 | v.add(mf); |
ohair@286 | 273 | } |
ohair@286 | 274 | } |
ohair@286 | 275 | |
ohair@286 | 276 | /** |
ohair@286 | 277 | * Load from the named file. |
ohair@286 | 278 | */ |
ohair@286 | 279 | private MailcapFile loadFile(String name) { |
ohair@286 | 280 | MailcapFile mtf = null; |
ohair@286 | 281 | |
ohair@286 | 282 | try { |
ohair@286 | 283 | mtf = new MailcapFile(name); |
ohair@286 | 284 | } catch (IOException e) { |
ohair@286 | 285 | // e.printStackTrace(); |
ohair@286 | 286 | } |
ohair@286 | 287 | return mtf; |
ohair@286 | 288 | } |
ohair@286 | 289 | |
ohair@286 | 290 | /** |
ohair@286 | 291 | * Constructor that allows the caller to specify the path |
ohair@286 | 292 | * of a <i>mailcap</i> file. |
ohair@286 | 293 | * |
ohair@286 | 294 | * @param fileName The name of the <i>mailcap</i> file to open |
ohair@286 | 295 | * @exception IOException if the file can't be accessed |
ohair@286 | 296 | */ |
ohair@286 | 297 | public MailcapCommandMap(String fileName) throws IOException { |
ohair@286 | 298 | this(); |
ohair@286 | 299 | |
ohair@286 | 300 | if (LogSupport.isLoggable()) |
ohair@286 | 301 | LogSupport.log("MailcapCommandMap: load PROG from " + fileName); |
ohair@286 | 302 | if (DB[PROG] == null) { |
ohair@286 | 303 | DB[PROG] = new MailcapFile(fileName); |
ohair@286 | 304 | } |
ohair@286 | 305 | } |
ohair@286 | 306 | |
ohair@286 | 307 | |
ohair@286 | 308 | /** |
ohair@286 | 309 | * Constructor that allows the caller to specify an <i>InputStream</i> |
ohair@286 | 310 | * containing a mailcap file. |
ohair@286 | 311 | * |
ohair@286 | 312 | * @param is InputStream of the <i>mailcap</i> file to open |
ohair@286 | 313 | */ |
ohair@286 | 314 | public MailcapCommandMap(InputStream is) { |
ohair@286 | 315 | this(); |
ohair@286 | 316 | |
ohair@286 | 317 | LogSupport.log("MailcapCommandMap: load PROG"); |
ohair@286 | 318 | if (DB[PROG] == null) { |
ohair@286 | 319 | try { |
ohair@286 | 320 | DB[PROG] = new MailcapFile(is); |
ohair@286 | 321 | } catch (IOException ex) { |
ohair@286 | 322 | // XXX - should throw it |
ohair@286 | 323 | } |
ohair@286 | 324 | } |
ohair@286 | 325 | } |
ohair@286 | 326 | |
ohair@286 | 327 | /** |
ohair@286 | 328 | * Get the preferred command list for a MIME Type. The MailcapCommandMap |
ohair@286 | 329 | * searches the mailcap files as described above under |
ohair@286 | 330 | * <i>Mailcap file search order</i>.<p> |
ohair@286 | 331 | * |
ohair@286 | 332 | * The result of the search is a proper subset of available |
ohair@286 | 333 | * commands in all mailcap files known to this instance of |
ohair@286 | 334 | * MailcapCommandMap. The first entry for a particular command |
ohair@286 | 335 | * is considered the preferred command. |
ohair@286 | 336 | * |
ohair@286 | 337 | * @param mimeType the MIME type |
ohair@286 | 338 | * @return the CommandInfo objects representing the preferred commands. |
ohair@286 | 339 | */ |
ohair@286 | 340 | public synchronized CommandInfo[] getPreferredCommands(String mimeType) { |
ohair@286 | 341 | List cmdList = new ArrayList(); |
ohair@286 | 342 | if (mimeType != null) |
ohair@286 | 343 | mimeType = mimeType.toLowerCase(Locale.ENGLISH); |
ohair@286 | 344 | |
ohair@286 | 345 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 346 | if (DB[i] == null) |
ohair@286 | 347 | continue; |
ohair@286 | 348 | Map cmdMap = DB[i].getMailcapList(mimeType); |
ohair@286 | 349 | if (cmdMap != null) |
ohair@286 | 350 | appendPrefCmdsToList(cmdMap, cmdList); |
ohair@286 | 351 | } |
ohair@286 | 352 | |
ohair@286 | 353 | // now add the fallback commands |
ohair@286 | 354 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 355 | if (DB[i] == null) |
ohair@286 | 356 | continue; |
ohair@286 | 357 | Map cmdMap = DB[i].getMailcapFallbackList(mimeType); |
ohair@286 | 358 | if (cmdMap != null) |
ohair@286 | 359 | appendPrefCmdsToList(cmdMap, cmdList); |
ohair@286 | 360 | } |
ohair@286 | 361 | |
ohair@286 | 362 | CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()]; |
ohair@286 | 363 | cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos); |
ohair@286 | 364 | |
ohair@286 | 365 | return cmdInfos; |
ohair@286 | 366 | } |
ohair@286 | 367 | |
ohair@286 | 368 | /** |
ohair@286 | 369 | * Put the commands that are in the hash table, into the list. |
ohair@286 | 370 | */ |
ohair@286 | 371 | private void appendPrefCmdsToList(Map cmdHash, List cmdList) { |
ohair@286 | 372 | Iterator verb_enum = cmdHash.keySet().iterator(); |
ohair@286 | 373 | |
ohair@286 | 374 | while (verb_enum.hasNext()) { |
ohair@286 | 375 | String verb = (String)verb_enum.next(); |
ohair@286 | 376 | if (!checkForVerb(cmdList, verb)) { |
ohair@286 | 377 | List cmdList2 = (List)cmdHash.get(verb); // get the list |
ohair@286 | 378 | String className = (String)cmdList2.get(0); |
ohair@286 | 379 | cmdList.add(new CommandInfo(verb, className)); |
ohair@286 | 380 | } |
ohair@286 | 381 | } |
ohair@286 | 382 | } |
ohair@286 | 383 | |
ohair@286 | 384 | /** |
ohair@286 | 385 | * Check the cmdList to see if this command exists, return |
ohair@286 | 386 | * true if the verb is there. |
ohair@286 | 387 | */ |
ohair@286 | 388 | private boolean checkForVerb(List cmdList, String verb) { |
ohair@286 | 389 | Iterator ee = cmdList.iterator(); |
ohair@286 | 390 | while (ee.hasNext()) { |
ohair@286 | 391 | String enum_verb = |
ohair@286 | 392 | (String)((CommandInfo)ee.next()).getCommandName(); |
ohair@286 | 393 | if (enum_verb.equals(verb)) |
ohair@286 | 394 | return true; |
ohair@286 | 395 | } |
ohair@286 | 396 | return false; |
ohair@286 | 397 | } |
ohair@286 | 398 | |
ohair@286 | 399 | /** |
ohair@286 | 400 | * Get all the available commands in all mailcap files known to |
ohair@286 | 401 | * this instance of MailcapCommandMap for this MIME type. |
ohair@286 | 402 | * |
ohair@286 | 403 | * @param mimeType the MIME type |
ohair@286 | 404 | * @return the CommandInfo objects representing all the commands. |
ohair@286 | 405 | */ |
ohair@286 | 406 | public synchronized CommandInfo[] getAllCommands(String mimeType) { |
ohair@286 | 407 | List cmdList = new ArrayList(); |
ohair@286 | 408 | if (mimeType != null) |
ohair@286 | 409 | mimeType = mimeType.toLowerCase(Locale.ENGLISH); |
ohair@286 | 410 | |
ohair@286 | 411 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 412 | if (DB[i] == null) |
ohair@286 | 413 | continue; |
ohair@286 | 414 | Map cmdMap = DB[i].getMailcapList(mimeType); |
ohair@286 | 415 | if (cmdMap != null) |
ohair@286 | 416 | appendCmdsToList(cmdMap, cmdList); |
ohair@286 | 417 | } |
ohair@286 | 418 | |
ohair@286 | 419 | // now add the fallback commands |
ohair@286 | 420 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 421 | if (DB[i] == null) |
ohair@286 | 422 | continue; |
ohair@286 | 423 | Map cmdMap = DB[i].getMailcapFallbackList(mimeType); |
ohair@286 | 424 | if (cmdMap != null) |
ohair@286 | 425 | appendCmdsToList(cmdMap, cmdList); |
ohair@286 | 426 | } |
ohair@286 | 427 | |
ohair@286 | 428 | CommandInfo[] cmdInfos = new CommandInfo[cmdList.size()]; |
ohair@286 | 429 | cmdInfos = (CommandInfo[])cmdList.toArray(cmdInfos); |
ohair@286 | 430 | |
ohair@286 | 431 | return cmdInfos; |
ohair@286 | 432 | } |
ohair@286 | 433 | |
ohair@286 | 434 | /** |
ohair@286 | 435 | * Put the commands that are in the hash table, into the list. |
ohair@286 | 436 | */ |
ohair@286 | 437 | private void appendCmdsToList(Map typeHash, List cmdList) { |
ohair@286 | 438 | Iterator verb_enum = typeHash.keySet().iterator(); |
ohair@286 | 439 | |
ohair@286 | 440 | while (verb_enum.hasNext()) { |
ohair@286 | 441 | String verb = (String)verb_enum.next(); |
ohair@286 | 442 | List cmdList2 = (List)typeHash.get(verb); |
ohair@286 | 443 | Iterator cmd_enum = ((List)cmdList2).iterator(); |
ohair@286 | 444 | |
ohair@286 | 445 | while (cmd_enum.hasNext()) { |
ohair@286 | 446 | String cmd = (String)cmd_enum.next(); |
ohair@286 | 447 | cmdList.add(new CommandInfo(verb, cmd)); |
ohair@286 | 448 | // cmdList.add(0, new CommandInfo(verb, cmd)); |
ohair@286 | 449 | } |
ohair@286 | 450 | } |
ohair@286 | 451 | } |
ohair@286 | 452 | |
ohair@286 | 453 | /** |
ohair@286 | 454 | * Get the command corresponding to <code>cmdName</code> for the MIME type. |
ohair@286 | 455 | * |
ohair@286 | 456 | * @param mimeType the MIME type |
ohair@286 | 457 | * @param cmdName the command name |
ohair@286 | 458 | * @return the CommandInfo object corresponding to the command. |
ohair@286 | 459 | */ |
ohair@286 | 460 | public synchronized CommandInfo getCommand(String mimeType, |
ohair@286 | 461 | String cmdName) { |
ohair@286 | 462 | if (mimeType != null) |
ohair@286 | 463 | mimeType = mimeType.toLowerCase(Locale.ENGLISH); |
ohair@286 | 464 | |
ohair@286 | 465 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 466 | if (DB[i] == null) |
ohair@286 | 467 | continue; |
ohair@286 | 468 | Map cmdMap = DB[i].getMailcapList(mimeType); |
ohair@286 | 469 | if (cmdMap != null) { |
ohair@286 | 470 | // get the cmd list for the cmd |
ohair@286 | 471 | List v = (List)cmdMap.get(cmdName); |
ohair@286 | 472 | if (v != null) { |
ohair@286 | 473 | String cmdClassName = (String)v.get(0); |
ohair@286 | 474 | |
ohair@286 | 475 | if (cmdClassName != null) |
ohair@286 | 476 | return new CommandInfo(cmdName, cmdClassName); |
ohair@286 | 477 | } |
ohair@286 | 478 | } |
ohair@286 | 479 | } |
ohair@286 | 480 | |
ohair@286 | 481 | // now try the fallback list |
ohair@286 | 482 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 483 | if (DB[i] == null) |
ohair@286 | 484 | continue; |
ohair@286 | 485 | Map cmdMap = DB[i].getMailcapFallbackList(mimeType); |
ohair@286 | 486 | if (cmdMap != null) { |
ohair@286 | 487 | // get the cmd list for the cmd |
ohair@286 | 488 | List v = (List)cmdMap.get(cmdName); |
ohair@286 | 489 | if (v != null) { |
ohair@286 | 490 | String cmdClassName = (String)v.get(0); |
ohair@286 | 491 | |
ohair@286 | 492 | if (cmdClassName != null) |
ohair@286 | 493 | return new CommandInfo(cmdName, cmdClassName); |
ohair@286 | 494 | } |
ohair@286 | 495 | } |
ohair@286 | 496 | } |
ohair@286 | 497 | return null; |
ohair@286 | 498 | } |
ohair@286 | 499 | |
ohair@286 | 500 | /** |
ohair@286 | 501 | * Add entries to the registry. Programmatically |
ohair@286 | 502 | * added entries are searched before other entries.<p> |
ohair@286 | 503 | * |
ohair@286 | 504 | * The string that is passed in should be in mailcap |
ohair@286 | 505 | * format. |
ohair@286 | 506 | * |
ohair@286 | 507 | * @param mail_cap a correctly formatted mailcap string |
ohair@286 | 508 | */ |
ohair@286 | 509 | public synchronized void addMailcap(String mail_cap) { |
ohair@286 | 510 | // check to see if one exists |
ohair@286 | 511 | LogSupport.log("MailcapCommandMap: add to PROG"); |
ohair@286 | 512 | if (DB[PROG] == null) |
ohair@286 | 513 | DB[PROG] = new MailcapFile(); |
ohair@286 | 514 | |
ohair@286 | 515 | DB[PROG].appendToMailcap(mail_cap); |
ohair@286 | 516 | } |
ohair@286 | 517 | |
ohair@286 | 518 | /** |
ohair@286 | 519 | * Return the DataContentHandler for the specified MIME type. |
ohair@286 | 520 | * |
ohair@286 | 521 | * @param mimeType the MIME type |
ohair@286 | 522 | * @return the DataContentHandler |
ohair@286 | 523 | */ |
ohair@286 | 524 | public synchronized DataContentHandler createDataContentHandler( |
ohair@286 | 525 | String mimeType) { |
ohair@286 | 526 | if (LogSupport.isLoggable()) |
ohair@286 | 527 | LogSupport.log( |
ohair@286 | 528 | "MailcapCommandMap: createDataContentHandler for " + mimeType); |
ohair@286 | 529 | if (mimeType != null) |
ohair@286 | 530 | mimeType = mimeType.toLowerCase(Locale.ENGLISH); |
ohair@286 | 531 | |
ohair@286 | 532 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 533 | if (DB[i] == null) |
ohair@286 | 534 | continue; |
ohair@286 | 535 | if (LogSupport.isLoggable()) |
ohair@286 | 536 | LogSupport.log(" search DB #" + i); |
ohair@286 | 537 | Map cmdMap = DB[i].getMailcapList(mimeType); |
ohair@286 | 538 | if (cmdMap != null) { |
ohair@286 | 539 | List v = (List)cmdMap.get("content-handler"); |
ohair@286 | 540 | if (v != null) { |
ohair@286 | 541 | String name = (String)v.get(0); |
ohair@286 | 542 | DataContentHandler dch = getDataContentHandler(name); |
ohair@286 | 543 | if (dch != null) |
ohair@286 | 544 | return dch; |
ohair@286 | 545 | } |
ohair@286 | 546 | } |
ohair@286 | 547 | } |
ohair@286 | 548 | |
ohair@286 | 549 | // now try the fallback entries |
ohair@286 | 550 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 551 | if (DB[i] == null) |
ohair@286 | 552 | continue; |
ohair@286 | 553 | if (LogSupport.isLoggable()) |
ohair@286 | 554 | LogSupport.log(" search fallback DB #" + i); |
ohair@286 | 555 | Map cmdMap = DB[i].getMailcapFallbackList(mimeType); |
ohair@286 | 556 | if (cmdMap != null) { |
ohair@286 | 557 | List v = (List)cmdMap.get("content-handler"); |
ohair@286 | 558 | if (v != null) { |
ohair@286 | 559 | String name = (String)v.get(0); |
ohair@286 | 560 | DataContentHandler dch = getDataContentHandler(name); |
ohair@286 | 561 | if (dch != null) |
ohair@286 | 562 | return dch; |
ohair@286 | 563 | } |
ohair@286 | 564 | } |
ohair@286 | 565 | } |
ohair@286 | 566 | return null; |
ohair@286 | 567 | } |
ohair@286 | 568 | |
ohair@286 | 569 | private DataContentHandler getDataContentHandler(String name) { |
ohair@286 | 570 | if (LogSupport.isLoggable()) |
ohair@286 | 571 | LogSupport.log(" got content-handler"); |
ohair@286 | 572 | if (LogSupport.isLoggable()) |
ohair@286 | 573 | LogSupport.log(" class " + name); |
ohair@286 | 574 | try { |
ohair@286 | 575 | ClassLoader cld = null; |
ohair@286 | 576 | // First try the "application's" class loader. |
ohair@286 | 577 | cld = SecuritySupport.getContextClassLoader(); |
ohair@286 | 578 | if (cld == null) |
ohair@286 | 579 | cld = this.getClass().getClassLoader(); |
ohair@286 | 580 | Class cl = null; |
ohair@286 | 581 | try { |
ohair@286 | 582 | cl = cld.loadClass(name); |
ohair@286 | 583 | } catch (Exception ex) { |
ohair@286 | 584 | // if anything goes wrong, do it the old way |
ohair@286 | 585 | cl = Class.forName(name); |
ohair@286 | 586 | } |
ohair@286 | 587 | if (cl != null) // XXX - always true? |
ohair@286 | 588 | return (DataContentHandler)cl.newInstance(); |
ohair@286 | 589 | } catch (IllegalAccessException e) { |
ohair@286 | 590 | if (LogSupport.isLoggable()) |
ohair@286 | 591 | LogSupport.log("Can't load DCH " + name, e); |
ohair@286 | 592 | } catch (ClassNotFoundException e) { |
ohair@286 | 593 | if (LogSupport.isLoggable()) |
ohair@286 | 594 | LogSupport.log("Can't load DCH " + name, e); |
ohair@286 | 595 | } catch (InstantiationException e) { |
ohair@286 | 596 | if (LogSupport.isLoggable()) |
ohair@286 | 597 | LogSupport.log("Can't load DCH " + name, e); |
ohair@286 | 598 | } |
ohair@286 | 599 | return null; |
ohair@286 | 600 | } |
ohair@286 | 601 | |
ohair@286 | 602 | /** |
ohair@286 | 603 | * Get all the MIME types known to this command map. |
ohair@286 | 604 | * |
ohair@286 | 605 | * @return array of MIME types as strings |
ohair@286 | 606 | * @since JAF 1.1 |
ohair@286 | 607 | */ |
ohair@286 | 608 | public synchronized String[] getMimeTypes() { |
ohair@286 | 609 | List mtList = new ArrayList(); |
ohair@286 | 610 | |
ohair@286 | 611 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 612 | if (DB[i] == null) |
ohair@286 | 613 | continue; |
ohair@286 | 614 | String[] ts = DB[i].getMimeTypes(); |
ohair@286 | 615 | if (ts != null) { |
ohair@286 | 616 | for (int j = 0; j < ts.length; j++) { |
ohair@286 | 617 | // eliminate duplicates |
ohair@286 | 618 | if (!mtList.contains(ts[j])) |
ohair@286 | 619 | mtList.add(ts[j]); |
ohair@286 | 620 | } |
ohair@286 | 621 | } |
ohair@286 | 622 | } |
ohair@286 | 623 | |
ohair@286 | 624 | String[] mts = new String[mtList.size()]; |
ohair@286 | 625 | mts = (String[])mtList.toArray(mts); |
ohair@286 | 626 | |
ohair@286 | 627 | return mts; |
ohair@286 | 628 | } |
ohair@286 | 629 | |
ohair@286 | 630 | /** |
ohair@286 | 631 | * Get the native commands for the given MIME type. |
ohair@286 | 632 | * Returns an array of strings where each string is |
ohair@286 | 633 | * an entire mailcap file entry. The application |
ohair@286 | 634 | * will need to parse the entry to extract the actual |
ohair@286 | 635 | * command as well as any attributes it needs. See |
ohair@286 | 636 | * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A> |
ohair@286 | 637 | * for details of the mailcap entry syntax. Only mailcap |
ohair@286 | 638 | * entries that specify a view command for the specified |
ohair@286 | 639 | * MIME type are returned. |
ohair@286 | 640 | * |
ohair@286 | 641 | * @return array of native command entries |
ohair@286 | 642 | * @since JAF 1.1 |
ohair@286 | 643 | */ |
ohair@286 | 644 | public synchronized String[] getNativeCommands(String mimeType) { |
ohair@286 | 645 | List cmdList = new ArrayList(); |
ohair@286 | 646 | if (mimeType != null) |
ohair@286 | 647 | mimeType = mimeType.toLowerCase(Locale.ENGLISH); |
ohair@286 | 648 | |
ohair@286 | 649 | for (int i = 0; i < DB.length; i++) { |
ohair@286 | 650 | if (DB[i] == null) |
ohair@286 | 651 | continue; |
ohair@286 | 652 | String[] cmds = DB[i].getNativeCommands(mimeType); |
ohair@286 | 653 | if (cmds != null) { |
ohair@286 | 654 | for (int j = 0; j < cmds.length; j++) { |
ohair@286 | 655 | // eliminate duplicates |
ohair@286 | 656 | if (!cmdList.contains(cmds[j])) |
ohair@286 | 657 | cmdList.add(cmds[j]); |
ohair@286 | 658 | } |
ohair@286 | 659 | } |
ohair@286 | 660 | } |
ohair@286 | 661 | |
ohair@286 | 662 | String[] cmds = new String[cmdList.size()]; |
ohair@286 | 663 | cmds = (String[])cmdList.toArray(cmds); |
ohair@286 | 664 | |
ohair@286 | 665 | return cmds; |
ohair@286 | 666 | } |
ohair@286 | 667 | |
ohair@286 | 668 | /** |
ohair@286 | 669 | * for debugging... |
ohair@286 | 670 | * |
ohair@286 | 671 | public static void main(String[] argv) throws Exception { |
ohair@286 | 672 | MailcapCommandMap map = new MailcapCommandMap(); |
ohair@286 | 673 | CommandInfo[] cmdInfo; |
ohair@286 | 674 | |
ohair@286 | 675 | cmdInfo = map.getPreferredCommands(argv[0]); |
ohair@286 | 676 | System.out.println("Preferred Commands:"); |
ohair@286 | 677 | for (int i = 0; i < cmdInfo.length; i++) |
ohair@286 | 678 | System.out.println("Command " + cmdInfo[i].getCommandName() + " [" + |
ohair@286 | 679 | cmdInfo[i].getCommandClass() + "]"); |
ohair@286 | 680 | cmdInfo = map.getAllCommands(argv[0]); |
ohair@286 | 681 | System.out.println(); |
ohair@286 | 682 | System.out.println("All Commands:"); |
ohair@286 | 683 | for (int i = 0; i < cmdInfo.length; i++) |
ohair@286 | 684 | System.out.println("Command " + cmdInfo[i].getCommandName() + " [" + |
ohair@286 | 685 | cmdInfo[i].getCommandClass() + "]"); |
ohair@286 | 686 | DataContentHandler dch = map.createDataContentHandler(argv[0]); |
ohair@286 | 687 | if (dch != null) |
ohair@286 | 688 | System.out.println("DataContentHandler " + |
ohair@286 | 689 | dch.getClass().toString()); |
ohair@286 | 690 | System.exit(0); |
ohair@286 | 691 | } |
ohair@286 | 692 | */ |
ohair@286 | 693 | } |