Skip to content

Commit 694d294

Browse files
committed
feat: support review
1 parent 63d96ae commit 694d294

File tree

5 files changed

+146
-13
lines changed

5 files changed

+146
-13
lines changed

src/main/java/com/huawei/push/message/Message.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import com.huawei.push.util.CollectionUtils;
2222
import com.huawei.push.util.ValidatorUtils;
2323

24-
import org.apache.commons.lang3.StringUtils;
25-
2624
import java.util.ArrayList;
2725
import java.util.List;
2826

@@ -157,6 +155,7 @@ public static class Builder {
157155
private List<String> token = new ArrayList<>();
158156
private String topic;
159157
private String condition;
158+
private List<Review> review;
160159

161160
private Builder() {
162161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.huawei.push.message;
17+
18+
import com.alibaba.fastjson.annotation.JSONField;
19+
import com.huawei.push.util.ValidatorUtils;
20+
import org.apache.commons.lang3.StringUtils;
21+
22+
public class Review {
23+
@JSONField(name = "reviewer")
24+
private String reviewer;
25+
26+
@JSONField(name = "type")
27+
private Integer type;
28+
29+
@JSONField(name = "result")
30+
private Object result;
31+
32+
public void check() {
33+
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.reviewer), "reviewer should not be empty");
34+
ValidatorUtils.checkArgument(type != null, "type should not be empty");
35+
ValidatorUtils.checkArgument(result != null, "result should not be empty");
36+
}
37+
38+
public Review() {
39+
}
40+
41+
public Review(String reviewer, Integer type, Object result) {
42+
this.reviewer = reviewer;
43+
this.type = type;
44+
this.result = result;
45+
}
46+
47+
private Review(Builder builder) {
48+
this.reviewer = builder.reviewer;
49+
this.type = builder.type;
50+
this.result = builder.result;
51+
}
52+
53+
public String getReviewer() {
54+
return reviewer;
55+
}
56+
57+
public void setReviewer(String reviewer) {
58+
this.reviewer = reviewer;
59+
}
60+
61+
public Integer getType() {
62+
return type;
63+
}
64+
65+
public void setType(Integer type) {
66+
this.type = type;
67+
}
68+
69+
public Object getResult() {
70+
return result;
71+
}
72+
73+
public void setResult(Object result) {
74+
this.result = result;
75+
}
76+
77+
// Builder class
78+
public static class Builder {
79+
private String reviewer;
80+
private Integer type;
81+
private Object result;
82+
83+
public Builder() {
84+
}
85+
86+
public Review.Builder setReviewer(String reviewer) {
87+
this.reviewer = reviewer;
88+
return this;
89+
}
90+
91+
public Review.Builder setType(Integer type) {
92+
this.type = type;
93+
return this;
94+
}
95+
96+
public Review.Builder setResult(Object result) {
97+
this.result = result;
98+
return this;
99+
}
100+
101+
public Review build() {
102+
return new Review(this);
103+
}
104+
}
105+
}

src/main/java/com/huawei/push/messaging/HuaweiMessageClient.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
import com.huawei.push.exception.HuaweiMesssagingException;
1919
import com.huawei.push.message.Message;
20+
import com.huawei.push.message.Review;
2021
import com.huawei.push.message.TopicMessage;
2122
import com.huawei.push.reponse.SendResponse;
2223

24+
import java.util.List;
25+
2326
/**
2427
* sending messages interface
2528
*/
@@ -30,10 +33,11 @@ public interface HuaweiMessageClient {
3033
*
3134
* @param message message {@link Message}
3235
* @param validateOnly A boolean indicating whether to send message for test. or not.
36+
* @param review A list of {@link Review} objects.
3337
* @return {@link SendResponse}.
3438
* @throws HuaweiMesssagingException
3539
*/
36-
SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException;
40+
SendResponse send(Message message, boolean validateOnly, List<Review> review, String accessToken) throws HuaweiMesssagingException;
3741

3842
SendResponse send(TopicMessage message, String operation, String accessToken) throws HuaweiMesssagingException;
3943
}

src/main/java/com/huawei/push/messaging/HuaweiMessageClientImpl.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.alibaba.fastjson.JSONObject;
2121
import com.huawei.push.exception.HuaweiMesssagingException;
2222
import com.huawei.push.message.Message;
23+
import com.huawei.push.message.Review;
2324
import com.huawei.push.message.TopicMessage;
2425
import com.huawei.push.model.TopicOperation;
2526
import com.huawei.push.reponse.SendResponse;
@@ -37,6 +38,7 @@
3738
import java.io.IOException;
3839
import java.text.MessageFormat;
3940
import java.util.HashMap;
41+
import java.util.List;
4042
import java.util.Map;
4143
import java.util.ResourceBundle;
4244

@@ -69,9 +71,9 @@ public CloseableHttpClient getHttpClient() {
6971
}
7072

7173
@Override
72-
public SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException {
74+
public SendResponse send(Message message, boolean validateOnly, List<Review> review, String accessToken) throws HuaweiMesssagingException {
7375
try {
74-
return sendRequest(message, validateOnly, accessToken);
76+
return sendRequest(message, validateOnly, review, accessToken);
7577
} catch (IOException e) {
7678
throw new HuaweiMesssagingException(HuaweiMessaging.INTERNAL_ERROR, "Error while calling HCM backend service", e);
7779
}
@@ -125,14 +127,14 @@ private SendResponse sendRequest(TopicMessage message, String operation, String
125127
/**
126128
* send request
127129
*
128-
* @param message message {@link Message}
130+
* @param message message {@link Message}
129131
* @param validateOnly A boolean indicating whether to send message for test or not.
130132
* @param accessToken A String for oauth
131133
* @return {@link SendResponse}
132134
* @throws IOException If a error occurs when sending request
133135
*/
134-
private SendResponse sendRequest(Message message, boolean validateOnly, String accessToken) throws IOException, HuaweiMesssagingException {
135-
Map<String, Object> map = createRequestMap(message, validateOnly);
136+
private SendResponse sendRequest(Message message, boolean validateOnly, List<Review> review, String accessToken) throws IOException, HuaweiMesssagingException {
137+
Map<String, Object> map = createRequestMap(message, validateOnly, review);
136138
HttpPost httpPost = new HttpPost(this.HcmPushUrl);
137139
StringEntity entity = new StringEntity(JSON.toJSONString(map), "UTF-8");
138140
// String aqa = JSON.toJSONString(map);
@@ -163,13 +165,17 @@ private SendResponse sendRequest(Message message, boolean validateOnly, String a
163165
*
164166
* @param message A non-null {@link Message} to be sent.
165167
* @param validateOnly A boolean indicating whether to send message for test or not.
168+
* @param review A list of {@link Review} objects.
166169
* @return a map of request
167170
*/
168-
private Map<String, Object> createRequestMap(Message message, boolean validateOnly) {
171+
private Map<String, Object> createRequestMap(Message message, boolean validateOnly, List<Review> review) {
169172
return new HashMap<String, Object>() {
170173
{
171174
put("validate_only", validateOnly);
172175
put("message", message);
176+
if (review != null && review.size() > 0) {
177+
put("review", review);
178+
}
173179
}
174180
};
175181
}

src/main/java/com/huawei/push/messaging/HuaweiMessaging.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
import com.google.common.base.Suppliers;
2020
import com.huawei.push.exception.HuaweiMesssagingException;
2121
import com.huawei.push.message.Message;
22+
import com.huawei.push.message.Review;
2223
import com.huawei.push.message.TopicMessage;
2324
import com.huawei.push.model.TopicOperation;
2425
import com.huawei.push.reponse.SendResponse;
2526
import com.huawei.push.util.ValidatorUtils;
26-
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

30+
import java.util.List;
31+
3032
/**
3133
* This class is the entrance for all server-side HCM actions.
3234
*
@@ -83,7 +85,7 @@ HuaweiMessageClient getMessagingClient() {
8385
* delivery.
8486
*/
8587
public SendResponse sendMessage(Message message) throws HuaweiMesssagingException {
86-
return sendMessage(message, false);
88+
return sendMessage(message, false, null);
8789
}
8890

8991
/**
@@ -116,7 +118,6 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging
116118
return messagingClient.send(topicMessage, TopicOperation.LIST.getValue(), ImplHuaweiTrampolines.getAccessToken(app));
117119
}
118120

119-
120121
/**
121122
* Sends message {@link Message}
122123
*
@@ -131,7 +132,25 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging
131132
public SendResponse sendMessage(Message message, boolean validateOnly) throws HuaweiMesssagingException {
132133
ValidatorUtils.checkArgument(message != null, "message must not be null");
133134
final HuaweiMessageClient messagingClient = getMessagingClient();
134-
return messagingClient.send(message, validateOnly, ImplHuaweiTrampolines.getAccessToken(app));
135+
return messagingClient.send(message, validateOnly, null, ImplHuaweiTrampolines.getAccessToken(app));
136+
}
137+
138+
/**
139+
* Sends message {@link Message}
140+
*
141+
* <p>If the {@code validateOnly} option is set to true, the message will not be actually sent. Instead
142+
* HCM performs all the necessary validations, and emulates the send operation.
143+
*
144+
* @param message message {@link Message} to be sent.
145+
* @param validateOnly a boolean indicating whether to send message for test or not.
146+
* @param review A list of {@link Review} objects.
147+
* @return {@link SendResponse}.
148+
* @throws HuaweiMesssagingException exception.
149+
*/
150+
public SendResponse sendMessage(Message message, boolean validateOnly, List<Review> review) throws HuaweiMesssagingException {
151+
ValidatorUtils.checkArgument(message != null, "message must not be null");
152+
final HuaweiMessageClient messagingClient = getMessagingClient();
153+
return messagingClient.send(message, validateOnly, review, ImplHuaweiTrampolines.getAccessToken(app));
135154
}
136155

137156
/**

0 commit comments

Comments
 (0)