in maven/core/src/main/java/org/apache/karaf/maven/command/HttpProxyCommand.java [64:150]
public void doAction(String prefix, Dictionary<String, Object> config) throws Exception {
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) {
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;
}
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);
}
if (add || change) {
proxy.setActive(true);
proxy.setProtocol("http");
if (nonProxyHosts != null && !"".equals(nonProxyHosts.trim())) {
proxy.setNonProxyHosts(nonProxyHosts);
}
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);
}
}
if (hasCredentials) {
proxy.setUsername(username);
proxy.setPassword(password);
}
}
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");
}
}