001: package abbot.util;
002:
003: import java.net.URLEncoder;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.io.IOException;
007: import abbot.Platform;
008:
009: /** Mail and browser launcher which augments {@link Runtime Runtime.exec}
010: * methods. Provides for built-in email and web browser support.
011: */
012: public class Launcher {
013:
014: // TODO: provide an array of known mailto: handlers
015: // for now we assume the browsers will make an attempt to handle mailto:
016: private static final String[] HTTP = { "firefox", "galeon",
017: "konqueror", "opera", "mozilla", "netscape", "mosaic", };
018:
019: /** Perform trickery to get the right contents into the email handler from
020: a mailto: line.
021: */
022: private static String encodeForMail(String base) {
023: StringBuffer buf = new StringBuffer(base);
024: // Avoid URLEncoder.encode for spaces; it replaces them with plus
025: // signs, which remain pluses when decoded.
026: String SPACE = "--SPACE--";
027: for (int idx = buf.toString().indexOf(" "); idx != -1; idx = buf
028: .toString().indexOf(" ")) {
029: buf.replace(idx, idx + 1, SPACE);
030: }
031: buf = new StringBuffer(URLEncoder.encode(buf.toString()));
032: for (int idx = buf.toString().indexOf(SPACE); idx != -1; idx = buf
033: .toString().indexOf(SPACE)) {
034: if (Platform.isOSX()) {
035: // The "open" command parses spaces
036: buf.replace(idx, idx + SPACE.length(), "%20");
037: } else {
038: buf.replace(idx, idx + SPACE.length(), " ");
039: }
040: }
041: return buf.toString();
042: }
043:
044: /** Format a message to the given user with the given subject and message
045: body. */
046: public static void mail(String user, String subject, String body)
047: throws IOException {
048: mail(user, subject, body, null);
049: }
050:
051: /** Format a message to the given user with the given subject and message
052: body, including a CC list. */
053: public static void mail(String user, String subject, String body,
054: String cc) throws IOException {
055: mail(user, subject, body, cc, null);
056: }
057:
058: /** Format a message to the given user with the given subject and message
059: body, including CC and BCC lists. */
060: public static void mail(String user, String subject, String body,
061: String cc, String bcc) throws IOException {
062: StringBuffer mailto = new StringBuffer("mailto:" + user + "?");
063: if (cc != null)
064: mailto.append("CC=" + cc + "&");
065: if (bcc != null)
066: mailto.append("BCC=" + bcc + "&");
067:
068: mailto.append("Subject=" + encodeForMail(subject) + "&"
069: + "Body=" + encodeForMail(body) + "");
070: open(mailto.toString());
071: }
072:
073: /** Open the given target URL in the platform's browser. */
074: public static void open(String target) throws IOException {
075: open(null, target);
076: }
077:
078: /** Use the given command/program to open the given target. */
079: public static void open(String command, String target)
080: throws IOException {
081: boolean tryBrowsers = false;
082: ArrayList args = new ArrayList();
083: if (command != null) {
084: args.add(command);
085: } else {
086: if (Platform.isOSX()) {
087: args.add("open");
088: } else if (Platform.isWindows()) {
089: if (Platform.isWindows9X()) {
090: args.add("command.com");
091: args.add("/o");
092: } else {
093: args.add("cmd.exe");
094: args.add("/c");
095: args.add("start");
096: args.add("\"Title\"");
097: }
098: // Always quote the argument, just in case
099: // See MS docs for cmd.exe; &, |, and () must be escaped with
100: // ^ or double-quoted. semicolon and comma are command
101: // argument separators, and probably require quoting as well.
102: target = "\"" + target + "\"";
103: } else {
104: args.add("placeholder");
105: tryBrowsers = true;
106: }
107: }
108: args.add(target);
109:
110: String[] cmd = (String[]) args.toArray(new String[args.size()]);
111: if (!tryBrowsers) {
112: ProcessOutputHandler.exec(cmd);
113: } else {
114: // TODO: choose the appropriate application based on the target
115: // URL format, instead of relying on browsers to do it.
116: for (int i = 0; i < HTTP.length; i++) {
117: try {
118: cmd[0] = HTTP[i];
119: ProcessOutputHandler.exec(cmd);
120: return;
121: } catch (IOException e) {
122: // not found, try another one
123: }
124: }
125: throw new IOException("No target handler found (tried "
126: + Arrays.asList(HTTP) + ")");
127: }
128: }
129: }
|