Esempio n. 1
0
  /**
   * Decodes a SmtpTransport URI.
   *
   * <p>Possible forms:
   *
   * <pre>
   * smtp://user:password@server:port CONNECTION_SECURITY_NONE
   * smtp+tls://user:password@server:port CONNECTION_SECURITY_TLS_OPTIONAL
   * smtp+tls+://user:password@server:port CONNECTION_SECURITY_TLS_REQUIRED
   * smtp+ssl+://user:password@server:port CONNECTION_SECURITY_SSL_REQUIRED
   * smtp+ssl://user:password@server:port CONNECTION_SECURITY_SSL_OPTIONAL
   * </pre>
   */
  public static ServerSettings decodeUri(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String authenticationType = AUTH_AUTOMATIC;
    String username = null;
    String password = null;

    URI smtpUri;
    try {
      smtpUri = new URI(uri);
    } catch (URISyntaxException use) {
      throw new IllegalArgumentException("Invalid SmtpTransport URI", use);
    }

    String scheme = smtpUri.getScheme();
    if (scheme.equals("smtp")) {
      connectionSecurity = ConnectionSecurity.NONE;
      port = 25;
    } else if (scheme.equals("smtp+tls")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_OPTIONAL;
      port = 25;
    } else if (scheme.equals("smtp+tls+")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
      port = 25;
    } else if (scheme.equals("smtp+ssl+")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
      port = 465;
    } else if (scheme.equals("smtp+ssl")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_OPTIONAL;
      port = 465;
    } else {
      throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }

    host = smtpUri.getHost();

    if (smtpUri.getPort() != -1) {
      port = smtpUri.getPort();
    }

    if (smtpUri.getUserInfo() != null) {
      try {
        String[] userInfoParts = smtpUri.getUserInfo().split(":");
        username = URLDecoder.decode(userInfoParts[0], "UTF-8");
        if (userInfoParts.length > 1) {
          password = URLDecoder.decode(userInfoParts[1], "UTF-8");
        }
        if (userInfoParts.length > 2) {
          authenticationType = userInfoParts[2];
        }
      } catch (UnsupportedEncodingException enc) {
        // This shouldn't happen since the encoding is hardcoded to UTF-8
        throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
      }
    }

    return new ServerSettings(
        TRANSPORT_TYPE, host, port, connectionSecurity, authenticationType, username, password);
  }
  /**
   * Decodes a Pop3Store URI.
   *
   * <p>Possible forms:
   *
   * <pre>
   * pop3://authType:user:password@server:port
   *      ConnectionSecurity.NONE
   * pop3+tls+://authType:user:password@server:port
   *      ConnectionSecurity.STARTTLS_REQUIRED
   * pop3+ssl+://authType:user:password@server:port
   *      ConnectionSecurity.SSL_TLS_REQUIRED
   * </pre>
   *
   * e.g.
   *
   * <pre>pop3://PLAIN:admin:[email protected]:12345</pre>
   */
  public static ServerSettings decodeUri(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;

    URI pop3Uri;
    try {
      pop3Uri = new URI(uri);
    } catch (URISyntaxException use) {
      throw new IllegalArgumentException("Invalid Pop3Store URI", use);
    }

    String scheme = pop3Uri.getScheme();
    /*
     * Currently available schemes are:
     * pop3
     * pop3+tls+
     * pop3+ssl+
     *
     * The following are obsolete schemes that may be found in pre-existing
     * settings from earlier versions or that may be found when imported. We
     * continue to recognize them and re-map them appropriately:
     * pop3+tls
     * pop3+ssl
     */
    if (scheme.equals("pop3")) {
      connectionSecurity = ConnectionSecurity.NONE;
      port = Type.POP3.defaultPort;
    } else if (scheme.startsWith("pop3+tls")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
      port = Type.POP3.defaultPort;
    } else if (scheme.startsWith("pop3+ssl")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
      port = Type.POP3.defaultTlsPort;
    } else {
      throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }

    host = pop3Uri.getHost();

    if (pop3Uri.getPort() != -1) {
      port = pop3Uri.getPort();
    }

    AuthType authType = AuthType.PLAIN;
    if (pop3Uri.getUserInfo() != null) {
      int userIndex = 0, passwordIndex = 1;
      String userinfo = pop3Uri.getUserInfo();
      String[] userInfoParts = userinfo.split(":");
      if (userInfoParts.length > 2 || userinfo.endsWith(":")) {
        // If 'userinfo' ends with ":" the password is empty. This can only happen
        // after an account was imported (so authType and username are present).
        userIndex++;
        passwordIndex++;
        authType = AuthType.valueOf(userInfoParts[0]);
      }
      username = decodeUtf8(userInfoParts[userIndex]);
      if (userInfoParts.length > passwordIndex) {
        if (authType == AuthType.EXTERNAL) {
          clientCertificateAlias = decodeUtf8(userInfoParts[passwordIndex]);
        } else {
          password = decodeUtf8(userInfoParts[passwordIndex]);
        }
      }
    }

    return new ServerSettings(
        ServerSettings.Type.POP3,
        host,
        port,
        connectionSecurity,
        authType,
        username,
        password,
        clientCertificateAlias);
  }