1 package org.apache.archiva.proxy.common;
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
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.wagon.Wagon;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.BeansException;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.stereotype.Service;
30 import javax.inject.Inject;
31 import java.lang.reflect.Method;
33 import java.util.Properties;
36 * @author Olivier Lamy
39 @Service ("wagonFactory")
40 public class DefaultWagonFactory
41 implements WagonFactory
44 private ApplicationContext applicationContext;
46 private Logger logger = LoggerFactory.getLogger( getClass() );
48 private DebugTransferListener debugTransferListener = new DebugTransferListener();
51 public DefaultWagonFactory( ApplicationContext applicationContext )
53 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 Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
66 wagon.addTransferListener( debugTransferListener );
67 configureUserAgent( wagon, wagonFactoryRequest );
70 catch ( BeansException e )
72 throw new WagonFactoryException( e.getMessage(), e );
76 protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
80 Class clazz = wagon.getClass();
81 Method getHttpHeaders = clazz.getMethod( "getHttpHeaders", null );
83 Properties headers = (Properties) getHttpHeaders.invoke( wagon, null );
84 if ( headers == null )
86 headers = new Properties();
89 headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
91 if ( !wagonFactoryRequest.getHeaders().isEmpty() )
93 for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() )
95 headers.put( entry.getKey(), entry.getValue() );
99 Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } );
100 setHttpHeaders.invoke( wagon, headers );
102 logger.debug( "http headers set to: {}", headers );
104 catch ( Exception e )
106 logger.warn( "fail to configure User-Agent: " + e.getMessage(), e );