This project has retired. For details please refer to its
Attic page.
DefaultWagonFactory xref
1 package org.apache.archiva.proxy.common;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
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
67
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 }