Skip to content

Commit

Permalink
Implemented support for proxies (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed May 5, 2015
1 parent 0a39640 commit 406c161
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.lang.reflect.Constructor;
import java.net.Proxy;
import java.util.List;

import com.redhat.ceylon.cmr.spi.ContentTransformer;
Expand All @@ -39,17 +40,17 @@ protected RepositoryManagerBuilder() {
}

public RepositoryManagerBuilder(Logger log) {
this(log, true, 0);
this(log, true, 0, Proxy.NO_PROXY);
}

public RepositoryManagerBuilder(Logger log, boolean offline, int timeout) {
this(log, offline, timeout, null);
public RepositoryManagerBuilder(Logger log, boolean offline, int timeout, Proxy proxy) {
this(log, offline, timeout, proxy, null);
}

public RepositoryManagerBuilder(Logger log, boolean offline, int timeout, Overrides overrides) {
public RepositoryManagerBuilder(Logger log, boolean offline, int timeout, Proxy proxy, Overrides overrides) {
try {
Constructor<? extends RepositoryManagerBuilder> ctor = getDelegateClass().getConstructor(Logger.class, boolean.class, int.class, Overrides.class);
delegate = ctor.newInstance(log, offline, timeout, overrides);
Constructor<? extends RepositoryManagerBuilder> ctor = getDelegateClass().getConstructor(Logger.class, boolean.class, int.class, Proxy.class, Overrides.class);
delegate = ctor.newInstance(log, offline, timeout, proxy, overrides);
} catch (java.lang.reflect.InvocationTargetException e) {
Throwable tex = e.getTargetException();
if (tex instanceof RuntimeException) {
Expand All @@ -64,14 +65,14 @@ public RepositoryManagerBuilder(Logger log, boolean offline, int timeout, Overri
}
}

public RepositoryManagerBuilder(File mainRepository, Logger log, boolean offline, int timeout) {
this(mainRepository, log, offline, timeout, null);
public RepositoryManagerBuilder(File mainRepository, Logger log, boolean offline, int timeout, Proxy proxy) {
this(mainRepository, log, offline, timeout, proxy, null);
}

public RepositoryManagerBuilder(File mainRepository, Logger log, boolean offline, int timeout, Overrides overrides) {
public RepositoryManagerBuilder(File mainRepository, Logger log, boolean offline, int timeout, Proxy proxy, Overrides overrides) {
try {
Constructor<? extends RepositoryManagerBuilder> ctor = getDelegateClass().getConstructor(File.class, Logger.class, boolean.class, int.class, Overrides.class);
delegate = ctor.newInstance(mainRepository, log, offline, timeout, overrides);
Constructor<? extends RepositoryManagerBuilder> ctor = getDelegateClass().getConstructor(File.class, Logger.class, boolean.class, int.class, Proxy.class, Overrides.class);
delegate = ctor.newInstance(mainRepository, log, offline, timeout, proxy, overrides);
} catch (java.lang.reflect.InvocationTargetException e) {
Throwable tex = e.getTargetException();
if (tex instanceof RuntimeException) {
Expand Down
19 changes: 17 additions & 2 deletions ceylon/src/main/java/com/redhat/ceylon/cmr/ceylon/CeylonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -46,6 +47,7 @@ public static class CeylonRepoManagerBuilder {
private String user;
private String password;
private int timeout = -1;
private Proxy proxy;
private boolean offline;
private boolean noSystemRepo;
private boolean noDefRepos;
Expand Down Expand Up @@ -261,6 +263,15 @@ public CeylonRepoManagerBuilder timeout(int timeout){
return this;
}

/**
* Sets the proxy for any connections that might be established
* to remote repositories (defaults to no proxy)
*/
public CeylonRepoManagerBuilder proxy(Proxy proxy){
this.proxy = proxy;
return this;
}

/**
* Enables offline mode that will prevent the module loader from connecting
* to remote repositories (defaults to false)
Expand Down Expand Up @@ -345,7 +356,7 @@ public RepositoryManagerBuilder buildManagerBuilder() {
skipRemoteRepositories = true;
}

final RepositoryManagerBuilder builder = new RepositoryManagerBuilder(root, log, isOffline(config), getTimeout(config), getOverrides(config));
final RepositoryManagerBuilder builder = new RepositoryManagerBuilder(root, log, isOffline(config), getTimeout(config), getProxy(config), getOverrides(config));

// Now we add all the rest of the repositories in the order that they will be searched

Expand Down Expand Up @@ -515,7 +526,7 @@ public RepositoryManager buildOutputManager() {
File cachingDir = FileUtil.makeTempDir("ceylon-webdav-cache-");

// HTTP
WebDAVContentStore davContentStore = new WebDAVContentStore(outRepo, log, false, getTimeout(config));
WebDAVContentStore davContentStore = new WebDAVContentStore(outRepo, log, false, getTimeout(config), getProxy(config));
davContentStore.setUsername(user);
davContentStore.setPassword(password);

Expand Down Expand Up @@ -597,6 +608,10 @@ private boolean isOffline(CeylonConfig config) {
private int getTimeout(CeylonConfig config) {
return (timeout >= 0) ? timeout : (int)DefaultToolOptions.getDefaultTimeout(config);
}

private Proxy getProxy(CeylonConfig config) {
return (proxy != null) ? proxy : DefaultToolOptions.getDefaultProxy(config);
}

protected Overrides getOverrides(CeylonConfig config) {
String path = (overrides != null) ? overrides : DefaultToolOptions.getDefaultOverrides(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.redhat.ceylon.cmr.spi.StructureBuilder;

import java.io.File;
import java.net.Proxy;

/**
* Maven repository helper.
Expand Down Expand Up @@ -53,8 +54,8 @@ public static Repository getMavenRepository(File mvnRepository) {
return new MavenRepository(new MavenContentStore(mvnRepository).createRoot());
}

public static Repository getMavenRepository(String repositoryURL, Logger log, boolean offline, int timeout) {
return new MavenRepository(new RemoteContentStore(repositoryURL, log, offline, timeout).createRoot());
public static Repository getMavenRepository(String repositoryURL, Logger log, boolean offline, int timeout, Proxy proxy) {
return new MavenRepository(new RemoteContentStore(repositoryURL, log, offline, timeout, proxy).createRoot());
}

public static Repository getMavenRepository(StructureBuilder structureBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
Expand All @@ -40,13 +41,13 @@
*/
public class RemoteContentStore extends URLContentStore {

public RemoteContentStore(String root, Logger log, boolean offline, int timeout) {
super(root, log, offline, timeout);
public RemoteContentStore(String root, Logger log, boolean offline, int timeout, Proxy proxy) {
super(root, log, offline, timeout, proxy);
}

protected InputStream openStream(final URL url) throws IOException {
if (connectionAllowed()) {
final URLConnection conn = url.openConnection();
final URLConnection conn = url.openConnection(proxy);
if (conn instanceof HttpURLConnection) {
HttpURLConnection huc = (HttpURLConnection) conn;
huc.setConnectTimeout(timeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.lang.reflect.Method;
import java.net.Proxy;
import java.net.URI;

import com.redhat.ceylon.cmr.api.Repository;
Expand All @@ -34,13 +35,15 @@
class RepositoryBuilderImpl implements RepositoryBuilder {

private Logger log;
private int timeout;
private boolean offline;
private int timeout;
private Proxy proxy;

RepositoryBuilderImpl(Logger log, boolean offline, int timeout) {
RepositoryBuilderImpl(Logger log, boolean offline, int timeout, Proxy proxy) {
this.log = log;
this.timeout = timeout;
this.offline = offline;
this.timeout = timeout;
this.proxy = proxy;
}

public Repository buildRepository(String token) throws Exception {
Expand All @@ -54,7 +57,7 @@ public Repository buildRepository(String token) throws Exception {

StructureBuilder structureBuilder;
if (token.startsWith("http:") || token.startsWith("https:")) {
structureBuilder = new RemoteContentStore(token, log, offline, timeout);
structureBuilder = new RemoteContentStore(token, log, offline, timeout, proxy);
} else if (token.equals("jdk") || token.equals("jdk:")) {
return new JDKRepository();
} else if (token.equals("aether") || token.equals("aether:") || token.equals("mvn") || token.equals("mvn:")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.redhat.ceylon.cmr.impl;

import java.io.File;
import java.net.Proxy;
import java.util.List;

import com.redhat.ceylon.common.log.Logger;
Expand All @@ -37,24 +38,27 @@
*/
public class RepositoryManagerBuilderImpl extends RepositoryManagerBuilder {

private RootRepositoryManager repository;
private Logger log;
private int timeout;
private boolean offline;
private final RootRepositoryManager repository;
private final Logger log;
private final boolean offline;
private final int timeout;
private final Proxy proxy;

public RepositoryManagerBuilderImpl(Logger log, boolean offline, int timeout, Overrides overrides) {
public RepositoryManagerBuilderImpl(Logger log, boolean offline, int timeout, Proxy proxy, Overrides overrides) {
repository = new RootRepositoryManager(log, overrides);
this.log = log;
this.timeout = timeout;
this.offline = offline;
this.timeout = timeout;
this.proxy = proxy;
init();
}

public RepositoryManagerBuilderImpl(File mainRepository, Logger log, boolean offline, int timeout, Overrides overrides) {
public RepositoryManagerBuilderImpl(File mainRepository, Logger log, boolean offline, int timeout, Proxy proxy, Overrides overrides) {
repository = new RootRepositoryManager(mainRepository, log, overrides);
this.log = log;
this.timeout = timeout;
this.offline = offline;
this.timeout = timeout;
this.proxy = proxy;
init();
}

Expand All @@ -69,7 +73,7 @@ private OpenNode getCache() {

@Override
public RepositoryBuilder repositoryBuilder() {
return new RepositoryBuilderImpl(log, offline, timeout);
return new RepositoryBuilderImpl(log, offline, timeout, proxy);
}

public RepositoryManagerBuilderImpl mergeStrategy(MergeStrategy strategy) {
Expand Down
17 changes: 11 additions & 6 deletions impl/src/main/java/com/redhat/ceylon/cmr/impl/URLContentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
Expand Down Expand Up @@ -70,24 +71,28 @@ public abstract class URLContentStore extends AbstractRemoteContentStore {
private static final String HERD_ORIGIN = "The Herd";

protected final String root;
protected final Proxy proxy;
private final String herdRequestedApi;

protected String username;
protected String password;
private Boolean _isHerd = null;
private Boolean _isLocalMachine = null;
private String herdCompleteModulesURL;
private String herdCompleteVersionsURL;
private String herdSearchModulesURL;
private String herdRequestedApi;
private int herdVersion = 1; // assume 1 until we find otherwise

protected URLContentStore(String root, Logger log, boolean offline, int timeout) {
this(root, log, offline, timeout, null);
protected URLContentStore(String root, Logger log, boolean offline, int timeout, Proxy proxy) {
this(root, log, offline, timeout, proxy, null);
}
protected URLContentStore(String root, Logger log, boolean offline, int timeout, String apiVersion) {

protected URLContentStore(String root, Logger log, boolean offline, int timeout, Proxy proxy, String apiVersion) {
super(log, offline, timeout);
if (root == null)
throw new IllegalArgumentException("Null root url");
this.root = root;
this.proxy = proxy;
this.herdRequestedApi = apiVersion != null ? apiVersion : "4";
if(apiVersion != null
&& !apiVersion.equals("1")
Expand Down Expand Up @@ -117,7 +122,7 @@ private boolean testHerd() {
try{
// we support both API 1 to 3
URL rootURL = getURL("?version="+herdRequestedApi);
HttpURLConnection con = (HttpURLConnection) rootURL.openConnection();
HttpURLConnection con = (HttpURLConnection) rootURL.openConnection(proxy);
try{
con.setConnectTimeout(timeout);
con.setReadTimeout(timeout * Constants.READ_TIMEOUT_MULTIPLIER);
Expand Down Expand Up @@ -295,7 +300,7 @@ protected long size(final URL url) throws IOException {

protected HttpURLConnection head(final URL url) throws IOException {
if (connectionAllowed()) {
final URLConnection conn = url.openConnection();
final URLConnection conn = url.openConnection(proxy);
if (conn instanceof HttpURLConnection) {
HttpURLConnection huc = (HttpURLConnection) conn;
huc.setConnectTimeout(timeout);
Expand Down
10 changes: 9 additions & 1 deletion impl/src/main/java/com/redhat/ceylon/cmr/util/WS.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,17 @@ public static void getXML(String url, List<Param> params, XMLHandler handler){
public static void getXML(String url, XMLHandler handler){
try{
URL endpoint = new URL(url);
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection(DefaultToolOptions.getDefaultProxy());
connection.setConnectTimeout((int) DefaultToolOptions.getDefaultTimeout());
connection.setReadTimeout((int) DefaultToolOptions.getDefaultTimeout() * Constants.READ_TIMEOUT_MULTIPLIER);
getXML(connection, handler);
}catch(IOException x){
throw new RuntimeException(x);
}
}

public static void getXML(HttpURLConnection connection, XMLHandler handler){
try{
connection.addRequestProperty("Accept", "application/xml");
connection.connect();
try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileVisitResult;
Expand Down Expand Up @@ -116,35 +117,35 @@ protected File getFolders() throws URISyntaxException {
return new File(url.toURI());
}

protected RepositoryManagerBuilder getRepositoryManagerBuilder(boolean offline, int timeout) throws Exception {
return getRepositoryManagerBuilder(getRepositoryRoot(), offline, timeout);
protected RepositoryManagerBuilder getRepositoryManagerBuilder(boolean offline, int timeout, Proxy proxy) throws Exception {
return getRepositoryManagerBuilder(getRepositoryRoot(), offline, timeout, proxy);
}

protected RepositoryManagerBuilder getRepositoryManagerBuilder(File root, boolean offline, int timeout) throws Exception {
return getRepositoryManagerBuilder(root, offline, timeout, null);
protected RepositoryManagerBuilder getRepositoryManagerBuilder(File root, boolean offline, int timeout, Proxy proxy) throws Exception {
return getRepositoryManagerBuilder(root, offline, timeout, proxy, null);
}

protected RepositoryManagerBuilder getRepositoryManagerBuilder(File root, boolean offline, int timeout, String overrideFileName) throws Exception {
RepositoryManagerBuilder builder = new RepositoryManagerBuilder(temp.toFile(), log, offline, timeout, RepositoryManagerBuilder.parseOverrides(overrideFileName));
protected RepositoryManagerBuilder getRepositoryManagerBuilder(File root, boolean offline, int timeout, Proxy proxy, String overrideFileName) throws Exception {
RepositoryManagerBuilder builder = new RepositoryManagerBuilder(temp.toFile(), log, offline, timeout, proxy, RepositoryManagerBuilder.parseOverrides(overrideFileName));
builder.addRepository(new DefaultRepository(new FileContentStore(root).createRoot()));
return builder;
}

protected RepositoryManager getRepositoryManager(boolean offline, int timeout) throws Exception {
protected RepositoryManager getRepositoryManager(boolean offline, int timeout, Proxy proxy) throws Exception {
return getRepositoryManager(offline, timeout, null);
}

protected RepositoryManager getRepositoryManager(boolean offline, int timeout, String overrideFileName) throws Exception {
RepositoryManagerBuilder builder = getRepositoryManagerBuilder(getRepositoryRoot(), offline, timeout, overrideFileName);
protected RepositoryManager getRepositoryManager(boolean offline, int timeout, Proxy proxy, String overrideFileName) throws Exception {
RepositoryManagerBuilder builder = getRepositoryManagerBuilder(getRepositoryRoot(), offline, timeout, proxy, overrideFileName);
return builder.buildRepository();
}

protected RepositoryManager getRepositoryManager() throws Exception {
return getRepositoryManager(false, 20000);
return getRepositoryManager(false, 20000, Proxy.NO_PROXY);
}

protected RepositoryManager getRepositoryManager(String overrideFileName) throws Exception {
return getRepositoryManager(false, 20000, overrideFileName);
return getRepositoryManager(false, 20000, Proxy.NO_PROXY, overrideFileName);
}

protected void testComplete(String query, ModuleDetails[] expected, RepositoryManager manager) {
Expand Down
Loading

0 comments on commit 406c161

Please sign in to comment.