src/java.base/share/classes/sun/net/www/protocol/mailto/Handler.java
author rriggs
Fri, 07 Dec 2018 11:51:17 -0500
changeset 52902 e3398b2e1ab0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214971: Replace use of string.equals("") with isEmpty() Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10596
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      mailto stream opener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.net.www.protocol.mailto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URLStreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
//import sun.net.www.protocol.news.ArticlePoster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.net.smtp.SmtpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/** open an nntp input stream given a URL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Handler extends URLStreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
//     private String decodePercent(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
//      if (s==null || s.indexOf('%') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
//          return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
//      int limit = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
//      char d[] = new char[limit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
//      int dp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
//      for (int sp = 0; sp < limit; sp++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
//          int c = s.charAt(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
//          if (c == '%' && sp + 2 < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
//              int s1 = s.charAt(sp + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
//              int s2 = s.charAt(sp + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
//              if ('0' <= s1 && s1 <= '9')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
//                  s1 = s1 - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
//              else if ('a' <= s1 && s1 <= 'f')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
//                  s1 = s1 - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
//              else if ('A' <= s1 && s1 <= 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
//                  s1 = s1 - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
//              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
//                  s1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
//              if ('0' <= s2 && s2 <= '9')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
//                  s2 = s2 - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
//              else if ('a' <= s2 && s2 <= 'f')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
//                  s2 = s2 - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
//              else if ('A' <= s2 && s2 <= 'F')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
//                  s2 = s2 - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
//              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
//                  s2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
//              if (s1 >= 0 && s2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
//                  c = (s1 << 4) | s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
//                  sp += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
//              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
//          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
//          d[dp++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
//      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
//      return new String(d, 0, dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
//     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
//     public InputStream openStream(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
//          String dest = u.file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
//          String subj = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
//          int lastsl = dest.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
//          if (lastsl >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
//              int st = dest.charAt(0) == '/' ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
//              if (lastsl > st)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
//                  subj = dest.substring(st, lastsl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
//              dest = dest.substring(lastsl + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
//          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
//          if (u.postData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//              ArticlePoster.MailTo("Posted form",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
//                                   decodePercent(dest),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
//                                   u.postData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
//          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
//          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
//              ArticlePoster.MailTo(decodePercent(subj), decodePercent(dest));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
//      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
//     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public synchronized URLConnection openConnection(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return new MailToURLConnection(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * This method is called to parse the string spec into URL u for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * mailto protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param   u the URL to receive the result of parsing the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param   spec the URL string to parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param   start the character position to start parsing at.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          just past the ':'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param   limit the character position to stop parsing at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void parseURL(URL u, String spec, int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String protocol = u.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int port = u.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String file = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (start < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            file = spec.substring(start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Let's just make sure we DO have an Email address in the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        boolean nogood = false;
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   130
        if (file == null || file.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            nogood = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            boolean allwhites = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            for (int i = 0; i < file.length(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (!Character.isWhitespace(file.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    allwhites = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (allwhites)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                nogood = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (nogood)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new RuntimeException("No email address");
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   142
        setURLHandler(u, protocol, host, port, file, null);
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   143
    }
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   144
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   145
    /**
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   146
     * This method is used to suppress the deprecated warning
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   147
     *
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   148
     * @param   u the URL to receive the result of parsing the spec
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   149
     * @param   spec the URL string to parse
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   150
     * @param   start the character position to start parsing at.  This is
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   151
     *          just past the ':'.
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   152
     * @param   limit the character position to stop parsing at.
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   153
     */
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   154
    @SuppressWarnings("deprecation")
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   155
    private void setURLHandler(URL u, String protocol, String host, int port, String file, String ref) {
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   156
        setURL(u,protocol,host,port,file,null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}