jdk/src/share/classes/sun/net/www/protocol/mailto/MailToURLConnection.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 79 09c7a5100eec
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 79
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.protocol.mailto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.smtp.SmtpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Handle mailto URLs. To send mail using a mailto URLConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * call <code>getOutputStream</code>, write the message to the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * stream, and close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class MailToURLConnection extends URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    SmtpClient client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    Permission permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int connectTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int readTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    MailToURLConnection(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        MessageHeader props = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        props.add("content-type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        setProperties(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Get the user's full email address - stolen from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * HotJavaApplet.getMailAddress().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    String getFromAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String str = System.getProperty("user.fromaddr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            str = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                String host = System.getProperty("mail.host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        host = InetAddress.getLocalHost().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                str += "@" + host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                str = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void connect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        client = new SmtpClient(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        client.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
    90
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public synchronized OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IOException("Cannot write output after reading input.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String to = ParseUtil.decode(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        client.from(getFromAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        client.to(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        os = client.startMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
   107
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public Permission getPermission() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (permission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            String host = client.getMailHost() + ":" + 25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            permission = new SocketPermission(host, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
   124
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
   129
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
79
09c7a5100eec 6651717: Debug output statement left in MailToURLConnection
jccollet
parents: 2
diff changeset
   136
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}