1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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
27 package javax.activation;
28
29 import java.util.*;
30 import java.io.*;
31 import java.net.*;
32 import com.sun.activation.registries.MailcapFile;
33 import com.sun.activation.registries.LogSupport;
34
35 /**
36 * MailcapCommandMap extends the CommandMap
37 * abstract class. It implements a CommandMap whose configuration
38 * is based on mailcap files
39 * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
40 * The MailcapCommandMap can be configured both programmatically
41 * and via configuration files.
42 * <p>
43 * <b>Mailcap file search order:</b><p>
44 * The MailcapCommandMap looks in various places in the user's
45 * system for mailcap file entries. When requests are made
46 * to search for commands in the MailcapCommandMap, it searches
47 * mailcap files in the following order:
48 * <ol>
49 * <li> Programatically added entries to the MailcapCommandMap instance.
50 * <li> The file {@code .mailcap} in the user's home directory.
51 * <li> The file {@literal <}<i>java.home</i>{@literal >}{@code /lib/mailcap}.
52 * <li> The file or resources named {@code META-INF/mailcap}.
53 * <li> The file or resource named {@code META-INF/mailcap.default}
54 * (usually found only in the {@code activation.jar} file).
55 * </ol>
56 * <p>
57 * <b>Mailcap file format:</b><p>
58 *
59 * Mailcap files must conform to the mailcap
60 * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
61 * For Multimedia Mail Format Information</i>).
62 * The file format consists of entries corresponding to
63 * particular MIME types. In general, the specification
64 * specifies <i>applications</i> for clients to use when they
65 * themselves cannot operate on the specified MIME type. The
66 * MailcapCommandMap extends this specification by using a parameter mechanism
67 * in mailcap files that allows JavaBeans(tm) components to be specified as
68 * corresponding to particular commands for a MIME type.<p>
69 *
70 * When a mailcap file is
71 * parsed, the MailcapCommandMap recognizes certain parameter signatures,
72 * specifically those parameter names that begin with {@code x-java-}.
73 * The MailcapCommandMap uses this signature to find
74 * command entries for inclusion into its registries.
75 * Parameter names with the form {@code x-java-<name>}
76 * are read by the MailcapCommandMap as identifying a command
103 * # and a parameter list looks like:
104 * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
105 * # Note that mailcap entries that do not contain 'x-java' parameters
106 * # and comply to RFC 1524 are simply ignored:
107 * image/gif; /usr/dt/bin/sdtimage %s
108 * }</pre>
109 *
110 * @author Bart Calder
111 * @author Bill Shannon
112 *
113 * @since 1.6
114 */
115
116 public class MailcapCommandMap extends CommandMap {
117 /*
118 * We manage a collection of databases, searched in order.
119 */
120 private MailcapFile[] DB;
121 private static final int PROG = 0; // programmatically added entries
122
123 /**
124 * The default Constructor.
125 */
126 public MailcapCommandMap() {
127 super();
128 List dbv = new ArrayList(5); // usually 5 or less databases
129 MailcapFile mf = null;
130 dbv.add(null); // place holder for PROG entry
131
132 LogSupport.log("MailcapCommandMap: load HOME");
133 try {
134 String user_home = System.getProperty("user.home");
135
136 if (user_home != null) {
137 String path = user_home + File.separator + ".mailcap";
138 mf = loadFile(path);
139 if (mf != null)
140 dbv.add(mf);
141 }
142 } catch (SecurityException ex) {}
143
144 LogSupport.log("MailcapCommandMap: load SYS");
145 try {
146 // check system's home
147 String system_mailcap = System.getProperty("java.home") +
148 File.separator + "lib" + File.separator + "mailcap";
149 mf = loadFile(system_mailcap);
150 if (mf != null)
151 dbv.add(mf);
152 } catch (SecurityException ex) {}
153
154 LogSupport.log("MailcapCommandMap: load JAR");
155 // load from the app's jar file
156 loadAllResources(dbv, "META-INF/mailcap");
157
158 LogSupport.log("MailcapCommandMap: load DEF");
159 mf = loadResource("/META-INF/mailcap.default");
160
161 if (mf != null)
162 dbv.add(mf);
163
164 DB = new MailcapFile[dbv.size()];
165 DB = (MailcapFile[])dbv.toArray(DB);
166 }
167
168 /**
169 * Load from the named resource.
170 */
171 private MailcapFile loadResource(String name) {
616 }
617 }
618
619 String[] mts = new String[mtList.size()];
620 mts = (String[])mtList.toArray(mts);
621
622 return mts;
623 }
624
625 /**
626 * Get the native commands for the given MIME type.
627 * Returns an array of strings where each string is
628 * an entire mailcap file entry. The application
629 * will need to parse the entry to extract the actual
630 * command as well as any attributes it needs. See
631 * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>
632 * for details of the mailcap entry syntax. Only mailcap
633 * entries that specify a view command for the specified
634 * MIME type are returned.
635 *
636 * @return array of native command entries
637 * @since 1.6, JAF 1.1
638 */
639 public synchronized String[] getNativeCommands(String mimeType) {
640 List cmdList = new ArrayList();
641 if (mimeType != null)
642 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
643
644 for (int i = 0; i < DB.length; i++) {
645 if (DB[i] == null)
646 continue;
647 String[] cmds = DB[i].getNativeCommands(mimeType);
648 if (cmds != null) {
649 for (int j = 0; j < cmds.length; j++) {
650 // eliminate duplicates
651 if (!cmdList.contains(cmds[j]))
652 cmdList.add(cmds[j]);
653 }
654 }
655 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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
27 package javax.activation;
28
29 import java.util.*;
30 import java.io.*;
31 import java.net.*;
32 import java.security.AccessController;
33 import java.security.PrivilegedAction;
34 import com.sun.activation.registries.MailcapFile;
35 import com.sun.activation.registries.LogSupport;
36
37 /**
38 * MailcapCommandMap extends the CommandMap
39 * abstract class. It implements a CommandMap whose configuration
40 * is based on mailcap files
41 * (<A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>).
42 * The MailcapCommandMap can be configured both programmatically
43 * and via configuration files.
44 * <p>
45 * <b>Mailcap file search order:</b><p>
46 * The MailcapCommandMap looks in various places in the user's
47 * system for mailcap file entries. When requests are made
48 * to search for commands in the MailcapCommandMap, it searches
49 * mailcap files in the following order:
50 * <ol>
51 * <li> Programatically added entries to the MailcapCommandMap instance.
52 * <li> The file {@code .mailcap} in the user's home directory.
53 * <li> The file {@code mailcap} in the Java runtime.
54 * <li> The file or resources named {@code META-INF/mailcap}.
55 * <li> The file or resource named {@code META-INF/mailcap.default}
56 * (usually found only in the {@code activation.jar} file).
57 * </ol>
58 * <p>
59 * The current implementation looks for the {@code mailcap} file
60 * in the Java runtime in the directory <i>java.home</i>{@code /conf}
61 * if it exists, and otherwise in the directory
62 * <i>java.home</i>{@code /lib}, where <i>java.home</i> is the value
63 * of the "java.home" System property. Note that the "conf" directory was
64 * introduced in JDK 9.
65 * <p>
66 * <b>Mailcap file format:</b><p>
67 *
68 * Mailcap files must conform to the mailcap
69 * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
70 * For Multimedia Mail Format Information</i>).
71 * The file format consists of entries corresponding to
72 * particular MIME types. In general, the specification
73 * specifies <i>applications</i> for clients to use when they
74 * themselves cannot operate on the specified MIME type. The
75 * MailcapCommandMap extends this specification by using a parameter mechanism
76 * in mailcap files that allows JavaBeans(tm) components to be specified as
77 * corresponding to particular commands for a MIME type.<p>
78 *
79 * When a mailcap file is
80 * parsed, the MailcapCommandMap recognizes certain parameter signatures,
81 * specifically those parameter names that begin with {@code x-java-}.
82 * The MailcapCommandMap uses this signature to find
83 * command entries for inclusion into its registries.
84 * Parameter names with the form {@code x-java-<name>}
85 * are read by the MailcapCommandMap as identifying a command
112 * # and a parameter list looks like:
113 * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
114 * # Note that mailcap entries that do not contain 'x-java' parameters
115 * # and comply to RFC 1524 are simply ignored:
116 * image/gif; /usr/dt/bin/sdtimage %s
117 * }</pre>
118 *
119 * @author Bart Calder
120 * @author Bill Shannon
121 *
122 * @since 1.6
123 */
124
125 public class MailcapCommandMap extends CommandMap {
126 /*
127 * We manage a collection of databases, searched in order.
128 */
129 private MailcapFile[] DB;
130 private static final int PROG = 0; // programmatically added entries
131
132 private static final String confDir;
133
134 static {
135 String dir = null;
136 try {
137 dir = (String)AccessController.doPrivileged(
138 new PrivilegedAction() {
139 public Object run() {
140 String home = System.getProperty("java.home");
141 String newdir = home + File.separator + "conf";
142 File conf = new File(newdir);
143 if (conf.exists())
144 return newdir + File.separator;
145 else
146 return home + File.separator + "lib" + File.separator;
147 }
148 });
149 } catch (Exception ex) {
150 // ignore any exceptions
151 }
152 confDir = dir;
153 }
154
155 /**
156 * The default Constructor.
157 */
158 public MailcapCommandMap() {
159 super();
160 List dbv = new ArrayList(5); // usually 5 or less databases
161 MailcapFile mf = null;
162 dbv.add(null); // place holder for PROG entry
163
164 LogSupport.log("MailcapCommandMap: load HOME");
165 try {
166 String user_home = System.getProperty("user.home");
167
168 if (user_home != null) {
169 String path = user_home + File.separator + ".mailcap";
170 mf = loadFile(path);
171 if (mf != null)
172 dbv.add(mf);
173 }
174 } catch (SecurityException ex) {}
175
176 LogSupport.log("MailcapCommandMap: load SYS");
177 try {
178 // check system's home
179 if (confDir != null) {
180 mf = loadFile(confDir + "mailcap");
181 if (mf != null)
182 dbv.add(mf);
183 }
184 } catch (SecurityException ex) {}
185
186 LogSupport.log("MailcapCommandMap: load JAR");
187 // load from the app's jar file
188 loadAllResources(dbv, "META-INF/mailcap");
189
190 LogSupport.log("MailcapCommandMap: load DEF");
191 mf = loadResource("/META-INF/mailcap.default");
192
193 if (mf != null)
194 dbv.add(mf);
195
196 DB = new MailcapFile[dbv.size()];
197 DB = (MailcapFile[])dbv.toArray(DB);
198 }
199
200 /**
201 * Load from the named resource.
202 */
203 private MailcapFile loadResource(String name) {
648 }
649 }
650
651 String[] mts = new String[mtList.size()];
652 mts = (String[])mtList.toArray(mts);
653
654 return mts;
655 }
656
657 /**
658 * Get the native commands for the given MIME type.
659 * Returns an array of strings where each string is
660 * an entire mailcap file entry. The application
661 * will need to parse the entry to extract the actual
662 * command as well as any attributes it needs. See
663 * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>
664 * for details of the mailcap entry syntax. Only mailcap
665 * entries that specify a view command for the specified
666 * MIME type are returned.
667 *
668 * @param mimeType the MIME type
669 * @return array of native command entries
670 * @since 1.6, JAF 1.1
671 */
672 public synchronized String[] getNativeCommands(String mimeType) {
673 List cmdList = new ArrayList();
674 if (mimeType != null)
675 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
676
677 for (int i = 0; i < DB.length; i++) {
678 if (DB[i] == null)
679 continue;
680 String[] cmds = DB[i].getNativeCommands(mimeType);
681 if (cmds != null) {
682 for (int j = 0; j < cmds.length; j++) {
683 // eliminate duplicates
684 if (!cmdList.contains(cmds[j]))
685 cmdList.add(cmds[j]);
686 }
687 }
688 }
|