/**
   * Creates a proxy server instance from the given properties.
   *
   * <p>Currently the default http.* proxy properties are supported as well as properties specific
   * for AHC.
   *
   * @param properties the properties to evaluate. Must not be null.
   * @return a ProxyServer instance or null, if no valid properties were set.
   * @see <a
   *     href="http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html">Networking
   *     Properties</a>
   * @see #PROXY_HOST
   * @see #PROXY_PORT
   * @see #PROXY_PROTOCOL
   * @see #PROXY_NONPROXYHOSTS
   */
  public static ProxyServer createProxy(Properties properties) {
    String host = System.getProperty(PROXY_HOST);

    if (host != null) {
      int port = Integer.valueOf(System.getProperty(PROXY_PORT, "80"));

      Protocol protocol;
      try {
        protocol = Protocol.valueOf(System.getProperty(PROXY_PROTOCOL, "HTTP"));
      } catch (IllegalArgumentException e) {
        protocol = Protocol.HTTP;
      }

      ProxyServer proxyServer =
          new ProxyServer(
              protocol,
              host,
              port,
              System.getProperty(PROXY_USER),
              System.getProperty(PROXY_PASSWORD));

      String nonProxyHosts = System.getProperties().getProperty(PROXY_NONPROXYHOSTS);
      if (nonProxyHosts != null) {
        for (String spec : nonProxyHosts.split("\\|")) {
          proxyServer.addNonProxyHost(spec);
        }
      }

      return proxyServer;
    }

    return null;
  }