001package org.apache.maven.wagon.proxy;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.StringTokenizer;
023
024/**
025 * @author <a href="mailto:lafeuil@gmail.com">Thomas Champagne</a>
026 */
027public final class ProxyUtils
028{
029    private ProxyUtils()
030    {
031    }
032
033    /**
034     * Check if the specified host is in the list of non proxy hosts.
035     * 
036     * @param proxy the proxy info object contains set of properties.
037     * @param targetHost the target hostname
038     * @return true if the hostname is in the list of non proxy hosts, false otherwise.
039     */
040    public static boolean validateNonProxyHosts( ProxyInfo proxy, String targetHost )
041    {
042        if ( targetHost == null )
043        {
044            targetHost = new String();
045        }
046        if ( proxy == null )
047        {
048            return false;
049        }
050        String nonProxyHosts = proxy.getNonProxyHosts();
051        if ( nonProxyHosts == null )
052        {
053            return false;
054        }
055
056        StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" );
057
058        while ( tokenizer.hasMoreTokens() )
059        {
060            String pattern = tokenizer.nextToken();
061            pattern = pattern.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" );
062            if ( targetHost.matches( pattern ) )
063            {
064                return true;
065            }
066        }
067        return false;
068    }
069}