Skip to content

Commit

Permalink
add: 添加SenderFactory,并支持为每一个Sender添加Proxy设置
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed May 8, 2018
1 parent 62984ab commit 8c6e9f9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 66 deletions.
135 changes: 69 additions & 66 deletions src/org/nutz/http/Sender.java
@@ -1,7 +1,6 @@
package org.nutz.http;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -25,9 +24,7 @@
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import org.nutz.http.sender.FilePostSender;
import org.nutz.http.sender.GetSender;
import org.nutz.http.sender.PostSender;
import org.nutz.http.sender.DefaultSenderFactory;
import org.nutz.lang.Lang;
import org.nutz.lang.stream.VoidInputStream;
import org.nutz.lang.util.Callback;
Expand Down Expand Up @@ -60,42 +57,35 @@ public static Sender create(String url, int timeout) {
}

public static Sender create(Request request) {
if (request.isGet() || request.isDelete())
return new GetSender(request);
if ((request.isPost() || request.isPut()) && request.getParams() != null) {
for (Object val : request.getParams().values()) {
if (val instanceof File || val instanceof File[]) {
return new FilePostSender(request);
}
}
}
return new PostSender(request);
return factory.create(request);
}

public static Sender create(Request request, int timeout) {
return create(request).setTimeout(timeout);
}

protected Request request;

private int connTimeout;

private int timeout;

protected HttpURLConnection conn;

protected HttpReqRespInterceptor interceptor = new Cookie();

protected Callback<Response> callback;

protected boolean followRedirects = true;

protected SSLSocketFactory sslSocketFactory;

protected Proxy proxy;

protected Sender(Request request) {
this.request = request;
}

protected Callback<Integer> progressListener;

public abstract Response send() throws HttpException;
Expand Down Expand Up @@ -133,7 +123,7 @@ protected Response createResponse(Map<String, String> reHeaders) throws IOExcept
this.interceptor.afterResponse(request, conn, rep);
return rep;
}

protected InputStream detectStreamEncode(String encoding, InputStream ins) throws IOException {
if (encoding != null && encoding.contains("gzip")) {
return new GZIPInputStream(ins);
Expand Down Expand Up @@ -164,36 +154,36 @@ protected void setupDoInputOutputFlag() {
protected void openConnection() throws IOException {
if (this.interceptor != null)
this.interceptor.beforeConnect(request);
ProxySwitcher proxySwitcher = Http.proxySwitcher;
Proxy proxy = this.proxy;
if (proxy == null && Http.proxySwitcher != null) {
proxy = Http.proxySwitcher.getProxy(request);
}
int connTime = connTimeout > 0 ? connTimeout : Default_Conn_Timeout;
if (proxySwitcher != null) {
if (proxy != null) {
try {
Proxy proxy = proxySwitcher.getProxy(request);
if (proxy != null) {
if (Http.autoSwitch) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(proxy.address(), connTime); //5 * 1000
OutputStream out = socket.getOutputStream();
out.write('\n');
out.flush();
}
finally {
if (socket != null)
socket.close();
}
if (Http.autoSwitch) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(proxy.address(), connTime); // 5 * 1000
OutputStream out = socket.getOutputStream();
out.write('\n');
out.flush();
}
finally {
if (socket != null)
socket.close();
}
log.debug("connect via proxy : " + proxy + " for " + request.getUrl());
conn = (HttpURLConnection) request.getUrl().openConnection(proxy);
conn.setConnectTimeout(connTime);
conn.setInstanceFollowRedirects(followRedirects);
if (timeout > 0)
conn.setReadTimeout(timeout);
else
conn.setReadTimeout(Default_Read_Timeout);
return;
}
log.debug("connect via proxy : " + proxy + " for " + request.getUrl());
conn = (HttpURLConnection) request.getUrl().openConnection(proxy);
conn.setConnectTimeout(connTime);
conn.setInstanceFollowRedirects(followRedirects);
if (timeout > 0)
conn.setReadTimeout(timeout);
else
conn.setReadTimeout(Default_Read_Timeout);
return;
}
catch (IOException e) {
if (!Http.autoSwitch) {
Expand All @@ -207,9 +197,9 @@ protected void openConnection() throws IOException {
conn = (HttpURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
if (sslSocketFactory != null)
((HttpsURLConnection)conn).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
else if (Http.sslSocketFactory != null)
((HttpsURLConnection)conn).setSSLSocketFactory(Http.sslSocketFactory);
((HttpsURLConnection) conn).setSSLSocketFactory(Http.sslSocketFactory);
}
if (!Lang.isIPv4Address(host)) {
if (url.getPort() > 0 && url.getPort() != 80)
Expand All @@ -218,9 +208,9 @@ else if (Http.sslSocketFactory != null)
}
conn.setConnectTimeout(connTime);
if (request.getMethodString() == null)
conn.setRequestMethod(request.getMethod().name());
conn.setRequestMethod(request.getMethod().name());
else
conn.setRequestMethod(request.getMethodString());
conn.setRequestMethod(request.getMethodString());
if (timeout > 0)
conn.setReadTimeout(timeout);
else
Expand All @@ -245,12 +235,12 @@ public Sender setTimeout(int timeout) {
public int getTimeout() {
return timeout;
}

public Sender setConnTimeout(int connTimeout) {
this.connTimeout = connTimeout;
return this;
}

public int getConnTimeout() {
return connTimeout;
}
Expand All @@ -259,28 +249,28 @@ public Sender setInterceptor(HttpReqRespInterceptor interceptor) {
this.interceptor = interceptor;
return this;
}

public Sender setCallback(Callback<Response> callback) {
this.callback = callback;
return this;
}

public Response call() throws Exception {
Response resp = send();
if (callback != null)
callback.invoke(resp);
return resp;
}

public Future<Response> send(Callback<Response> callback) throws HttpException {
if (es == null)
throw new IllegalStateException("Sender ExecutorService is null, Call setup first");
this.callback = callback;
return es.submit(this);
}

protected static ExecutorService es;

public static ExecutorService setup(ExecutorService es) {
if (Sender.es != null)
shutdown();
Expand All @@ -289,48 +279,61 @@ public static ExecutorService setup(ExecutorService es) {
Sender.es = es;
return es;
}

public static List<Runnable> shutdown() {
ExecutorService _es = es;
es = null;
if (_es == null)
return null;
return _es.shutdownNow();
}

public static ExecutorService getExecutorService() {
return es;
}

public Sender setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}

protected OutputStream getOutputStream() throws IOException {
OutputStream out = conn.getOutputStream();
if (progressListener == null)
return out;
return new FilterOutputStream(out) {
int count;

public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
count += len;
progressListener.invoke(count);
}
};
}

public int getEstimationSize() throws IOException {
return 0;
}

public Sender setProgressListener(Callback<Integer> progressListener) {
this.progressListener = progressListener;
return this;
}
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {

public Sender setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
return this;
}

public Sender setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}

protected static SenderFactory factory = new DefaultSenderFactory();

public static void setFactory(SenderFactory factory) {
Sender.factory = factory;
}
}
6 changes: 6 additions & 0 deletions src/org/nutz/http/SenderFactory.java
@@ -0,0 +1,6 @@
package org.nutz.http;

public interface SenderFactory {

Sender create(Request request);
}
23 changes: 23 additions & 0 deletions src/org/nutz/http/sender/DefaultSenderFactory.java
@@ -0,0 +1,23 @@
package org.nutz.http.sender;

import java.io.File;

import org.nutz.http.Request;
import org.nutz.http.Sender;
import org.nutz.http.SenderFactory;

public class DefaultSenderFactory implements SenderFactory {

public Sender create(Request request) {
if (request.isGet() || request.isDelete())
return new GetSender(request);
if ((request.isPost() || request.isPut()) && request.getParams() != null) {
for (Object val : request.getParams().values()) {
if (val instanceof File || val instanceof File[]) {
return new FilePostSender(request);
}
}
}
return new PostSender(request);
}
}

0 comments on commit 8c6e9f9

Please sign in to comment.