From 64d930bfbcb4e4d08bd7c7cc77e592e505c3481c Mon Sep 17 00:00:00 2001 From: Naccl Date: Thu, 3 Mar 2022 17:36:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9AWebhook=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=80=9A=E9=81=93=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?Header=20(#129)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复:手动重发消息中UTC时间未转换本地时间 (#122) * 新增:Webhook发送通道支持设置Header (#128) --- .../idormy/sms/forwarder/SenderActivity.java | 71 ++++++++++++++++++- .../model/vo/WebNotifySettingVo.java | 5 +- .../idormy/sms/forwarder/sender/SendUtil.java | 2 +- .../forwarder/sender/SenderWebNotifyMsg.java | 22 +++--- .../main/res/drawable/ic_add_round_fill.xml | 9 +++ .../layout/alert_dialog_setview_webnotify.xml | 31 ++++++++ app/src/main/res/layout/item_add_header.xml | 47 ++++++++++++ app/src/main/res/values-en/strings.xml | 5 ++ app/src/main/res/values/strings.xml | 5 ++ 9 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/drawable/ic_add_round_fill.xml create mode 100644 app/src/main/res/layout/item_add_header.xml diff --git a/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java b/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java index 79510b9f..719e4b8d 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java +++ b/app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java @@ -31,6 +31,7 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RadioGroup; @@ -86,6 +87,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; @SuppressWarnings("deprecation") public class SenderActivity extends AppCompatActivity { @@ -832,6 +834,57 @@ public class SenderActivity extends AppCompatActivity { } + //header序号 + int headerItemId = 0; + + /** + * 动态增删header + * + * @param headerItemMap 管理item的map,用于删除指定header + * @param linearLayoutWebNotifyHeaders 需要挂载item的LinearLayout + * @param key header的key,为空则不设置 + * @param value header的value,为空则不设置 + */ + private void addHeaderItemLinearLayout(Map headerItemMap, final LinearLayout linearLayoutWebNotifyHeaders, String key, String value) { + LinearLayout linearLayoutItemAddHeader = (LinearLayout) View.inflate(this, R.layout.item_add_header, null); + ImageView imageViewRemoveHeader = linearLayoutItemAddHeader.findViewById(R.id.imageViewRemoveHeader); + + if (key != null && value != null) { + EditText editTextHeaderKey = linearLayoutItemAddHeader.findViewById(R.id.editTextHeaderKey); + EditText editTextHeaderValue = linearLayoutItemAddHeader.findViewById(R.id.editTextHeaderValue); + editTextHeaderKey.setText(key); + editTextHeaderValue.setText(value); + } + + imageViewRemoveHeader.setTag(headerItemId); + imageViewRemoveHeader.setOnClickListener(view2 -> { + int itemId = (int) view2.getTag(); + linearLayoutWebNotifyHeaders.removeView(headerItemMap.get(itemId)); + headerItemMap.remove(itemId); + }); + linearLayoutWebNotifyHeaders.addView(linearLayoutItemAddHeader); + headerItemMap.put(headerItemId, linearLayoutItemAddHeader); + headerItemId++; + } + + /** + * 从EditText控件中获取全部headers + * + * @param headerItemMap 管理item的map + * @return 全部headers + */ + private Map getHeadersFromHeaderItemMap(Map headerItemMap) { + Map headers = new HashMap<>(); + for (LinearLayout headerItem : headerItemMap.values()) { + EditText editTextHeaderKey = headerItem.findViewById(R.id.editTextHeaderKey); + EditText editTextHeaderValue = headerItem.findViewById(R.id.editTextHeaderValue); + String key = editTextHeaderKey.getText().toString().trim(); + String value = editTextHeaderValue.getText().toString().trim(); + headers.put(key, value); + } + return headers; + } + //webhook @SuppressLint("SimpleDateFormat") private void setWebNotify(final SenderModel senderModel, final boolean isClone) { @@ -858,13 +911,25 @@ public class SenderActivity extends AppCompatActivity { final EditText editTextWebNotifyWebParams = view1.findViewById(R.id.editTextWebNotifyWebParams); final ClearEditText editTextWebNotifySecret = view1.findViewById(R.id.editTextWebNotifySecret); final RadioGroup radioGroupWebNotifyMethod = view1.findViewById(R.id.radioGroupWebNotifyMethod); + + Map headerItemMap = new HashMap<>(2); + final LinearLayout linearLayoutWebNotifyHeaders = view1.findViewById(R.id.linearLayoutWebNotifyHeaders); + final ImageView imageViewWebNotifyAddHeader = view1.findViewById(R.id.imageViewWebNotifyAddHeader); + if (webNotifySettingVo != null) { editTextWebNotifyWebServer.setText(webNotifySettingVo.getWebServer()); editTextWebNotifyWebParams.setText(webNotifySettingVo.getWebParams()); editTextWebNotifySecret.setText(webNotifySettingVo.getSecret()); radioGroupWebNotifyMethod.check(webNotifySettingVo.getWebNotifyMethodCheckId()); + //set header + for (Map.Entry header : webNotifySettingVo.getHeaders().entrySet()) { + addHeaderItemLinearLayout(headerItemMap, linearLayoutWebNotifyHeaders, header.getKey(), header.getValue()); + } } + //add header + imageViewWebNotifyAddHeader.setOnClickListener(view -> addHeaderItemLinearLayout(headerItemMap, linearLayoutWebNotifyHeaders, null, null)); + Button buttonOk = view1.findViewById(R.id.buttonOk); Button buttonDel = view1.findViewById(R.id.buttonDel); Button buttonTest = view1.findViewById(R.id.buttonTest); @@ -887,13 +952,14 @@ public class SenderActivity extends AppCompatActivity { String secret = editTextWebNotifySecret.getText().trim(); String method = radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST"; String webParams = editTextWebNotifyWebParams.getText().toString().trim(); + Map headers = getHeadersFromHeaderItemMap(headerItemMap); if (!CommonUtil.checkUrl(webServer, false)) { ToastUtils.delayedShow(R.string.invalid_webserver, 3000); return; } - WebNotifySettingVo webNotifySettingVoNew = new WebNotifySettingVo(webServer, secret, method, webParams); + WebNotifySettingVo webNotifySettingVoNew = new WebNotifySettingVo(webServer, secret, method, webParams, headers); if (isClone || senderModel == null) { SenderModel newSenderModel = new SenderModel(); newSenderModel.setName(senderName); @@ -927,6 +993,7 @@ public class SenderActivity extends AppCompatActivity { String secret = editTextWebNotifySecret.getText().trim(); String method = radioGroupWebNotifyMethod.getCheckedRadioButtonId() == R.id.radioWebNotifyMethodGet ? "GET" : "POST"; String webParams = editTextWebNotifyWebParams.getText().toString().trim(); + Map headers = getHeadersFromHeaderItemMap(headerItemMap); if (!CommonUtil.checkUrl(webServer, false)) { ToastUtils.delayedShow(R.string.invalid_webserver, 3000); @@ -935,7 +1002,7 @@ public class SenderActivity extends AppCompatActivity { try { SmsVo smsVo = new SmsVo(getString(R.string.test_phone_num), getString(R.string.test_sender_sms), new Date(), getString(R.string.test_sim_info)); - SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, smsVo, "", ""); + SenderWebNotifyMsg.sendMsg(0, handler, null, webServer, webParams, secret, method, headers, smsVo, "", ""); } catch (Exception e) { ToastUtils.delayedShow(getString(R.string.failed_to_fwd) + e.getMessage(), 3000); e.printStackTrace(); diff --git a/app/src/main/java/com/idormy/sms/forwarder/model/vo/WebNotifySettingVo.java b/app/src/main/java/com/idormy/sms/forwarder/model/vo/WebNotifySettingVo.java index ef9cc0df..48bcf67a 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/model/vo/WebNotifySettingVo.java +++ b/app/src/main/java/com/idormy/sms/forwarder/model/vo/WebNotifySettingVo.java @@ -3,6 +3,7 @@ package com.idormy.sms.forwarder.model.vo; import com.idormy.sms.forwarder.R; import java.io.Serializable; +import java.util.Map; import lombok.Data; @@ -12,15 +13,17 @@ public class WebNotifySettingVo implements Serializable { private String secret; private String method; private String webParams; + private Map headers; public WebNotifySettingVo() { } - public WebNotifySettingVo(String webServer, String secret, String method, String webParams) { + public WebNotifySettingVo(String webServer, String secret, String method, String webParams, Map headers) { this.webServer = webServer; this.secret = secret; this.method = method; this.webParams = webParams; + this.headers = headers; } public int getWebNotifyMethodCheckId() { diff --git a/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java b/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java index 2e5c3df1..29231f24 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java +++ b/app/src/main/java/com/idormy/sms/forwarder/sender/SendUtil.java @@ -241,7 +241,7 @@ public class SendUtil { WebNotifySettingVo webNotifySettingVo = JSON.parseObject(senderModel.getJsonSetting(), WebNotifySettingVo.class); if (webNotifySettingVo != null) { try { - SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), smsVo, smsTemplate, regexReplace); + SenderWebNotifyMsg.sendMsg(logId, handError, retryInterceptor, webNotifySettingVo.getWebServer(), webNotifySettingVo.getWebParams(), webNotifySettingVo.getSecret(), webNotifySettingVo.getMethod(), webNotifySettingVo.getHeaders(), smsVo, smsTemplate, regexReplace); } catch (Exception e) { LogUtil.updateLog(logId, 0, e.getMessage()); Log.e(TAG, "senderSendMsg: SenderWebNotifyMsg error " + e.getMessage()); diff --git a/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java b/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java index 559dbf59..6e2e2c56 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java +++ b/app/src/main/java/com/idormy/sms/forwarder/sender/SenderWebNotifyMsg.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; +import java.util.Map; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -39,7 +40,7 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { static final String TAG = "SenderWebNotifyMsg"; - public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, SmsVo smsVo, String smsTemplate, String regexReplace) throws Exception { + public static void sendMsg(final long logId, final Handler handError, final RetryIntercepter retryInterceptor, String webServer, String webParams, String secret, String method, Map headers, SmsVo smsVo, String smsTemplate, String regexReplace) throws Exception { String from = smsVo.getMobile(); String content = smsVo.getSmsVoForSend(smsTemplate, regexReplace); Log.i(TAG, "sendMsg webServer:" + webServer + " webParams:" + webParams + " from:" + from + " content:" + content); @@ -65,7 +66,7 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { Log.i(TAG, "sign:" + sign); } - Request request; + Request.Builder requestBuilder; if (method.equals("GET") && TextUtils.isEmpty(webParams)) { webServer += (webServer.contains("?") ? "&" : "?") + "from=" + URLEncoder.encode(from, "UTF-8"); webServer += "&content=" + URLEncoder.encode(content, "UTF-8"); @@ -75,7 +76,7 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { } Log.d(TAG, "method = GET, Url = " + webServer); - request = new Request.Builder().url(webServer).get().build(); + requestBuilder = new Request.Builder().url(webServer).get(); } else if (method.equals("GET") && !TextUtils.isEmpty(webParams)) { webParams = webParams.replace("[from]", URLEncoder.encode(from, "UTF-8")) .replace("[content]", URLEncoder.encode(content, "UTF-8")) @@ -94,7 +95,7 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { webServer += (webServer.contains("?") ? "&" : "?") + webParams; Log.d(TAG, "method = GET, Url = " + webServer); - request = new Request.Builder().url(webServer).get().build(); + requestBuilder = new Request.Builder().url(webServer).get(); } else if (webParams != null && webParams.contains("[msg]")) { String bodyMsg; String contentType = "application/x-www-form-urlencoded"; @@ -121,11 +122,10 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { .replace("[receive_time]", URLEncoder.encode(receiveTime, "UTF-8")); } RequestBody body = RequestBody.create(MediaType.parse(contentType), bodyMsg); - request = new Request.Builder() + requestBuilder = new Request.Builder() .url(webServer) .addHeader("Content-Type", contentType) - .method("POST", body) - .build(); + .method("POST", body); Log.d(TAG, "method = POST webParams, Body = " + bodyMsg); } else { MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM) @@ -138,7 +138,11 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { RequestBody body = builder.build(); Log.d(TAG, "method = POST, Body = " + body); - request = new Request.Builder().url(webServer).method("POST", body).build(); + requestBuilder = new Request.Builder().url(webServer).method("POST", body); + } + + for (Map.Entry entry : headers.entrySet()) { + requestBuilder.addHeader(entry.getKey(), entry.getValue()); } OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); @@ -153,7 +157,7 @@ public class SenderWebNotifyMsg extends SenderBaseMsg { .connectTimeout(Define.REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) .build(); - client.newCall(request).enqueue(new Callback() { + client.newCall(requestBuilder.build()).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull final IOException e) { LogUtil.updateLog(logId, 0, e.getMessage()); diff --git a/app/src/main/res/drawable/ic_add_round_fill.xml b/app/src/main/res/drawable/ic_add_round_fill.xml new file mode 100644 index 00000000..bdb5e0a5 --- /dev/null +++ b/app/src/main/res/drawable/ic_add_round_fill.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/alert_dialog_setview_webnotify.xml b/app/src/main/res/layout/alert_dialog_setview_webnotify.xml index 099c17ab..c77114ab 100644 --- a/app/src/main/res/layout/alert_dialog_setview_webnotify.xml +++ b/app/src/main/res/layout/alert_dialog_setview_webnotify.xml @@ -169,6 +169,37 @@ app:showEye="true" /> + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index 7bc814b1..21d068f3 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -24,6 +24,11 @@ Clear logs Add rule Add sender + Headers + Key + Value + Add Header + Remove Headerh Version Check for updates diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3d4c5492..b512217b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,6 +24,11 @@ 清空日志 清空日志 添加发送通道 + Headers + Key + Value + 添加Header + 删除Header 当前版本 检查新版本