This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.proxy.maven;
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 org.apache.commons.lang3.StringUtils;
023import org.apache.maven.wagon.Wagon;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.beans.BeansException;
027import org.springframework.context.ApplicationContext;
028import org.springframework.stereotype.Service;
029
030import javax.inject.Inject;
031import java.lang.reflect.Method;
032import java.util.Map;
033import java.util.Properties;
034
035/**
036 * @author Olivier Lamy
037 * @since 1.4-M1
038 */
039@Service ("wagonFactory")
040public class DefaultWagonFactory
041    implements WagonFactory
042{
043
044    private ApplicationContext applicationContext;
045
046    private Logger logger = LoggerFactory.getLogger( getClass() );
047
048    private DebugTransferListener debugTransferListener = new DebugTransferListener();
049
050    @Inject
051    public DefaultWagonFactory( ApplicationContext applicationContext )
052    {
053        this.applicationContext = applicationContext;
054    }
055
056    @Override
057    public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest )
058        throws WagonFactoryException
059    {
060        try
061        {
062            String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" )
063                ? wagonFactoryRequest.getProtocol()
064                : "wagon#" + wagonFactoryRequest.getProtocol();
065
066            // if it's a ntlm proxy we have to lookup the wagon light which support thats
067            // wagon http client doesn't support that
068            if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() )
069            {
070                protocol = protocol + "-ntlm";
071            }
072
073            Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
074            wagon.addTransferListener( debugTransferListener );
075            configureUserAgent( wagon, wagonFactoryRequest );
076            return wagon;
077        }
078        catch ( BeansException e )
079        {
080            throw new WagonFactoryException( e.getMessage(), e );
081        }
082    }
083
084    protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
085    {
086        try
087        {
088            Class<? extends Wagon> clazz = wagon.getClass();
089            Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" );
090
091            Properties headers = (Properties) getHttpHeaders.invoke( wagon );
092            if ( headers == null )
093            {
094                headers = new Properties();
095            }
096
097            headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
098
099            if ( !wagonFactoryRequest.getHeaders().isEmpty() )
100            {
101                for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() )
102                {
103                    headers.put( entry.getKey(), entry.getValue() );
104                }
105            }
106
107            Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } );
108            setHttpHeaders.invoke( wagon, headers );
109
110            logger.debug( "http headers set to: {}", headers );
111        }
112        catch ( Exception e )
113        {
114            logger.warn( "fail to configure User-Agent: {}", e.getMessage(), e );
115        }
116    }
117}