Skip to content

Commit

Permalink
Merge pull request #186 from rememberber/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rememberber committed Jul 28, 2019
2 parents 56a7304 + 85739f9 commit 0e9c252
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 20 deletions.
4 changes: 4 additions & 0 deletions download.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<details>
<summary>Windows</summary>

[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)
Expand Down Expand Up @@ -63,6 +65,7 @@
<details>
<summary>Mac OS</summary>

[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)
Expand Down Expand Up @@ -101,6 +104,7 @@
<details>
<summary>Linux</summary>

[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)
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<httpasyncclient.version>4.1.4</httpasyncclient.version>
<HikariCP.version>3.3.1</HikariCP.version>
<quartz.version>2.3.1</quartz.version>
<okhttp.version>4.0.1</okhttp.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -297,6 +298,11 @@
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>

</dependencies>

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fangxuele/tool/push/domain/TMsgHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TMsgHttp implements Serializable {

private String modifiedTime;

private String bodyType;

private static final long serialVersionUID = 1L;

public Integer getId() {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpMsgForm.NameValueObject> paramList;
public static List<HttpMsgForm.NameValueObject> headerList;
public static List<HttpMsgForm.CookieObject> cookieList;
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +11,22 @@
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;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* <pre>
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String> 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<HttpCookie> 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) {
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/fangxuele/tool/push/ui/UiConsts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
* 主窗口图标-大
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/fangxuele/tool/push/ui/form/msg/HttpMsgForm.form
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="41458" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="41458" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -338,12 +338,32 @@
<children>
<component id="2c381" class="javax.swing.JTextArea" binding="bodyTextArea">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="6c97c" class="javax.swing.JComboBox" binding="bodyTypeComboBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="text/plain"/>
<item value="application/json"/>
<item value="application/javascript"/>
<item value="application/xml"/>
<item value="text/xml"/>
<item value="text/html"/>
</model>
</properties>
</component>
<hspacer id="e187">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</children>
Expand Down
Loading

0 comments on commit 0e9c252

Please sign in to comment.