1 /*
   2  * Copyright (c) 1995, 2011, 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 
  26 /*-
  27  *      mailto stream opener
  28  */
  29 
  30 package sun.net.www.protocol.mailto;
  31 
  32 import java.net.URL;
  33 import java.net.URLConnection;
  34 import java.net.URLStreamHandler;
  35 import java.io.*;
  36 import sun.net.www.*;
  37 //import sun.net.www.protocol.news.ArticlePoster;
  38 import sun.net.smtp.SmtpClient;
  39 
  40 /** open an nntp input stream given a URL */
  41 public class Handler extends URLStreamHandler {
  42 
  43 /*
  44 //     private String decodePercent(String s) {
  45 //      if (s==null || s.indexOf('%') < 0)
  46 //          return s;
  47 //      int limit = s.length();
  48 //      char d[] = new char[limit];
  49 //      int dp = 0;
  50 //      for (int sp = 0; sp < limit; sp++) {
  51 //          int c = s.charAt(sp);
  52 //          if (c == '%' && sp + 2 < limit) {
  53 //              int s1 = s.charAt(sp + 1);
  54 //              int s2 = s.charAt(sp + 2);
  55 //              if ('0' <= s1 && s1 <= '9')
  56 //                  s1 = s1 - '0';
  57 //              else if ('a' <= s1 && s1 <= 'f')
  58 //                  s1 = s1 - 'a' + 10;
  59 //              else if ('A' <= s1 && s1 <= 'F')
  60 //                  s1 = s1 - 'A' + 10;
  61 //              else
  62 //                  s1 = -1;
  63 //              if ('0' <= s2 && s2 <= '9')
  64 //                  s2 = s2 - '0';
  65 //              else if ('a' <= s2 && s2 <= 'f')
  66 //                  s2 = s2 - 'a' + 10;
  67 //              else if ('A' <= s2 && s2 <= 'F')
  68 //                  s2 = s2 - 'A' + 10;
  69 //              else
  70 //                  s2 = -1;
  71 //              if (s1 >= 0 && s2 >= 0) {
  72 //                  c = (s1 << 4) | s2;
  73 //                  sp += 2;
  74 //              }
  75 //          }
  76 //          d[dp++] = (char) c;
  77 //      }
  78 //      return new String(d, 0, dp);
  79 //     }
  80 
  81 //     public InputStream openStream(URL u) {
  82 //          String dest = u.file;
  83 //          String subj = "";
  84 //          int lastsl = dest.lastIndexOf('/');
  85 //          if (lastsl >= 0) {
  86 //              int st = dest.charAt(0) == '/' ? 1 : 0;
  87 //              if (lastsl > st)
  88 //                  subj = dest.substring(st, lastsl);
  89 //              dest = dest.substring(lastsl + 1);
  90 //          }
  91 //          if (u.postData != null) {
  92 //              ArticlePoster.MailTo("Posted form",
  93 //                                   decodePercent(dest),
  94 //                                   u.postData);
  95 //          }
  96 //          else
  97 //              ArticlePoster.MailTo(decodePercent(subj), decodePercent(dest));
  98 //      return null;
  99 //     }
 100     */
 101 
 102     public synchronized URLConnection openConnection(URL u) {
 103         return new MailToURLConnection(u);
 104     }
 105 
 106     /**
 107      * This method is called to parse the string spec into URL u for a
 108      * mailto protocol.
 109      *
 110      * @param   u the URL to receive the result of parsing the spec
 111      * @param   spec the URL string to parse
 112      * @param   start the character position to start parsing at.  This is
 113      *          just past the ':'.
 114      * @param   limit the character position to stop parsing at.
 115      */
 116     public void parseURL(URL u, String spec, int start, int limit) {
 117 
 118         String protocol = u.getProtocol();
 119         String host = "";
 120         int port = u.getPort();
 121         String file = "";
 122 
 123         if (start < limit) {
 124             file = spec.substring(start, limit);
 125         }
 126         /*
 127          * Let's just make sure we DO have an Email address in the URL.
 128          */
 129         boolean nogood = false;
 130         if (file == null || file.isEmpty())
 131             nogood = true;
 132         else {
 133             boolean allwhites = true;
 134             for (int i = 0; i < file.length(); i++)
 135                 if (!Character.isWhitespace(file.charAt(i)))
 136                     allwhites = false;
 137             if (allwhites)
 138                 nogood = true;
 139         }
 140         if (nogood)
 141             throw new RuntimeException("No email address");
 142         setURLHandler(u, protocol, host, port, file, null);
 143     }
 144 
 145     /**
 146      * This method is used to suppress the deprecated warning
 147      *
 148      * @param   u the URL to receive the result of parsing the spec
 149      * @param   spec the URL string to parse
 150      * @param   start the character position to start parsing at.  This is
 151      *          just past the ':'.
 152      * @param   limit the character position to stop parsing at.
 153      */
 154     @SuppressWarnings("deprecation")
 155     private void setURLHandler(URL u, String protocol, String host, int port, String file, String ref) {
 156         setURL(u,protocol,host,port,file,null);
 157     }
 158 }