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 <i>java.home</i>{@code /}<i>conf</i>{@code /mailcap}.
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 * (Where <i>java.home</i> is the value of the "java.home" System property
60 * and <i>conf</i> is the directory named "conf" if it exists,
61 * otherwise the directory named "lib"; the "conf" directory was
62 * introduced in JDK 1.9.)
63 * <p>
64 * <b>Mailcap file format:</b><p>
65 *
66 * Mailcap files must conform to the mailcap
67 * file specification (RFC 1524, <i>A User Agent Configuration Mechanism
68 * For Multimedia Mail Format Information</i>).
69 * The file format consists of entries corresponding to
70 * particular MIME types. In general, the specification
71 * specifies <i>applications</i> for clients to use when they
72 * themselves cannot operate on the specified MIME type. The
73 * MailcapCommandMap extends this specification by using a parameter mechanism
74 * in mailcap files that allows JavaBeans(tm) components to be specified as
75 * corresponding to particular commands for a MIME type.<p>
76 *
77 * When a mailcap file is
78 * parsed, the MailcapCommandMap recognizes certain parameter signatures,
79 * specifically those parameter names that begin with {@code x-java-}.
80 * The MailcapCommandMap uses this signature to find
81 * command entries for inclusion into its registries.
82 * Parameter names with the form {@code x-java-<name>}
83 * are read by the MailcapCommandMap as identifying a command
110 * # and a parameter list looks like:
111 * text/plain; ; x-java-view=com.sun.TextViewer; x-java-edit=com.sun.TextEdit
112 * # Note that mailcap entries that do not contain 'x-java' parameters
113 * # and comply to RFC 1524 are simply ignored:
114 * image/gif; /usr/dt/bin/sdtimage %s
115 * }</pre>
116 *
117 * @author Bart Calder
118 * @author Bill Shannon
119 *
120 * @since 1.6
121 */
122
123 public class MailcapCommandMap extends CommandMap {
124 /*
125 * We manage a collection of databases, searched in order.
126 */
127 private MailcapFile[] DB;
128 private static final int PROG = 0; // programmatically added entries
129
130 private static final String confDir;
131
132 static {
133 String dir = null;
134 try {
135 dir = (String)AccessController.doPrivileged(
136 new PrivilegedAction() {
137 public Object run() {
138 String home = System.getProperty("java.home");
139 String newdir = home + File.separator + "conf";
140 File conf = new File(newdir);
141 if (conf.exists())
142 return newdir + File.separator;
143 else
144 return home + File.separator + "lib" + File.separator;
145 }
146 });
147 } catch (Exception ex) {
148 // ignore any exceptions
149 }
150 confDir = dir;
151 }
152
153 /**
154 * The default Constructor.
155 */
156 public MailcapCommandMap() {
157 super();
158 List dbv = new ArrayList(5); // usually 5 or less databases
159 MailcapFile mf = null;
160 dbv.add(null); // place holder for PROG entry
161
162 LogSupport.log("MailcapCommandMap: load HOME");
163 try {
164 String user_home = System.getProperty("user.home");
165
166 if (user_home != null) {
167 String path = user_home + File.separator + ".mailcap";
168 mf = loadFile(path);
169 if (mf != null)
170 dbv.add(mf);
171 }
172 } catch (SecurityException ex) {}
173
174 LogSupport.log("MailcapCommandMap: load SYS");
175 try {
176 // check system's home
177 if (confDir != null) {
178 mf = loadFile(confDir + "mailcap");
179 if (mf != null)
180 dbv.add(mf);
181 }
182 } catch (SecurityException ex) {}
183
184 LogSupport.log("MailcapCommandMap: load JAR");
185 // load from the app's jar file
186 loadAllResources(dbv, "META-INF/mailcap");
187
188 LogSupport.log("MailcapCommandMap: load DEF");
189 mf = loadResource("/META-INF/mailcap.default");
190
191 if (mf != null)
192 dbv.add(mf);
193
194 DB = new MailcapFile[dbv.size()];
195 DB = (MailcapFile[])dbv.toArray(DB);
196 }
197
198 /**
199 * Load from the named resource.
200 */
201 private MailcapFile loadResource(String name) {
646 }
647 }
648
649 String[] mts = new String[mtList.size()];
650 mts = (String[])mtList.toArray(mts);
651
652 return mts;
653 }
654
655 /**
656 * Get the native commands for the given MIME type.
657 * Returns an array of strings where each string is
658 * an entire mailcap file entry. The application
659 * will need to parse the entry to extract the actual
660 * command as well as any attributes it needs. See
661 * <A HREF="http://www.ietf.org/rfc/rfc1524.txt">RFC 1524</A>
662 * for details of the mailcap entry syntax. Only mailcap
663 * entries that specify a view command for the specified
664 * MIME type are returned.
665 *
666 * @param mimeType the MIME type
667 * @return array of native command entries
668 * @since 1.6, JAF 1.1
669 */
670 public synchronized String[] getNativeCommands(String mimeType) {
671 List cmdList = new ArrayList();
672 if (mimeType != null)
673 mimeType = mimeType.toLowerCase(Locale.ENGLISH);
674
675 for (int i = 0; i < DB.length; i++) {
676 if (DB[i] == null)
677 continue;
678 String[] cmds = DB[i].getNativeCommands(mimeType);
679 if (cmds != null) {
680 for (int j = 0; j < cmds.length; j++) {
681 // eliminate duplicates
682 if (!cmdList.contains(cmds[j]))
683 cmdList.add(cmds[j]);
684 }
685 }
686 }
|