You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultWagonFactory.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.apache.archiva.proxy.maven;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import org.apache.commons.lang.StringUtils;
  21. import org.apache.maven.wagon.Wagon;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.BeansException;
  25. import org.springframework.context.ApplicationContext;
  26. import org.springframework.stereotype.Service;
  27. import javax.inject.Inject;
  28. import java.lang.reflect.Method;
  29. import java.util.Map;
  30. import java.util.Properties;
  31. /**
  32. * @author Olivier Lamy
  33. * @since 1.4-M1
  34. */
  35. @Service ("wagonFactory")
  36. public class DefaultWagonFactory
  37. implements WagonFactory
  38. {
  39. private ApplicationContext applicationContext;
  40. private Logger logger = LoggerFactory.getLogger( getClass() );
  41. private DebugTransferListener debugTransferListener = new DebugTransferListener();
  42. @Inject
  43. public DefaultWagonFactory( ApplicationContext applicationContext )
  44. {
  45. this.applicationContext = applicationContext;
  46. }
  47. @Override
  48. public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest )
  49. throws WagonFactoryException
  50. {
  51. try
  52. {
  53. String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" )
  54. ? wagonFactoryRequest.getProtocol()
  55. : "wagon#" + wagonFactoryRequest.getProtocol();
  56. // if it's a ntlm proxy we have to lookup the wagon light which support thats
  57. // wagon http client doesn't support that
  58. if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() )
  59. {
  60. protocol = protocol + "-ntlm";
  61. }
  62. Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
  63. wagon.addTransferListener( debugTransferListener );
  64. configureUserAgent( wagon, wagonFactoryRequest );
  65. return wagon;
  66. }
  67. catch ( BeansException e )
  68. {
  69. throw new WagonFactoryException( e.getMessage(), e );
  70. }
  71. }
  72. protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
  73. {
  74. try
  75. {
  76. Class<? extends Wagon> clazz = wagon.getClass();
  77. Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" );
  78. Properties headers = (Properties) getHttpHeaders.invoke( wagon );
  79. if ( headers == null )
  80. {
  81. headers = new Properties();
  82. }
  83. headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
  84. if ( !wagonFactoryRequest.getHeaders().isEmpty() )
  85. {
  86. for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() )
  87. {
  88. headers.put( entry.getKey(), entry.getValue() );
  89. }
  90. }
  91. Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } );
  92. setHttpHeaders.invoke( wagon, headers );
  93. logger.debug( "http headers set to: {}", headers );
  94. }
  95. catch ( Exception e )
  96. {
  97. logger.warn( "fail to configure User-Agent: {}", e.getMessage(), e );
  98. }
  99. }
  100. }