1 package org.apache.archiva.maven.proxy;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.maven.wagon.Wagon;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.BeansException;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.stereotype.Service;
29 import javax.inject.Inject;
30 import java.lang.reflect.Method;
32 import java.util.Properties;
35 * @author Olivier Lamy
38 @Service ("wagonFactory")
39 public class DefaultWagonFactory
40 implements WagonFactory
43 private ApplicationContext applicationContext;
45 private Logger logger = LoggerFactory.getLogger( getClass() );
47 private DebugTransferListener debugTransferListener = new DebugTransferListener();
50 public DefaultWagonFactory( ApplicationContext applicationContext )
52 this.applicationContext = applicationContext;
56 public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest )
57 throws WagonFactoryException
61 String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" )
62 ? wagonFactoryRequest.getProtocol()
63 : "wagon#" + wagonFactoryRequest.getProtocol();
65 // if it's a ntlm proxy we have to lookup the wagon light which support thats
66 // wagon http client doesn't support that
67 if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() )
69 protocol = protocol + "-ntlm";
72 Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
73 wagon.addTransferListener( debugTransferListener );
74 configureUserAgent( wagon, wagonFactoryRequest );
77 catch ( BeansException e )
79 throw new WagonFactoryException( e.getMessage(), e );
83 protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
87 Class<? extends Wagon> clazz = wagon.getClass();
88 Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" );
90 Properties headers = (Properties) getHttpHeaders.invoke( wagon );
91 if ( headers == null )
93 headers = new Properties();
96 headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
98 if ( !wagonFactoryRequest.getHeaders().isEmpty() )
100 for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() )
102 headers.put( entry.getKey(), entry.getValue() );
106 Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } );
107 setHttpHeaders.invoke( wagon, headers );
109 logger.debug( "http headers set to: {}", headers );
111 catch ( Exception e )
113 logger.warn( "fail to configure User-Agent: {}", e.getMessage(), e );