author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 79 | 09c7a5100eec |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.net.www.protocol.mailto; |
|
27 |
||
28 |
import java.net.URL; |
|
29 |
import java.net.InetAddress; |
|
30 |
import java.net.SocketPermission; |
|
31 |
import java.io.*; |
|
32 |
import java.security.Permission; |
|
33 |
import sun.net.www.*; |
|
34 |
import sun.net.smtp.SmtpClient; |
|
35 |
import sun.net.www.ParseUtil; |
|
36 |
||
37 |
||
38 |
/** |
|
39 |
* Handle mailto URLs. To send mail using a mailto URLConnection, |
|
40 |
* call <code>getOutputStream</code>, write the message to the output |
|
41 |
* stream, and close it. |
|
42 |
* |
|
43 |
*/ |
|
44 |
public class MailToURLConnection extends URLConnection { |
|
45 |
InputStream is = null; |
|
46 |
OutputStream os = null; |
|
47 |
||
48 |
SmtpClient client; |
|
49 |
Permission permission; |
|
50 |
private int connectTimeout = -1; |
|
51 |
private int readTimeout = -1; |
|
52 |
||
53 |
MailToURLConnection(URL u) { |
|
54 |
super(u); |
|
55 |
||
56 |
MessageHeader props = new MessageHeader(); |
|
57 |
props.add("content-type", "text/html"); |
|
58 |
setProperties(props); |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Get the user's full email address - stolen from |
|
63 |
* HotJavaApplet.getMailAddress(). |
|
64 |
*/ |
|
65 |
String getFromAddress() { |
|
66 |
String str = System.getProperty("user.fromaddr"); |
|
67 |
if (str == null) { |
|
68 |
str = System.getProperty("user.name"); |
|
69 |
if (str != null) { |
|
70 |
String host = System.getProperty("mail.host"); |
|
71 |
if (host == null) { |
|
72 |
try { |
|
73 |
host = InetAddress.getLocalHost().getHostName(); |
|
74 |
} catch (java.net.UnknownHostException e) { |
|
75 |
} |
|
76 |
} |
|
77 |
str += "@" + host; |
|
78 |
} else { |
|
79 |
str = ""; |
|
80 |
} |
|
81 |
} |
|
82 |
return str; |
|
83 |
} |
|
84 |
||
85 |
public void connect() throws IOException { |
|
86 |
client = new SmtpClient(connectTimeout); |
|
87 |
client.setReadTimeout(readTimeout); |
|
88 |
} |
|
89 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
90 |
@Override |
2 | 91 |
public synchronized OutputStream getOutputStream() throws IOException { |
92 |
if (os != null) { |
|
93 |
return os; |
|
94 |
} else if (is != null) { |
|
95 |
throw new IOException("Cannot write output after reading input."); |
|
96 |
} |
|
97 |
connect(); |
|
98 |
||
99 |
String to = ParseUtil.decode(url.getPath()); |
|
100 |
client.from(getFromAddress()); |
|
101 |
client.to(to); |
|
102 |
||
103 |
os = client.startMessage(); |
|
104 |
return os; |
|
105 |
} |
|
106 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
107 |
@Override |
2 | 108 |
public Permission getPermission() throws IOException { |
109 |
if (permission == null) { |
|
110 |
connect(); |
|
111 |
String host = client.getMailHost() + ":" + 25; |
|
112 |
permission = new SocketPermission(host, "connect"); |
|
113 |
} |
|
114 |
return permission; |
|
115 |
} |
|
116 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
117 |
@Override |
2 | 118 |
public void setConnectTimeout(int timeout) { |
119 |
if (timeout < 0) |
|
120 |
throw new IllegalArgumentException("timeouts can't be negative"); |
|
121 |
connectTimeout = timeout; |
|
122 |
} |
|
123 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
124 |
@Override |
2 | 125 |
public int getConnectTimeout() { |
126 |
return (connectTimeout < 0 ? 0 : connectTimeout); |
|
127 |
} |
|
128 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
129 |
@Override |
2 | 130 |
public void setReadTimeout(int timeout) { |
131 |
if (timeout < 0) |
|
132 |
throw new IllegalArgumentException("timeouts can't be negative"); |
|
133 |
readTimeout = timeout; |
|
134 |
} |
|
135 |
||
79
09c7a5100eec
6651717: Debug output statement left in MailToURLConnection
jccollet
parents:
2
diff
changeset
|
136 |
@Override |
2 | 137 |
public int getReadTimeout() { |
138 |
return readTimeout < 0 ? 0 : readTimeout; |
|
139 |
} |
|
140 |
} |