blob: df4d3306735cb31efca42fba1307ad7f2ca53bdf [file] [log] [blame]
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001/*
2 * Copyright (c) 1996, 2008, 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
26package sun.net.www.protocol.mailto;
27
28import java.net.URL;
29import java.net.InetAddress;
30import java.net.SocketPermission;
31import java.io.*;
32import java.security.Permission;
33import sun.net.www.*;
34import sun.net.smtp.SmtpClient;
35import 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 */
44public 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
90 @Override
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
107 @Override
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
117 @Override
118 public void setConnectTimeout(int timeout) {
119 if (timeout < 0)
120 throw new IllegalArgumentException("timeouts can't be negative");
121 connectTimeout = timeout;
122 }
123
124 @Override
125 public int getConnectTimeout() {
126 return (connectTimeout < 0 ? 0 : connectTimeout);
127 }
128
129 @Override
130 public void setReadTimeout(int timeout) {
131 if (timeout < 0)
132 throw new IllegalArgumentException("timeouts can't be negative");
133 readTimeout = timeout;
134 }
135
136 @Override
137 public int getReadTimeout() {
138 return readTimeout < 0 ? 0 : readTimeout;
139 }
140}