diff --git a/src/org/nutz/http/Sender.java b/src/org/nutz/http/Sender.java index c231f49ad..7314e2f47 100644 --- a/src/org/nutz/http/Sender.java +++ b/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; @@ -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; @@ -60,16 +57,7 @@ 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) { @@ -77,25 +65,27 @@ public static Sender create(Request request, int timeout) { } protected Request request; - + private int connTimeout; private int timeout; protected HttpURLConnection conn; - + protected HttpReqRespInterceptor interceptor = new Cookie(); - + protected Callback callback; - + protected boolean followRedirects = true; - + protected SSLSocketFactory sslSocketFactory; + protected Proxy proxy; + protected Sender(Request request) { this.request = request; } - + protected Callback progressListener; public abstract Response send() throws HttpException; @@ -133,7 +123,7 @@ protected Response createResponse(Map 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); @@ -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) { @@ -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) @@ -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 @@ -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; } @@ -259,28 +249,28 @@ public Sender setInterceptor(HttpReqRespInterceptor interceptor) { this.interceptor = interceptor; return this; } - + public Sender setCallback(Callback callback) { this.callback = callback; return this; } - + public Response call() throws Exception { Response resp = send(); if (callback != null) callback.invoke(resp); return resp; } - + public Future send(Callback 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(); @@ -289,7 +279,7 @@ public static ExecutorService setup(ExecutorService es) { Sender.es = es; return es; } - + public static List shutdown() { ExecutorService _es = es; es = null; @@ -297,22 +287,23 @@ public static List shutdown() { 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; @@ -320,17 +311,29 @@ public void write(byte[] b, int off, int len) throws IOException { } }; } - + public int getEstimationSize() throws IOException { return 0; } - + public Sender setProgressListener(Callback 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; } } diff --git a/src/org/nutz/http/SenderFactory.java b/src/org/nutz/http/SenderFactory.java new file mode 100644 index 000000000..364ec2949 --- /dev/null +++ b/src/org/nutz/http/SenderFactory.java @@ -0,0 +1,6 @@ +package org.nutz.http; + +public interface SenderFactory { + + Sender create(Request request); +} \ No newline at end of file diff --git a/src/org/nutz/http/sender/DefaultSenderFactory.java b/src/org/nutz/http/sender/DefaultSenderFactory.java new file mode 100644 index 000000000..905bea75e --- /dev/null +++ b/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); + } +} \ No newline at end of file