karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggrzy...@apache.org
Subject [5/9] karaf git commit: [KARAF-5008] maven:http-proxy and maven:http-proxy-list commands
Date Thu, 22 Jun 2017 11:15:36 GMT
[KARAF-5008] maven:http-proxy and maven:http-proxy-list commands


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/6a676630
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/6a676630
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/6a676630

Branch: refs/heads/master
Commit: 6a6766303030ac9437ba3920d98a4abea1edb605
Parents: 32d5c7b
Author: Grzegorz Grzybek <gr.grzybek@gmail.com>
Authored: Wed Jun 21 15:26:45 2017 +0200
Committer: Grzegorz Grzybek <gr.grzybek@gmail.com>
Committed: Wed Jun 21 15:31:54 2017 +0200

----------------------------------------------------------------------
 .../karaf/maven/command/HttpProxyCommand.java   | 126 ++++++++++++++-----
 .../maven/command/HttpProxyListCommand.java     |  70 +++++++++++
 .../src/test/resources/reference-settings.xml   |   1 +
 3 files changed, 168 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/6a676630/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java
----------------------------------------------------------------------
diff --git a/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java
b/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java
index 129aca5..d7a3bfa 100644
--- a/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java
+++ b/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java
@@ -17,13 +17,17 @@
 package org.apache.karaf.maven.command;
 
 import java.util.Dictionary;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
+import org.apache.karaf.shell.api.action.Argument;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
-import org.apache.karaf.shell.support.table.Row;
-import org.apache.karaf.shell.support.table.ShellTable;
 import org.apache.maven.settings.Proxy;
+import org.osgi.service.cm.Configuration;
 
 @Command(scope = "maven", name = "http-proxy", description = "Manage HTTP proxy configuration
for Maven remote repositories")
 @Service
@@ -32,53 +36,117 @@ public class HttpProxyCommand extends MavenSecuritySupport {
     @Option(name = "--add", description = "Adds HTTP proxy configuration to Maven settings",
required = false, multiValued = false)
     boolean add;
 
+    @Option(name = "--change", description = "Changes HTTP proxy configuration in Maven settings",
required = false, multiValued = false)
+    boolean change;
+
     @Option(name = "--remove", description = "Removes HTTP proxy configuration from Maven
settings", required = false, multiValued = false)
     boolean remove;
 
+    @Option(name = "-id", description = "Identifier of HTTP proxy", required = true, multiValued
= false)
+    String id;
+
+    @Option(name = "-f", aliases = { "--force" }, description = "Do not ask for confirmation",
required = false, multiValued = false)
+    boolean force = false;
+
+    @Option(name = "-u", aliases = { "--username" }, description = "Username for remote repository",
required = false, multiValued = false)
+    String username;
+
+    @Option(name = "-p", aliases = { "--password" }, description = "Password for remote repository
(may be encrypted, see \"maven:password -ep\")", required = false, multiValued = false)
+    String password;
+
+    @Option(name = "-n", aliases = { "--non-proxy-hosts" }, description = "Non-proxied hosts
(in the format '192.168.*|localhost|...')", required = false, multiValued = false)
+    String nonProxyHosts;
+
+    @Argument(description = "host:port of HTTP proxy", required = false, multiValued = false)
+    String hostPort;
+
     @Override
     public void doAction(String prefix, Dictionary<String, Object> config) throws Exception
{
-        if (add && remove) {
-            System.err.println("Please specify only one of --add and --remove");
+        if (add && (change || remove) || change && remove) {
+            System.err.println("Please specify only one of --add/--change/--remove");
             return;
         }
 
+        if (id == null || "".equals(id.trim())) {
+            System.err.println("Please specify ID of HTTP proxy");
+            return;
+        }
+
+        if (mavenSettings.getProxies() == null) {
+            mavenSettings.setProxies(new LinkedList<>());
+        }
+        Optional<Proxy> existingProxy = mavenSettings.getProxies().stream()
+                .filter((p) -> id.equals(p.getId())).findAny();
+
         if (add) {
-            // add
+            if (hostPort == null || "".equals(hostPort.trim())) {
+                System.err.println("Please specify host:port of new HTTP proxy");
+                return;
+            }
+            if (existingProxy.isPresent()) {
+                System.err.printf("HTTP proxy with ID \"%s\" is already configured\n", id);
+                return;
+            }
+        } else if (!existingProxy.isPresent()) {
+            System.err.printf("Can't find HTTP proxy with ID \"%s\"\n", id);
+            return;
         }
 
-        if (remove) {
+        boolean hasUsername = username != null && !"".equals(username.trim());
+        boolean hasPassword = password != null && !"".equals(password.trim());
+        boolean hasCredentials = hasUsername && hasPassword;
+
+        if ((hasUsername && !hasPassword) || (!hasUsername && hasPassword))
{
+            System.err.println("Please specify both username and password");
+            return;
+        }
+
+        Proxy proxy = null;
+        if (add) {
+            proxy = new Proxy();
+            proxy.setId(id);
+            mavenSettings.getProxies().add(proxy);
+        } else if (change) {
+            proxy = existingProxy.get(); // should be there
+        } else /*if (remove)*/ {
             // remove
+            List<Proxy> newProxies = mavenSettings.getProxies().stream()
+                    .filter((p) -> !id.equals(p.getId())).collect(Collectors.toList());
+            mavenSettings.setProxies(newProxies);
         }
 
-        // list (also after --add or --remove)
-        System.out.println();
-        if (mavenSettings != null && mavenSettings.getProxies() != null &&
mavenSettings.getProxies().size() > 0) {
-            ShellTable table = new ShellTable();
-            table.column("ID");
-            table.column("Host");
-            table.column("Port");
-            table.column("Username");
-            if (showPasswords) {
-                table.column("Password");
+        if (add || change) {
+            proxy.setActive(true);
+            proxy.setProtocol("http");
+            if (nonProxyHosts != null && !"".equals(nonProxyHosts.trim())) {
+                proxy.setNonProxyHosts(nonProxyHosts);
             }
-            for (Proxy proxy : mavenSettings.getProxies()) {
-                Row row = table.addRow();
-                row.addContent(proxy.getId(), proxy.getHost(), proxy.getPort());
-                row.addContent(proxy.getUsername() != null ? proxy.getUsername() : "");
-                if (showPasswords) {
-                    addPasswordInfo(row, proxyPasswords, proxy.getId(), proxy.getPassword());
+            if (hostPort != null && !"".equals(hostPort.trim())) {
+                if (hostPort.contains(":")) {
+                    proxy.setHost(hostPort.substring(0, hostPort.indexOf(':')));
+                    proxy.setPort(Integer.parseInt(hostPort.substring(hostPort.indexOf(':')
+ 1)));
+                } else {
+                    proxy.setHost(hostPort);
+                    proxy.setPort(3128);
                 }
             }
-            table.print(System.out);
-        } else {
-            System.out.print("No HTTP proxies configured");
-            if (settings != null && settings.value != null) {
-                System.out.print(" in " + settings.value);
+            if (hasCredentials) {
+                proxy.setUsername(username);
+                proxy.setPassword(password);
             }
-            System.out.println();
         }
 
-        System.out.println();
+        updateSettings(prefix, config);
+
+        Configuration cmConfig = cm.getConfiguration(PID);
+        cmConfig.update(config);
+
+        // list (also after --add or --remove)
+        if (showPasswords) {
+            session.execute("maven:http-proxy-list -x");
+        } else {
+            session.execute("maven:http-proxy-list");
+        }
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/6a676630/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyListCommand.java
----------------------------------------------------------------------
diff --git a/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyListCommand.java
b/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyListCommand.java
new file mode 100644
index 0000000..799b0da
--- /dev/null
+++ b/maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyListCommand.java
@@ -0,0 +1,70 @@
+/*
+ * 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.karaf.maven.command;
+
+import java.util.Dictionary;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.support.table.Row;
+import org.apache.karaf.shell.support.table.ShellTable;
+import org.apache.maven.settings.Proxy;
+
+@Command(scope = "maven", name = "http-proxy-list", description = "Lists HTTP proxy configurations
for Maven remote repositories")
+@Service
+public class HttpProxyListCommand extends MavenSecuritySupport {
+
+    @Override
+    public void doAction(String prefix, Dictionary<String, Object> config) throws Exception
{
+        System.out.println();
+        if (mavenSettings != null && mavenSettings.getProxies() != null &&
mavenSettings.getProxies().size() > 0) {
+            ShellTable table = new ShellTable();
+            table.column("ID");
+            table.column("Host");
+            table.column("Port");
+            table.column("Non-proxy hosts");
+            table.column("Username");
+            if (showPasswords) {
+                table.column("Password");
+            }
+            for (Proxy _p : mavenSettings.getProxies()) {
+                Row row = table.addRow();
+                row.addContent(_p.getId(), _p.getHost(), _p.getPort(), _p.getNonProxyHosts());
+                row.addContent(_p.getUsername() != null ? _p.getUsername() : "");
+                if (showPasswords) {
+                    addPasswordInfo(row, proxyPasswords, _p.getId(), _p.getPassword());
+                }
+            }
+            table.print(System.out);
+        } else {
+            System.out.print("No HTTP proxies configured");
+            if (settings != null && settings.value != null) {
+                System.out.print(" in " + settings.value);
+            }
+            System.out.println();
+        }
+
+        System.out.println();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/6a676630/maven/core/src/test/resources/reference-settings.xml
----------------------------------------------------------------------
diff --git a/maven/core/src/test/resources/reference-settings.xml b/maven/core/src/test/resources/reference-settings.xml
index 25b58fb..979ab11 100644
--- a/maven/core/src/test/resources/reference-settings.xml
+++ b/maven/core/src/test/resources/reference-settings.xml
@@ -29,6 +29,7 @@
             <protocol>http</protocol>
             <username>username</username>
             <password>password</password>
+            <nonProxyHosts></nonProxyHosts>
         </proxy>
     </proxies>
 


Mime
View raw message