You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2013/09/13 22:54:04 UTC

git commit: [WAGON-401] define User-Agent header by default

Updated Branches:
  refs/heads/master 3b4347891 -> a3979843a


[WAGON-401] define User-Agent header by default

Project: http://git-wip-us.apache.org/repos/asf/maven-wagon/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-wagon/commit/a3979843
Tree: http://git-wip-us.apache.org/repos/asf/maven-wagon/tree/a3979843
Diff: http://git-wip-us.apache.org/repos/asf/maven-wagon/diff/a3979843

Branch: refs/heads/master
Commit: a3979843a06f6d8d29dc81a6612e92c677650ca7
Parents: 3b43478
Author: Hervé Boutemy <hb...@apache.org>
Authored: Fri Sep 13 22:54:01 2013 +0200
Committer: Hervé Boutemy <hb...@apache.org>
Committed: Fri Sep 13 22:54:01 2013 +0200

----------------------------------------------------------------------
 .../providers/http/AbstractHttpClientWagon.java | 34 +++++++++++++
 .../http/AbstractHttpClientWagonTest.java       | 51 ++++++++++++++++++++
 2 files changed, 85 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/a3979843/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
index 995c1f2..6fac842 100644
--- a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
+++ b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
@@ -101,6 +101,7 @@ import org.codehaus.plexus.util.StringUtils;
 public abstract class AbstractHttpClientWagon
     extends StreamWagon
 {
+    private static String defaultUserAgent;
 
     private BasicHttpContext localContext;
 
@@ -799,6 +800,7 @@ public abstract class AbstractHttpClientWagon
             method.addHeader( "Pragma", "no-cache" );
             method.addHeader( "Expires", "0" );
             method.addHeader( "Accept-Encoding", "gzip" );
+            method.addHeader( "User-Agent", getDefaultUserAgent() );
         }
 
         if ( httpHeaders != null )
@@ -819,6 +821,38 @@ public abstract class AbstractHttpClientWagon
         }
     }
 
+    private String getDefaultUserAgent()
+    {
+        if ( defaultUserAgent == null )
+        {
+            defaultUserAgent =
+                "Apache-Maven-Wagon/" + getWagonVersion() + " (Java " + System.getProperty( "java.version" ) + "; "
+                    + System.getProperty( "os.name" ) + " " + System.getProperty( "os.version" ) + ")";
+        }
+        return defaultUserAgent;
+    }
+
+    private String getWagonVersion()
+    {
+        Properties props = new Properties();
+
+        InputStream is = getClass().getResourceAsStream( "/META-INF/maven/org.apache.maven.wagon/wagon-http/pom.properties" );
+        if ( is != null )
+        {
+            try
+            {
+                props.load( is );
+            }
+            catch ( IOException e )
+            {
+                // ignore
+            }
+            IOUtil.close( is );
+        }
+
+        return props.getProperty( "version", "unknown-version" );
+    }
+
     protected String getUserAgent( HttpUriRequest method )
     {
         if ( httpHeaders != null )

http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/a3979843/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagonTest.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagonTest.java b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagonTest.java
new file mode 100644
index 0000000..c1814f4
--- /dev/null
+++ b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagonTest.java
@@ -0,0 +1,51 @@
+package org.apache.maven.wagon.providers.http;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.wagon.InputData;
+import org.apache.maven.wagon.repository.Repository;
+import org.apache.maven.wagon.resource.Resource;
+import org.junit.Test;
+
+public class AbstractHttpClientWagonTest
+{
+    @Test
+    public void test()
+        throws Exception
+    {
+        AbstractHttpClientWagon wagon = new AbstractHttpClientWagon()
+        {
+        };
+
+        Repository repository = new Repository( "central", "http://repo.maven.apache.org/maven2" );
+
+        wagon.connect( repository );
+
+        Resource resource = new Resource();
+
+        resource.setName( "junit/junit/maven-metadata.xml" );
+
+        InputData inputData = new InputData();
+
+        inputData.setResource( resource );
+
+        wagon.fillInputData( inputData );
+    }
+}