0N/A/*
2362N/A * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.net.www.protocol.mailto;
0N/A
0N/Aimport java.net.URL;
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.SocketPermission;
0N/Aimport java.io.*;
0N/Aimport java.security.Permission;
0N/Aimport sun.net.www.*;
0N/Aimport sun.net.smtp.SmtpClient;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A
0N/A/**
0N/A * Handle mailto URLs. To send mail using a mailto URLConnection,
0N/A * call <code>getOutputStream</code>, write the message to the output
0N/A * stream, and close it.
0N/A *
0N/A */
0N/Apublic class MailToURLConnection extends URLConnection {
0N/A InputStream is = null;
0N/A OutputStream os = null;
0N/A
0N/A SmtpClient client;
0N/A Permission permission;
0N/A private int connectTimeout = -1;
0N/A private int readTimeout = -1;
0N/A
0N/A MailToURLConnection(URL u) {
0N/A super(u);
0N/A
0N/A MessageHeader props = new MessageHeader();
0N/A props.add("content-type", "text/html");
0N/A setProperties(props);
0N/A }
0N/A
0N/A /**
0N/A * Get the user's full email address - stolen from
0N/A * HotJavaApplet.getMailAddress().
0N/A */
0N/A String getFromAddress() {
0N/A String str = System.getProperty("user.fromaddr");
0N/A if (str == null) {
0N/A str = System.getProperty("user.name");
0N/A if (str != null) {
0N/A String host = System.getProperty("mail.host");
0N/A if (host == null) {
0N/A try {
0N/A host = InetAddress.getLocalHost().getHostName();
0N/A } catch (java.net.UnknownHostException e) {
0N/A }
0N/A }
0N/A str += "@" + host;
0N/A } else {
0N/A str = "";
0N/A }
0N/A }
0N/A return str;
0N/A }
0N/A
0N/A public void connect() throws IOException {
0N/A client = new SmtpClient(connectTimeout);
0N/A client.setReadTimeout(readTimeout);
0N/A }
0N/A
56N/A @Override
0N/A public synchronized OutputStream getOutputStream() throws IOException {
0N/A if (os != null) {
0N/A return os;
0N/A } else if (is != null) {
0N/A throw new IOException("Cannot write output after reading input.");
0N/A }
0N/A connect();
0N/A
0N/A String to = ParseUtil.decode(url.getPath());
0N/A client.from(getFromAddress());
0N/A client.to(to);
0N/A
0N/A os = client.startMessage();
0N/A return os;
0N/A }
0N/A
56N/A @Override
0N/A public Permission getPermission() throws IOException {
0N/A if (permission == null) {
0N/A connect();
0N/A String host = client.getMailHost() + ":" + 25;
0N/A permission = new SocketPermission(host, "connect");
0N/A }
0N/A return permission;
0N/A }
0N/A
56N/A @Override
0N/A public void setConnectTimeout(int timeout) {
0N/A if (timeout < 0)
0N/A throw new IllegalArgumentException("timeouts can't be negative");
0N/A connectTimeout = timeout;
0N/A }
0N/A
56N/A @Override
0N/A public int getConnectTimeout() {
0N/A return (connectTimeout < 0 ? 0 : connectTimeout);
0N/A }
0N/A
56N/A @Override
0N/A public void setReadTimeout(int timeout) {
0N/A if (timeout < 0)
0N/A throw new IllegalArgumentException("timeouts can't be negative");
0N/A readTimeout = timeout;
0N/A }
0N/A
56N/A @Override
0N/A public int getReadTimeout() {
0N/A return readTimeout < 0 ? 0 : readTimeout;
0N/A }
0N/A}