This project has retired. For details please refer to its Attic page.
DefaultWagonFactory xref
View Javadoc
1   package org.apache.archiva.proxy.common;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
29  
30  import javax.inject.Inject;
31  import java.lang.reflect.Method;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  /**
36   * @author Olivier Lamy
37   * @since 1.4-M1
38   */
39  @Service ("wagonFactory")
40  public class DefaultWagonFactory
41      implements WagonFactory
42  {
43  
44      private ApplicationContext applicationContext;
45  
46      private Logger logger = LoggerFactory.getLogger( getClass() );
47  
48      private DebugTransferListener debugTransferListener = new DebugTransferListener();
49  
50      @Inject
51      public DefaultWagonFactory( ApplicationContext applicationContext )
52      {
53          this.applicationContext = applicationContext;
54      }
55  
56      @Override
57      public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest )
58          throws WagonFactoryException
59      {
60          try
61          {
62              String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" )
63                  ? wagonFactoryRequest.getProtocol()
64                  : "wagon#" + wagonFactoryRequest.getProtocol();
65  
66              // if it's a ntlm proxy we have to lookup the wagon light which support thats
67              // wagon http client doesn't support that
68              if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() )
69              {
70                  protocol = protocol + "-ntlm";
71              }
72  
73              Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
74              wagon.addTransferListener( debugTransferListener );
75              configureUserAgent( wagon, wagonFactoryRequest );
76              return wagon;
77          }
78          catch ( BeansException e )
79          {
80              throw new WagonFactoryException( e.getMessage(), e );
81          }
82      }
83  
84      protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
85      {
86          try
87          {
88              Class clazz = wagon.getClass();
89              Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" );
90  
91              Properties headers = (Properties) getHttpHeaders.invoke( wagon );
92              if ( headers == null )
93              {
94                  headers = new Properties();
95              }
96  
97              headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
98  
99              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 }