/**
   * Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go
   * directly to target host. If <code>null</code> proxy is passed in, this method returns true --
   * since there is NO proxy, we should avoid to use it. Simple hostname pattern matching using "*"
   * are supported, but only as prefixes. See
   * http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
   *
   * @param proxyServer
   * @param target the hostname
   * @return true if we have to avoid proxy use (obeying non-proxy hosts settings), false otherwise.
   */
  public static boolean avoidProxy(final ProxyServer proxyServer, final String target) {
    if (proxyServer != null) {
      final String targetHost = target.toLowerCase();

      List<String> nonProxyHosts = proxyServer.getNonProxyHosts();

      if (nonProxyHosts != null && nonProxyHosts.size() > 0) {
        for (String nonProxyHost : nonProxyHosts) {
          if (nonProxyHost.startsWith("*")
              && nonProxyHost.length() > 1
              && targetHost.endsWith(nonProxyHost.substring(1).toLowerCase())) {
            return true;
          } else if (nonProxyHost.equalsIgnoreCase(targetHost)) {
            return true;
          }
        }
      }

      return false;
    } else {
      return true;
    }
  }