Skip to content

Commit

Permalink
Added proxy information to HttpBrowser (closes #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Dec 23, 2013
1 parent 7874d00 commit 1c62bd6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
9 changes: 8 additions & 1 deletion jodd-http/src/main/java/jodd/http/HttpBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ public HttpBrowser() {
httpConnectionProvider = JoddHttp.httpConnectionProvider;
}

/**
* Defines proxy for a browser.
*/
public void setProxyInfo(ProxyInfo proxyInfo) {
httpConnectionProvider.useProxy(proxyInfo);
}

/**
* Defines {@link jodd.http.HttpConnectionProvider} for this browser session.
* Resets the previous proxy definition, if set.
*/
public void setHttpConnectionProvider(HttpConnectionProvider httpConnectionProvider) {
this.httpConnectionProvider = httpConnectionProvider;
Expand Down Expand Up @@ -146,5 +154,4 @@ protected void addCookies(HttpRequest httpRequest) {
httpRequest.header("cookie", cookieString.toString(), true);
}
}

}
6 changes: 6 additions & 0 deletions jodd-http/src/main/java/jodd/http/HttpConnectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/
public interface HttpConnectionProvider {

/**
* Specifies {@link ProxyInfo proxy} for provide to use.
*/
public void useProxy(ProxyInfo proxyInfo);


/**
* Creates new {@link HttpConnection}
* from {@link jodd.http.HttpRequest request}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2003-2013, Jodd Team (jodd.org). All Rights Reserved.

package jodd.http.net;

import javax.net.SocketFactory;
package jodd.http;

/**
* Proxy information.
Expand Down Expand Up @@ -98,20 +96,4 @@ public String getProxyPassword() {
return proxyPassword;
}

/**
* Returns socket factory based on socket
*/
public SocketFactory getSocketFactory() {
switch (proxyType) {
case NONE:
return SocketFactory.getDefault();
case HTTP:
return new HTTPProxySocketFactory(this);
case SOCKS4:
return new Socks4ProxySocketFactory(this);
case SOCKS5:
return new Socks5ProxySocketFactory(this);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package jodd.http.net;

import jodd.http.HttpException;
import jodd.http.ProxyInfo;
import jodd.util.Base64;

import javax.net.SocketFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jodd.http.HttpConnection;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.ProxyInfo;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
Expand Down Expand Up @@ -42,7 +43,7 @@ public HttpConnection createHttpConnection(HttpRequest httpRequest) throws IOExc

socket = sslSocket;
} else {
SocketFactory socketFactory = proxy.getSocketFactory();
SocketFactory socketFactory = getSocketFactory(proxy);

socket = createSocket(socketFactory, httpRequest.host(), httpRequest.port());
}
Expand All @@ -57,4 +58,21 @@ protected Socket createSocket(SocketFactory socketFactory, String host, int port
return socketFactory.createSocket(host, port);
}

/**
* Returns socket factory based on proxy type.
*/
public SocketFactory getSocketFactory(ProxyInfo proxy) {
switch (proxy.getProxyType()) {
case NONE:
return SocketFactory.getDefault();
case HTTP:
return new HTTPProxySocketFactory(proxy);
case SOCKS4:
return new Socks4ProxySocketFactory(proxy);
case SOCKS5:
return new Socks5ProxySocketFactory(proxy);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package jodd.http.net;

import jodd.http.HttpException;
import jodd.http.ProxyInfo;

import javax.net.SocketFactory;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package jodd.http.net;

import jodd.http.HttpException;
import jodd.http.ProxyInfo;

import java.io.IOException;
import java.io.InputStream;
Expand Down

0 comments on commit 1c62bd6

Please sign in to comment.