Repository: flex-utilities
Updated Branches:
  refs/heads/develop 6c48e3d3d -> a84cdf1d3


Added a first version of support for proxys ... unfortnately I currently can't 
really test authenticated proxies.


Project: http://git-wip-us.apache.org/repos/asf/flex-utilities/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-utilities/commit/a84cdf1d
Tree: http://git-wip-us.apache.org/repos/asf/flex-utilities/tree/a84cdf1d
Diff: http://git-wip-us.apache.org/repos/asf/flex-utilities/diff/a84cdf1d

Branch: refs/heads/develop
Commit: a84cdf1d3c72aa6c6d25c28603e6b72c6862e91d
Parents: 6c48e3d
Author: Christofer Dutz <[email protected]>
Authored: Thu Jul 2 15:51:31 2015 +0200
Committer: Christofer Dutz <[email protected]>
Committed: Thu Jul 2 15:51:31 2015 +0200

----------------------------------------------------------------------
 .../converter/mavenextension/FlexEventSpy.java  | 21 ++++-
 .../converter/retrievers/Retriever.java         |  4 +
 .../retrievers/model/ProxySettings.java         | 48 +++++++++++
 .../mavenizer/retrievers/download/pom.xml       | 16 +++-
 .../retrievers/download/DownloadRetriever.java  | 54 +++++++++---
 .../retrievers/download/ProxyTest.java          | 88 ++++++++++++++++++++
 6 files changed, 217 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/maven-extension/src/main/java/org/apache/flex/utilities/converter/mavenextension/FlexEventSpy.java
----------------------------------------------------------------------
diff --git 
a/flex-maven-tools/mavenizer/maven-extension/src/main/java/org/apache/flex/utilities/converter/mavenextension/FlexEventSpy.java
 
b/flex-maven-tools/mavenizer/maven-extension/src/main/java/org/apache/flex/utilities/converter/mavenextension/FlexEventSpy.java
index 863d288..696592e 100644
--- 
a/flex-maven-tools/mavenizer/maven-extension/src/main/java/org/apache/flex/utilities/converter/mavenextension/FlexEventSpy.java
+++ 
b/flex-maven-tools/mavenizer/maven-extension/src/main/java/org/apache/flex/utilities/converter/mavenextension/FlexEventSpy.java
@@ -7,6 +7,7 @@ import org.apache.flex.utilities.converter.flex.FlexConverter;
 import org.apache.flex.utilities.converter.fontkit.FontkitConverter;
 import 
org.apache.flex.utilities.converter.retrievers.download.DownloadRetriever;
 import org.apache.flex.utilities.converter.retrievers.types.PlatformType;
+import org.apache.flex.utilities.converter.retrievers.model.ProxySettings;
 import org.apache.flex.utilities.converter.retrievers.types.SdkType;
 import org.apache.flex.utilities.converter.wrapper.WrapperConverter;
 import org.apache.maven.MavenExecutionException;
@@ -176,13 +177,19 @@ public class FlexEventSpy extends AbstractEventSpy {
         try {
             File localRepoBaseDir = new 
File(mavenSession.getLocalRepository().getBasedir());
             DownloadRetriever downloadRetriever = new DownloadRetriever();
+
+            ProxySettings proxySettings = null;
+            if(mavenSession.getSettings().getActiveProxy() != null) {
+                proxySettings = getProxySettings();
+            }
+
             PlatformType platformType;
             if(System.getProperty("platform-type") == null) {
                 platformType = PlatformType.getCurrent();
             } else {
                 platformType = 
PlatformType.valueOf(System.getProperty("platform-type"));
             }
-            File sdkRoot = downloadRetriever.retrieve(SdkType.AIR, version, 
platformType);
+            File sdkRoot = downloadRetriever.retrieve(SdkType.AIR, version, 
platformType, proxySettings);
             AirConverter converter = new AirConverter(sdkRoot, 
localRepoBaseDir);
             converter.convert();
         } catch (Throwable ce) {
@@ -244,4 +251,16 @@ public class FlexEventSpy extends AbstractEventSpy {
         flexSplashScreenShown = true;
     }
 
+    protected ProxySettings getProxySettings() {
+        org.apache.maven.settings.Proxy settingsProxy = 
mavenSession.getSettings().getActiveProxy();
+        String protocol = settingsProxy.getProtocol();
+        String host = settingsProxy.getHost();
+        int port = settingsProxy.getPort();
+        String nonProxyHost = settingsProxy.getNonProxyHosts();
+        String username = settingsProxy.getUsername();
+        String password = settingsProxy.getPassword();
+
+        return new ProxySettings(protocol, host, port, nonProxyHost, username, 
password);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/Retriever.java
----------------------------------------------------------------------
diff --git 
a/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/Retriever.java
 
b/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/Retriever.java
index ee863e3..c020378 100644
--- 
a/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/Retriever.java
+++ 
b/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/Retriever.java
@@ -18,6 +18,7 @@ package org.apache.flex.utilities.converter.retrievers;
 
 import 
org.apache.flex.utilities.converter.retrievers.exceptions.RetrieverException;
 import org.apache.flex.utilities.converter.retrievers.types.PlatformType;
+import org.apache.flex.utilities.converter.retrievers.model.ProxySettings;
 import org.apache.flex.utilities.converter.retrievers.types.SdkType;
 
 import java.io.File;
@@ -29,4 +30,7 @@ public interface Retriever {
 
     File retrieve(SdkType sdkType, String version, PlatformType platformType) 
throws RetrieverException;
 
+    File retrieve(SdkType sdkType, String version, PlatformType platformType, 
ProxySettings proxySettings)
+            throws RetrieverException;
+
 }

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/model/ProxySettings.java
----------------------------------------------------------------------
diff --git 
a/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/model/ProxySettings.java
 
b/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/model/ProxySettings.java
new file mode 100644
index 0000000..22661a4
--- /dev/null
+++ 
b/flex-maven-tools/mavenizer/retrievers/base/src/main/java/org/apache/flex/utilities/converter/retrievers/model/ProxySettings.java
@@ -0,0 +1,48 @@
+package org.apache.flex.utilities.converter.retrievers.model;
+
+/**
+ * Created by christoferdutz on 01.07.15.
+ */
+public class ProxySettings {
+
+    private String protocol;
+    private String host;
+    private int port;
+    private String nonProxyHost;
+    private String username;
+    private String password;
+
+    public ProxySettings(String protocol, String host, int port, String 
nonProxyHost, String username, String password) {
+        this.protocol = protocol;
+        this.host = host;
+        this.port = port;
+        this.nonProxyHost = nonProxyHost;
+        this.username = username;
+        this.password = password;
+    }
+
+    public String getProtocol() {
+        return protocol;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public String getNonProxyHost() {
+        return nonProxyHost;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/retrievers/download/pom.xml
----------------------------------------------------------------------
diff --git a/flex-maven-tools/mavenizer/retrievers/download/pom.xml 
b/flex-maven-tools/mavenizer/retrievers/download/pom.xml
index 6ba5e08..ffa2665 100644
--- a/flex-maven-tools/mavenizer/retrievers/download/pom.xml
+++ b/flex-maven-tools/mavenizer/retrievers/download/pom.xml
@@ -40,8 +40,22 @@
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
-            <version>4.2.3</version>
+            <version>4.5</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.mock-server</groupId>
+            <artifactId>mockserver-netty</artifactId>
+            <version>RELEASE</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <version>6.8.8</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.maven</groupId>
             <artifactId>maven-artifact</artifactId>

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
----------------------------------------------------------------------
diff --git 
a/flex-maven-tools/mavenizer/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
 
b/flex-maven-tools/mavenizer/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
index a6b0a1a..f086221 100644
--- 
a/flex-maven-tools/mavenizer/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
+++ 
b/flex-maven-tools/mavenizer/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java
@@ -20,14 +20,17 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.flex.utilities.converter.retrievers.BaseRetriever;
 import 
org.apache.flex.utilities.converter.retrievers.exceptions.RetrieverException;
+import org.apache.flex.utilities.converter.retrievers.model.ProxySettings;
 import org.apache.flex.utilities.converter.retrievers.types.PlatformType;
 import org.apache.flex.utilities.converter.retrievers.types.SdkType;
 import org.apache.flex.utilities.converter.retrievers.utils.ProgressBar;
 import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -74,6 +77,11 @@ public class DownloadRetriever extends BaseRetriever {
     }
 
     public File retrieve(SdkType type, String version, PlatformType 
platformType) throws RetrieverException {
+        return retrieve(type, version, platformType, null);
+    }
+
+    public File retrieve(SdkType type, String version, PlatformType 
platformType, ProxySettings proxySettings)
+            throws RetrieverException {
         try {
             if (type.equals(SdkType.FLASH) || type.equals(SdkType.AIR) || 
type.equals(SdkType.FONTKIT)) {
                 confirmLicenseAcceptance(type);
@@ -96,19 +104,19 @@ public class DownloadRetriever extends BaseRetriever {
 
                 final URI afeUri = new 
URI("http://sourceforge.net/adobe/flexsdk/code/HEAD/tree/trunk/lib/afe.jar?format=raw";);
                 final File afeFile = new File(targetDir, "afe.jar");
-                performSafeDownload(afeUri, afeFile);
+                performSafeDownload(afeUri, afeFile, proxySettings);
 
                 final URI aglj40Uri = new 
URI("http://sourceforge.net/adobe/flexsdk/code/HEAD/tree/trunk/lib/aglj40.jar?format=raw";);
                 final File aglj40File = new File(targetDir, "aglj40.jar");
-                performSafeDownload(aglj40Uri, aglj40File);
+                performSafeDownload(aglj40Uri, aglj40File, proxySettings);
 
                 final URI rideauUri = new 
URI("http://sourceforge.net/adobe/flexsdk/code/HEAD/tree/trunk/lib/rideau.jar?format=raw";);
                 final File rideauFile = new File(targetDir, "rideau.jar");
-                performSafeDownload(rideauUri, rideauFile);
+                performSafeDownload(rideauUri, rideauFile, proxySettings);
 
                 final URI flexFontkitUri = new 
URI("http://sourceforge.net/adobe/flexsdk/code/HEAD/tree/trunk/lib/flex-fontkit.jar?format=raw";);
                 final File flexFontkitFile = new File(targetDir, 
"flex-fontkit.jar");
-                performSafeDownload(flexFontkitUri, flexFontkitFile);
+                performSafeDownload(flexFontkitUri, flexFontkitFile, 
proxySettings);
 
                 return targetRootDir;
             } else {
@@ -116,7 +124,7 @@ public class DownloadRetriever extends BaseRetriever {
                 final File targetFile = File.createTempFile(type.toString() + 
"-" + version +
                                 ((platformType != null) ? "-" + platformType : 
"") + "-",
                         
sourceUrl.getFile().substring(sourceUrl.getFile().lastIndexOf(".")));
-                performFastDownload(sourceUrl, targetFile);
+                performFastDownload(sourceUrl, targetFile, proxySettings);
 
                 
////////////////////////////////////////////////////////////////////////////////
                 // Do the extracting.
@@ -125,9 +133,12 @@ public class DownloadRetriever extends BaseRetriever {
                 if (type.equals(SdkType.FLASH)) {
                     final File targetDirectory = new 
File(targetFile.getParent(),
                             targetFile.getName().substring(0, 
targetFile.getName().lastIndexOf(".") - 1));
-                    final File libDestFile = new File(targetDirectory, 
"frameworks/libs/player/" + version + "/playerglobal.swc");
+                    final File libDestFile = new File(targetDirectory, 
"frameworks/libs/player/" + version +
+                            "/playerglobal.swc");
                     if (!libDestFile.getParentFile().exists()) {
-                        libDestFile.getParentFile().mkdirs();
+                        if(!libDestFile.getParentFile().mkdirs()) {
+                            throw new RetrieverException("Error creating 
directory " + libDestFile.getParent());
+                        }
                     }
                     FileUtils.moveFile(targetFile, libDestFile);
                     return targetDirectory;
@@ -170,8 +181,18 @@ public class DownloadRetriever extends BaseRetriever {
         }
     }
 
-    protected void performFastDownload(URL sourceUrl, File targetFile) throws 
IOException {
-        final URLConnection connection = sourceUrl.openConnection();
+    protected void performFastDownload(URL sourceUrl, File targetFile, 
ProxySettings proxySettings) throws IOException {
+        URLConnection connection;
+        if(proxySettings != null) {
+            SocketAddress socketAddress = new 
InetSocketAddress(proxySettings.getHost(), proxySettings.getPort());
+            Proxy proxy = new 
Proxy(Proxy.Type.valueOf(proxySettings.getProtocol()), socketAddress);
+            connection = sourceUrl.openConnection(proxy);
+            String encoded = new String
+                    
(Base64.getEncoder().encode("username:password".getBytes()));
+            connection.setRequestProperty("Proxy-Authorization", "Basic " + 
encoded);
+        } else {
+            connection = sourceUrl.openConnection();
+        }
         final ReadableByteChannel rbc = 
Channels.newChannel(connection.getInputStream());
         final FileOutputStream fos = new FileOutputStream(targetFile);
 
@@ -200,9 +221,18 @@ public class DownloadRetriever extends BaseRetriever {
         
System.out.println("===========================================================");
     }
 
-    protected void performSafeDownload(URI sourceUri, File targetFile) throws 
IOException {
+    protected void performSafeDownload(URI sourceUri, File targetFile, 
ProxySettings proxySettings) throws IOException {
+        RequestConfig config;
+        if(proxySettings != null) {
+            HttpHost proxy = new HttpHost(proxySettings.getHost(), 
proxySettings.getPort());
+            config = RequestConfig.custom().setProxy(proxy).build();
+        } else {
+            config = RequestConfig.DEFAULT;
+        }
+
         HttpGet httpget = new HttpGet(sourceUri);
-        HttpClient httpclient = new DefaultHttpClient();
+        httpget.setConfig(config);
+        HttpClient httpclient = HttpClients.createDefault();
         HttpResponse response = httpclient.execute(httpget);
 
         String reasonPhrase = response.getStatusLine().getReasonPhrase();

http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/a84cdf1d/flex-maven-tools/mavenizer/retrievers/download/src/test/java/org/apache/flex/utilities/converter/retrievers/download/ProxyTest.java
----------------------------------------------------------------------
diff --git 
a/flex-maven-tools/mavenizer/retrievers/download/src/test/java/org/apache/flex/utilities/converter/retrievers/download/ProxyTest.java
 
b/flex-maven-tools/mavenizer/retrievers/download/src/test/java/org/apache/flex/utilities/converter/retrievers/download/ProxyTest.java
new file mode 100644
index 0000000..e186e53
--- /dev/null
+++ 
b/flex-maven-tools/mavenizer/retrievers/download/src/test/java/org/apache/flex/utilities/converter/retrievers/download/ProxyTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+package org.apache.flex.utilities.converter.retrievers.download;
+
+import org.apache.flex.utilities.converter.retrievers.model.ProxySettings;
+import org.apache.flex.utilities.converter.retrievers.types.PlatformType;
+import org.apache.flex.utilities.converter.retrievers.types.SdkType;
+import org.mockserver.integration.ClientAndProxy;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import static org.mockserver.integration.ClientAndProxy.startClientAndProxy;
+
+/**
+ * Created by christoferdutz on 02.07.15.
+ */
+public class ProxyTest {
+
+    private ClientAndProxy proxy;
+
+    @BeforeMethod
+    public void startProxy() {
+        proxy = startClientAndProxy(3456);
+        // Make the test accept the license agreement.
+        
System.setProperty("com.adobe.systemIdsForWhichTheTermsOfTheAdobeLicenseAgreementAreAccepted",
 SystemIdHelper.getSystemId());
+    }
+
+    @AfterMethod
+    public void stopProxy() {
+        proxy.stop();
+    }
+
+    /**
+     * Does a download using the URLConnection class
+     */
+    @Test
+    public void simpleFastHttpNoAuthProxy() throws Exception {
+        ProxySettings proxySettings = new ProxySettings("HTTP", "localhost", 
3456, null, null, null);
+        DownloadRetriever downloadRetriever = new DownloadRetriever();
+        downloadRetriever.retrieve(SdkType.FLASH, "17.0", 
PlatformType.WINDOWS, proxySettings);
+    }
+
+    /**
+     * Does a download using the HttpClient class
+     */
+    @Test
+    public void simpleSafeHttpNoAuthProxy() throws Exception {
+        ProxySettings proxySettings = new ProxySettings("HTTP", "localhost", 
3456, null, null, null);
+        DownloadRetriever downloadRetriever = new DownloadRetriever();
+        downloadRetriever.retrieve(SdkType.FONTKIT, "1.0", 
PlatformType.WINDOWS, proxySettings);
+    }
+
+    /**
+     * Does a download using the URLConnection class using a proxy that 
requires authentication.
+     */
+    @Test(enabled = false)
+    public void simpleFastHttpWithAuthProxy() throws Exception {
+        ProxySettings proxySettings = new ProxySettings("HTTP", "localhost", 
3456, "testuser", "testpass", null);
+        DownloadRetriever downloadRetriever = new DownloadRetriever();
+        downloadRetriever.retrieve(SdkType.FLASH, "17.0", 
PlatformType.WINDOWS, proxySettings);
+    }
+
+    /**
+     * Does a download using the HttpClient class using a proxy that requires 
authentication.
+     */
+    @Test(enabled = false)
+    public void simpleSafeHttpWithAuthProxy() throws Exception {
+        ProxySettings proxySettings = new ProxySettings("HTTP", "localhost", 
3456, "testuser", "testpass", null);
+        DownloadRetriever downloadRetriever = new DownloadRetriever();
+        downloadRetriever.retrieve(SdkType.FONTKIT, "1.0", 
PlatformType.WINDOWS, proxySettings);
+    }
+
+}

Reply via email to