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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package org.apache.archiva.maven.common.proxy;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.maven.wagon.Wagon;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.BeansException;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.stereotype.Service;
  26. import javax.inject.Inject;
  27. import java.lang.reflect.Method;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. /**
  31. * @author Olivier Lamy
  32. * @since 1.4-M1
  33. */
  34. @Service ("wagonFactory")
  35. public class DefaultWagonFactory
  36. implements WagonFactory
  37. {
  38. private ApplicationContext applicationContext;
  39. private Logger logger = LoggerFactory.getLogger( getClass() );
  40. private DebugTransferListener debugTransferListener = new DebugTransferListener();
  41. @Inject
  42. public DefaultWagonFactory( ApplicationContext applicationContext )
  43. {
  44. this.applicationContext = applicationContext;
  45. }
  46. @Override
  47. public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest )
  48. throws WagonFactoryException
  49. {
  50. try
  51. {
  52. String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" )
  53. ? wagonFactoryRequest.getProtocol()
  54. : "wagon#" + wagonFactoryRequest.getProtocol();
  55. // if it's a ntlm proxy we have to lookup the wagon light which support thats
  56. // wagon http client doesn't support that
  57. if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() )
  58. {
  59. protocol = protocol + "-ntlm";
  60. }
  61. Wagon wagon = applicationContext.getBean( protocol, Wagon.class );
  62. wagon.addTransferListener( debugTransferListener );
  63. configureUserAgent( wagon, wagonFactoryRequest );
  64. return wagon;
  65. }
  66. catch ( BeansException e )
  67. {
  68. throw new WagonFactoryException( e.getMessage(), e );
  69. }
  70. }
  71. protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest )
  72. {
  73. try
  74. {
  75. Class<? extends Wagon> clazz = wagon.getClass();
  76. Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" );
  77. Properties headers = (Properties) getHttpHeaders.invoke( wagon );
  78. if ( headers == null )
  79. {
  80. headers = new Properties();
  81. }
  82. headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() );
  83. if ( !wagonFactoryRequest.getHeaders().isEmpty() )
  84. {
  85. for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() )
  86. {
  87. headers.put( entry.getKey(), entry.getValue() );
  88. }
  89. }
  90. Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } );
  91. setHttpHeaders.invoke( wagon, headers );
  92. logger.debug( "http headers set to: {}", headers );
  93. }
  94. catch ( Exception e )
  95. {
  96. logger.warn( "fail to configure User-Agent: {}", e.getMessage(), e );
  97. }
  98. }
  99. }