diff --git a/download.md b/download.md index fee76f3c..16de86df 100644 --- a/download.md +++ b/download.md @@ -3,6 +3,8 @@
Windows +[WePush-v3.6.1_190728-x64-Setup.exe](http://download.zhoubochina.com/exe/WePush-v3.6.1_190728-x64-Setup.exe) +[WePush-with-jre-v3.6.1_190728-x64-Setup.exe](http://download.zhoubochina.com/exe/WePush-with-jre-v3.6.1_190728-x64-Setup.exe) [WePush-v3.6.0_190721-x64-Setup.exe](http://download.zhoubochina.com/exe/WePush-v3.6.0_190721-x64-Setup.exe) [WePush-with-jre-v3.6.0_190721-x64-Setup.exe](http://download.zhoubochina.com/exe/WePush-with-jre-v3.6.0_190721-x64-Setup.exe) [WePush-v3.5.0_190713-x64-Setup.exe](http://download.zhoubochina.com/exe/WePush-v3.5.0_190713-x64-Setup.exe) @@ -63,6 +65,7 @@
Mac OS +[v_3.6.1_190728.app](http://download.zhoubochina.com/mac/3.6.1.zip) [v_3.6.0_190721.app](http://download.zhoubochina.com/mac/3.6.0.zip) [v_3.5.0_190713.app](http://download.zhoubochina.com/mac/3.5.0.zip) [v_3.4.1_190624.app](http://download.zhoubochina.com/mac/3.4.1.zip) @@ -101,6 +104,7 @@
Linux +[v3.6.1_190728](http://download.zhoubochina.com/linux/WePush-3.6.1.zip) [v3.6.0_190721](http://download.zhoubochina.com/linux/WePush-3.6.0.zip) [v3.5.0_190713](http://download.zhoubochina.com/linux/WePush-3.5.0.zip) [v3.4.2_190630](http://download.zhoubochina.com/linux/WePush-3.4.2.zip) diff --git a/pom.xml b/pom.xml index 26bfd251..01dbe9f0 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,7 @@ 4.1.4 3.3.1 2.3.1 + 4.0.1 @@ -297,6 +298,11 @@ quartz ${quartz.version} + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + diff --git a/src/main/java/com/fangxuele/tool/push/domain/TMsgHttp.java b/src/main/java/com/fangxuele/tool/push/domain/TMsgHttp.java index f70602ca..5624365e 100644 --- a/src/main/java/com/fangxuele/tool/push/domain/TMsgHttp.java +++ b/src/main/java/com/fangxuele/tool/push/domain/TMsgHttp.java @@ -25,6 +25,8 @@ public class TMsgHttp implements Serializable { private String modifiedTime; + private String bodyType; + private static final long serialVersionUID = 1L; public Integer getId() { @@ -114,4 +116,12 @@ public String getModifiedTime() { public void setModifiedTime(String modifiedTime) { this.modifiedTime = modifiedTime == null ? null : modifiedTime.trim(); } + + public String getBodyType() { + return bodyType; + } + + public void setBodyType(String bodyType) { + this.bodyType = bodyType == null ? null : bodyType.trim(); + } } \ No newline at end of file diff --git a/src/main/java/com/fangxuele/tool/push/logic/msgmaker/HttpMsgMaker.java b/src/main/java/com/fangxuele/tool/push/logic/msgmaker/HttpMsgMaker.java index e9bcc4fe..7f01a933 100644 --- a/src/main/java/com/fangxuele/tool/push/logic/msgmaker/HttpMsgMaker.java +++ b/src/main/java/com/fangxuele/tool/push/logic/msgmaker/HttpMsgMaker.java @@ -29,6 +29,7 @@ public class HttpMsgMaker extends BaseMsgMaker implements IMsgMaker { public static String method; public static String url; public static String body; + public static String bodyType; public static List paramList; public static List headerList; public static List cookieList; @@ -38,6 +39,7 @@ public void prepare() { method = (String) HttpMsgForm.getInstance().getMethodComboBox().getSelectedItem(); url = HttpMsgForm.getInstance().getUrlTextField().getText().trim(); body = HttpMsgForm.getInstance().getBodyTextArea().getText(); + bodyType = (String) HttpMsgForm.getInstance().getBodyTypeComboBox().getSelectedItem(); // Params========================= if (HttpMsgForm.getInstance().getParamTable().getModel().getRowCount() == 0) { @@ -120,7 +122,7 @@ public HttpMsg makeMsg(String[] msgData) { httpCookie.setDomain(cookieObject.getDomain()); httpCookie.setPath(cookieObject.getPath()); try { - httpCookie.setMaxAge(DateUtils.parseDate(cookieObject.getExpiry(), "yyyy-MM-dd").getTime()); + httpCookie.setMaxAge(DateUtils.parseDate(cookieObject.getExpiry(), "yyyy-MM-dd HH:mm:ss").getTime()); } catch (ParseException e) { log.error(e.toString()); } diff --git a/src/main/java/com/fangxuele/tool/push/logic/msgsender/HttpMsgSender.java b/src/main/java/com/fangxuele/tool/push/logic/msgsender/HttpMsgSender.java index b75b6828..59986f26 100644 --- a/src/main/java/com/fangxuele/tool/push/logic/msgsender/HttpMsgSender.java +++ b/src/main/java/com/fangxuele/tool/push/logic/msgsender/HttpMsgSender.java @@ -1,5 +1,6 @@ package com.fangxuele.tool.push.logic.msgsender; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; @@ -10,6 +11,14 @@ import com.fangxuele.tool.push.logic.PushControl; import com.fangxuele.tool.push.logic.msgmaker.HttpMsgMaker; import lombok.extern.slf4j.Slf4j; +import okhttp3.ConnectionPool; +import okhttp3.FormBody; +import okhttp3.HttpUrl; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; import org.apache.commons.lang3.StringUtils; import java.net.HttpCookie; @@ -17,6 +26,7 @@ import java.net.Proxy; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** *
@@ -31,14 +41,26 @@ public class HttpMsgSender implements IMsgSender {
 
     private HttpMsgMaker httpMsgMaker;
 
+    public volatile static OkHttpClient okHttpClient;
+
     public volatile static Proxy proxy;
 
     public HttpMsgSender() {
         httpMsgMaker = new HttpMsgMaker();
+        okHttpClient = getOkHttpClient();
     }
 
     @Override
     public HttpSendResult send(String[] msgData) {
+        return sendUseOkHttp(msgData);
+    }
+
+    @Override
+    public SendResult asyncSend(String[] msgData) {
+        return null;
+    }
+
+    public HttpSendResult sendUseHutool(String[] msgData) {
         HttpSendResult sendResult = new HttpSendResult();
         HttpResponse httpResponse;
         try {
@@ -78,9 +100,8 @@ public HttpSendResult send(String[] msgData) {
                 }
             }
             if (httpMsg.getCookies() != null && !httpMsg.getCookies().isEmpty()) {
-                for (HttpCookie cookie : httpMsg.getCookies()) {
-                    httpRequest.cookie(cookie);
-                }
+                HttpCookie[] cookies = ArrayUtil.toArray(httpMsg.getCookies(), HttpCookie.class);
+                httpRequest.cookie(cookies);
             }
             if (StringUtils.isNotEmpty(httpMsg.getBody())) {
                 httpRequest.body(httpMsg.getBody());
@@ -93,7 +114,7 @@ public HttpSendResult send(String[] msgData) {
                 sendResult.setSuccess(true);
                 return sendResult;
             } else {
-                httpResponse = httpRequest.execute(true);
+                httpResponse = httpRequest.execute();
                 if (!httpResponse.isOk()) {
                     sendResult.setSuccess(false);
                     sendResult.setInfo(httpResponse.toString());
@@ -137,6 +158,125 @@ public HttpSendResult send(String[] msgData) {
         return sendResult;
     }
 
+    public HttpSendResult sendUseOkHttp(String[] msgData) {
+        HttpSendResult sendResult = new HttpSendResult();
+        try {
+            HttpMsg httpMsg = httpMsgMaker.makeMsg(msgData);
+
+            Request.Builder requestBuilder = new Request.Builder();
+
+            RequestBody requestBody = null;
+            if (!"GET".equals(HttpMsgMaker.method) && httpMsg.getParamMap() != null && !httpMsg.getParamMap().isEmpty()) {
+                FormBody.Builder formBodyBuilder = new FormBody.Builder();
+                for (Map.Entry paramEntry : httpMsg.getParamMap().entrySet()) {
+                    formBodyBuilder.add(paramEntry.getKey(), (String) paramEntry.getValue());
+                }
+                requestBody = formBodyBuilder.build();
+            } else if (StringUtils.isNotEmpty(httpMsg.getBody())) {
+                String bodyType = HttpMsgMaker.bodyType;
+                MediaType mediaType = MediaType.get(bodyType + "; charset=utf-8");
+                requestBody = RequestBody.create(httpMsg.getBody(), mediaType);
+            }
+
+            if (httpMsg.getHeaderMap() != null && !httpMsg.getHeaderMap().isEmpty()) {
+                for (Map.Entry headerEntry : httpMsg.getHeaderMap().entrySet()) {
+                    requestBuilder.addHeader(headerEntry.getKey(), (String) headerEntry.getValue());
+                }
+            }
+            if (httpMsg.getCookies() != null && !httpMsg.getCookies().isEmpty()) {
+                requestBuilder.addHeader(Header.COOKIE.toString(), cookieHeader(httpMsg.getCookies()));
+            }
+            switch (HttpMsgMaker.method) {
+                case "GET":
+                    HttpUrl.Builder urlBuilder = HttpUrl.parse(httpMsg.getUrl()).newBuilder();
+                    if (httpMsg.getParamMap() != null && !httpMsg.getParamMap().isEmpty()) {
+                        for (Map.Entry paramEntry : httpMsg.getParamMap().entrySet()) {
+                            urlBuilder.addQueryParameter(paramEntry.getKey(), (String) paramEntry.getValue());
+                        }
+                    }
+                    requestBuilder.url(urlBuilder.build()).get();
+                    break;
+                case "POST":
+                    requestBuilder.url(httpMsg.getUrl()).post(requestBody);
+                    break;
+                case "PUT":
+                    requestBuilder.url(httpMsg.getUrl()).put(requestBody);
+                    break;
+                case "PATCH":
+                    requestBuilder.url(httpMsg.getUrl()).patch(requestBody);
+                    break;
+                case "DELETE":
+                    requestBuilder.url(httpMsg.getUrl()).delete(requestBody);
+                    break;
+                case "HEAD":
+                    requestBuilder.url(httpMsg.getUrl()).head();
+                    break;
+                case "OPTIONS":
+                    return sendUseHutool(msgData);
+                default:
+                    requestBuilder.url(httpMsg.getUrl());
+            }
+
+            Request request = requestBuilder.build();
+
+            if (PushControl.dryRun) {
+                sendResult.setSuccess(true);
+                return sendResult;
+            } else {
+                Response response = okHttpClient.newCall(request).execute();
+                if (!response.isSuccessful()) {
+                    sendResult.setSuccess(false);
+                    sendResult.setInfo(response.toString());
+                    return sendResult;
+                }
+
+                String responseBody = "";
+                if (response.body() != null) {
+                    responseBody = response.body().string();
+                }
+                sendResult.setInfo(responseBody);
+                if (responseBody.startsWith("{") && responseBody.endsWith("}")) {
+                    try {
+                        responseBody = JSONUtil.toJsonPrettyStr(responseBody);
+                    } catch (Exception e) {
+                        log.error(e.toString());
+                    }
+                }
+                sendResult.setBody(responseBody);
+
+                sendResult.setHeaders(response.headers().toString());
+
+                StringBuilder cookiesBuilder = StrUtil.builder();
+                List headerList = response.headers(Header.SET_COOKIE.toString());
+                for (String cookieStr : headerList) {
+                    cookiesBuilder.append(cookieStr).append(StrUtil.CRLF);
+                }
+
+                sendResult.setCookies(cookiesBuilder.toString());
+
+                sendResult.setSuccess(true);
+                return sendResult;
+            }
+        } catch (Exception e) {
+            sendResult.setSuccess(false);
+            sendResult.setInfo(e.getMessage());
+            log.error(e.toString());
+            return sendResult;
+        }
+    }
+
+    private String cookieHeader(List cookies) {
+        StringBuilder cookieHeader = new StringBuilder();
+        for (int i = 0, size = cookies.size(); i < size; i++) {
+            if (i > 0) {
+                cookieHeader.append("; ");
+            }
+            HttpCookie cookie = cookies.get(i);
+            cookieHeader.append(cookie.getName()).append('=').append(cookie.getValue());
+        }
+        return cookieHeader.toString();
+    }
+
     private static Proxy getProxy() {
         if (proxy == null) {
             synchronized (HttpMsgSender.class) {
@@ -148,8 +288,22 @@ private static Proxy getProxy() {
         return proxy;
     }
 
-    @Override
-    public SendResult asyncSend(String[] msgData) {
-        return null;
+    public static OkHttpClient getOkHttpClient() {
+        if (okHttpClient == null) {
+            synchronized (HttpMsgSender.class) {
+                if (okHttpClient == null) {
+                    OkHttpClient.Builder builder = new OkHttpClient.Builder();
+                    builder.connectTimeout(3, TimeUnit.MINUTES);
+                    if (App.config.isHttpUseProxy()) {
+                        builder.proxy(getProxy());
+                    }
+
+                    ConnectionPool pool = new ConnectionPool(App.config.getThreadCount(), 10, TimeUnit.MINUTES);
+                    builder.connectionPool(pool);
+                    okHttpClient = builder.build();
+                }
+            }
+        }
+        return okHttpClient;
     }
 }
diff --git a/src/main/java/com/fangxuele/tool/push/ui/UiConsts.java b/src/main/java/com/fangxuele/tool/push/ui/UiConsts.java
index 228bd68a..6cbb2188 100644
--- a/src/main/java/com/fangxuele/tool/push/ui/UiConsts.java
+++ b/src/main/java/com/fangxuele/tool/push/ui/UiConsts.java
@@ -16,7 +16,7 @@ public class UiConsts {
      * 软件名称,版本
      */
     public final static String APP_NAME = "WePush";
-    public final static String APP_VERSION = "v_3.6.0_190721";
+    public final static String APP_VERSION = "v_3.6.1_190728";
 
     /**
      * 主窗口图标-大
diff --git a/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.form b/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.form
index b99c3851..6e2269ad 100644
--- a/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.form
+++ b/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.form
@@ -328,7 +328,7 @@
             
             
             
-              
+              
                 
                 
                   
@@ -338,12 +338,32 @@
                 
                   
                     
-                      
+                      
                         
                       
                     
                     
                   
+                  
+                    
+                      
+                    
+                    
+                      
+                        
+                        
+                        
+                        
+                        
+                        
+                      
+                    
+                  
+                  
+                    
+                      
+                    
+                  
                 
               
             
diff --git a/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.java b/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.java
index 81f78e18..3cbf54ab 100644
--- a/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.java
+++ b/src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.java
@@ -10,6 +10,7 @@
 import com.fangxuele.tool.push.util.SqliteUtil;
 import com.intellij.uiDesigner.core.GridConstraints;
 import com.intellij.uiDesigner.core.GridLayoutManager;
+import com.intellij.uiDesigner.core.Spacer;
 import lombok.Getter;
 import lombok.Setter;
 import org.apache.commons.lang3.StringUtils;
@@ -56,6 +57,7 @@ public class HttpMsgForm implements IMsgForm {
     private JTextField cookieExpiryTextField;
     private JTextArea bodyTextArea;
     private JTabbedPane tabbedPane1;
+    private JComboBox bodyTypeComboBox;
 
     private static HttpMsgForm httpMsgForm;
 
@@ -172,6 +174,7 @@ public void init(String msgName) {
             getInstance().getMethodComboBox().setSelectedItem(tMsgHttp.getMethod());
             getInstance().getUrlTextField().setText(tMsgHttp.getUrl());
             getInstance().getBodyTextArea().setText(tMsgHttp.getBody());
+            getInstance().getBodyTypeComboBox().setSelectedItem(tMsgHttp.getBodyType());
             switchMethod(tMsgHttp.getMethod());
 
             // Params=====================================
@@ -262,6 +265,7 @@ public void save(String msgName) {
             String method = (String) getInstance().getMethodComboBox().getSelectedItem();
             String url = getInstance().getUrlTextField().getText();
             String body = getInstance().getBodyTextArea().getText();
+            String bodyType = (String) getInstance().getBodyTypeComboBox().getSelectedItem();
             String now = SqliteUtil.nowDateForSqlite();
 
             TMsgHttp tMsgHttp = new TMsgHttp();
@@ -270,6 +274,7 @@ public void save(String msgName) {
             tMsgHttp.setMethod(method);
             tMsgHttp.setUrl(url);
             tMsgHttp.setBody(body);
+            tMsgHttp.setBodyType(bodyType);
             tMsgHttp.setCreateTime(now);
             tMsgHttp.setModifiedTime(now);
 
@@ -370,6 +375,7 @@ public static void clearAllField() {
         getInstance().getCookiePathTextField().setText("");
         getInstance().getCookieExpiryTextField().setText("");
         getInstance().getBodyTextArea().setText("");
+        getInstance().getBodyTypeComboBox().setSelectedIndex(0);
         initParamTable();
         initHeaderTable();
         initCookieTable();
@@ -617,10 +623,22 @@ public static class CookieObject implements Serializable {
         panel8.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
         tabbedPane1.addTab("Body", panel8);
         final JPanel panel9 = new JPanel();
-        panel9.setLayout(new GridLayoutManager(1, 1, new Insets(5, 0, 0, 0), -1, -1));
+        panel9.setLayout(new GridLayoutManager(2, 2, new Insets(5, 0, 0, 0), -1, -1));
         panel8.add(panel9, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
         bodyTextArea = new JTextArea();
-        panel9.add(bodyTextArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
+        panel9.add(bodyTextArea, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
+        bodyTypeComboBox = new JComboBox();
+        final DefaultComboBoxModel defaultComboBoxModel2 = new DefaultComboBoxModel();
+        defaultComboBoxModel2.addElement("text/plain");
+        defaultComboBoxModel2.addElement("application/json");
+        defaultComboBoxModel2.addElement("application/javascript");
+        defaultComboBoxModel2.addElement("application/xml");
+        defaultComboBoxModel2.addElement("text/xml");
+        defaultComboBoxModel2.addElement("text/html");
+        bodyTypeComboBox.setModel(defaultComboBoxModel2);
+        panel9.add(bodyTypeComboBox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
+        final Spacer spacer1 = new Spacer();
+        panel9.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
     }
 
     /**
diff --git a/src/main/resources/db_init.sql b/src/main/resources/db_init.sql
index 570f3a64..d3f60f3a 100644
--- a/src/main/resources/db_init.sql
+++ b/src/main/resources/db_init.sql
@@ -215,6 +215,7 @@ create table if not exists t_msg_http
     headers       text,
     cookies       text,
     body          text,
+    body_type     text,
     create_time   datetime,
     modified_time datetime
 );
diff --git a/src/main/resources/mapper/TMsgHttpMapper.xml b/src/main/resources/mapper/TMsgHttpMapper.xml
index c885a532..acebb97c 100644
--- a/src/main/resources/mapper/TMsgHttpMapper.xml
+++ b/src/main/resources/mapper/TMsgHttpMapper.xml
@@ -13,10 +13,11 @@
         
         
         
+        
     
     
         id, msg_type, msg_name, method, url, params, headers, cookies, body, create_time,
-    modified_time
+    modified_time, body_type
     
     
@@ -177,6 +187,7 @@
             headers       = #{headers,jdbcType=VARCHAR},
             cookies       = #{cookies,jdbcType=VARCHAR},
             body          = #{body,jdbcType=VARCHAR},
+            body_type     = #{bodyType,jdbcType=VARCHAR},
             modified_time = #{modifiedTime,jdbcType=VARCHAR}
         where msg_type = #{msgType,jdbcType=INTEGER}
           and msg_name = #{msgName,jdbcType=VARCHAR}
diff --git a/src/main/resources/upgrade/30.sql b/src/main/resources/upgrade/30.sql
new file mode 100644
index 00000000..1bfc3a57
--- /dev/null
+++ b/src/main/resources/upgrade/30.sql
@@ -0,0 +1,2 @@
+alter table t_msg_http
+    add body_type text;
\ No newline at end of file
diff --git a/src/main/resources/version_summary.json b/src/main/resources/version_summary.json
index b15c193e..1a21a799 100644
--- a/src/main/resources/version_summary.json
+++ b/src/main/resources/version_summary.json
@@ -1,5 +1,5 @@
 {
-  "currentVersion": "v_3.6.0_190721",
+  "currentVersion": "v_3.6.1_190728",
   "versionIndex": {
     "v_1.1.0_170701": "0",
     "v_1.2.0_170831": "1",
@@ -30,7 +30,8 @@
     "v_3.4.1_190624": "26",
     "v_3.4.2_190630": "27",
     "v_3.5.0_190713": "28",
-    "v_3.6.0_190721": "29"
+    "v_3.6.0_190721": "29",
+    "v_3.6.1_190728": "30"
   },
   "versionDetailList": [
     {
@@ -182,6 +183,11 @@
       "version": "v_3.6.0_190721",
       "title": "消息类型新增支持HTTP请求",
       "log": "● feature:消息类型新增支持HTTP请求\n● feature:成员导出支持选择文件类型\n● feature:支持外部AccessToken\n● feature:推送页签增加tps展示\n● optimization:消息编辑form重构\n● 其他细节调整\n"
+    },
+    {
+      "version": "v_3.6.1_190728",
+      "title": "HTTP请求类型消息使用OkHttp",
+      "log": "● feature:HTTP请求类型消息使用OkHttp引擎\n● optimization:HTTP请求支持选择body类型\n"
     }
   ]
 }
\ No newline at end of file