diff --git a/src/main/java/com/huawei/push/message/Message.java b/src/main/java/com/huawei/push/message/Message.java index 8217eb0..2075a8f 100644 --- a/src/main/java/com/huawei/push/message/Message.java +++ b/src/main/java/com/huawei/push/message/Message.java @@ -21,8 +21,6 @@ import com.huawei.push.util.CollectionUtils; import com.huawei.push.util.ValidatorUtils; -import org.apache.commons.lang3.StringUtils; - import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/huawei/push/message/Review.java b/src/main/java/com/huawei/push/message/Review.java new file mode 100644 index 0000000..e67fd8e --- /dev/null +++ b/src/main/java/com/huawei/push/message/Review.java @@ -0,0 +1,105 @@ +/* + * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package com.huawei.push.message; + +import com.alibaba.fastjson.annotation.JSONField; +import com.huawei.push.util.ValidatorUtils; +import org.apache.commons.lang3.StringUtils; + +public class Review { + @JSONField(name = "reviewer") + private String reviewer; + + @JSONField(name = "type") + private Integer type; + + @JSONField(name = "result") + private Object result; + + public void check() { + ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.reviewer), "reviewer should not be empty"); + ValidatorUtils.checkArgument(type != null, "type should not be empty"); + ValidatorUtils.checkArgument(result != null, "result should not be empty"); + } + + public Review() { + } + + public Review(String reviewer, Integer type, Object result) { + this.reviewer = reviewer; + this.type = type; + this.result = result; + } + + private Review(Builder builder) { + this.reviewer = builder.reviewer; + this.type = builder.type; + this.result = builder.result; + } + + public String getReviewer() { + return reviewer; + } + + public void setReviewer(String reviewer) { + this.reviewer = reviewer; + } + + public Integer getType() { + return type; + } + + public void setType(Integer type) { + this.type = type; + } + + public Object getResult() { + return result; + } + + public void setResult(Object result) { + this.result = result; + } + + // Builder class + public static class Builder { + private String reviewer; + private Integer type; + private Object result; + + public Builder() { + } + + public Review.Builder setReviewer(String reviewer) { + this.reviewer = reviewer; + return this; + } + + public Review.Builder setType(Integer type) { + this.type = type; + return this; + } + + public Review.Builder setResult(Object result) { + this.result = result; + return this; + } + + public Review build() { + return new Review(this); + } + } +} diff --git a/src/main/java/com/huawei/push/messaging/HuaweiMessageClient.java b/src/main/java/com/huawei/push/messaging/HuaweiMessageClient.java index 4955c98..f1ceff0 100644 --- a/src/main/java/com/huawei/push/messaging/HuaweiMessageClient.java +++ b/src/main/java/com/huawei/push/messaging/HuaweiMessageClient.java @@ -17,9 +17,12 @@ import com.huawei.push.exception.HuaweiMesssagingException; import com.huawei.push.message.Message; +import com.huawei.push.message.Review; import com.huawei.push.message.TopicMessage; import com.huawei.push.reponse.SendResponse; +import java.util.List; + /** * sending messages interface */ @@ -30,10 +33,11 @@ public interface HuaweiMessageClient { * * @param message message {@link Message} * @param validateOnly A boolean indicating whether to send message for test. or not. + * @param review A list of {@link Review} objects. * @return {@link SendResponse}. * @throws HuaweiMesssagingException */ - SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException; + SendResponse send(Message message, boolean validateOnly, List review, String accessToken) throws HuaweiMesssagingException; SendResponse send(TopicMessage message, String operation, String accessToken) throws HuaweiMesssagingException; } diff --git a/src/main/java/com/huawei/push/messaging/HuaweiMessageClientImpl.java b/src/main/java/com/huawei/push/messaging/HuaweiMessageClientImpl.java index dd2c0c5..5f597df 100644 --- a/src/main/java/com/huawei/push/messaging/HuaweiMessageClientImpl.java +++ b/src/main/java/com/huawei/push/messaging/HuaweiMessageClientImpl.java @@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONObject; import com.huawei.push.exception.HuaweiMesssagingException; import com.huawei.push.message.Message; +import com.huawei.push.message.Review; import com.huawei.push.message.TopicMessage; import com.huawei.push.model.TopicOperation; import com.huawei.push.reponse.SendResponse; @@ -37,6 +38,7 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.ResourceBundle; @@ -69,9 +71,9 @@ public CloseableHttpClient getHttpClient() { } @Override - public SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException { + public SendResponse send(Message message, boolean validateOnly, List review, String accessToken) throws HuaweiMesssagingException { try { - return sendRequest(message, validateOnly, accessToken); + return sendRequest(message, validateOnly, review, accessToken); } catch (IOException e) { throw new HuaweiMesssagingException(HuaweiMessaging.INTERNAL_ERROR, "Error while calling HCM backend service", e); } @@ -125,14 +127,14 @@ private SendResponse sendRequest(TopicMessage message, String operation, String /** * send request * - * @param message message {@link Message} + * @param message message {@link Message} * @param validateOnly A boolean indicating whether to send message for test or not. * @param accessToken A String for oauth * @return {@link SendResponse} * @throws IOException If a error occurs when sending request */ - private SendResponse sendRequest(Message message, boolean validateOnly, String accessToken) throws IOException, HuaweiMesssagingException { - Map map = createRequestMap(message, validateOnly); + private SendResponse sendRequest(Message message, boolean validateOnly, List review, String accessToken) throws IOException, HuaweiMesssagingException { + Map map = createRequestMap(message, validateOnly, review); HttpPost httpPost = new HttpPost(this.HcmPushUrl); StringEntity entity = new StringEntity(JSON.toJSONString(map), "UTF-8"); // String aqa = JSON.toJSONString(map); @@ -163,13 +165,17 @@ private SendResponse sendRequest(Message message, boolean validateOnly, String a * * @param message A non-null {@link Message} to be sent. * @param validateOnly A boolean indicating whether to send message for test or not. + * @param review A list of {@link Review} objects. * @return a map of request */ - private Map createRequestMap(Message message, boolean validateOnly) { + private Map createRequestMap(Message message, boolean validateOnly, List review) { return new HashMap() { { put("validate_only", validateOnly); put("message", message); + if (review != null && review.size() > 0) { + put("review", review); + } } }; } diff --git a/src/main/java/com/huawei/push/messaging/HuaweiMessaging.java b/src/main/java/com/huawei/push/messaging/HuaweiMessaging.java index 48596bb..e5968f6 100644 --- a/src/main/java/com/huawei/push/messaging/HuaweiMessaging.java +++ b/src/main/java/com/huawei/push/messaging/HuaweiMessaging.java @@ -19,14 +19,16 @@ import com.google.common.base.Suppliers; import com.huawei.push.exception.HuaweiMesssagingException; import com.huawei.push.message.Message; +import com.huawei.push.message.Review; import com.huawei.push.message.TopicMessage; import com.huawei.push.model.TopicOperation; import com.huawei.push.reponse.SendResponse; import com.huawei.push.util.ValidatorUtils; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + /** * This class is the entrance for all server-side HCM actions. * @@ -83,7 +85,7 @@ HuaweiMessageClient getMessagingClient() { * delivery. */ public SendResponse sendMessage(Message message) throws HuaweiMesssagingException { - return sendMessage(message, false); + return sendMessage(message, false, null); } /** @@ -116,7 +118,6 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging return messagingClient.send(topicMessage, TopicOperation.LIST.getValue(), ImplHuaweiTrampolines.getAccessToken(app)); } - /** * Sends message {@link Message} * @@ -131,7 +132,25 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging public SendResponse sendMessage(Message message, boolean validateOnly) throws HuaweiMesssagingException { ValidatorUtils.checkArgument(message != null, "message must not be null"); final HuaweiMessageClient messagingClient = getMessagingClient(); - return messagingClient.send(message, validateOnly, ImplHuaweiTrampolines.getAccessToken(app)); + return messagingClient.send(message, validateOnly, null, ImplHuaweiTrampolines.getAccessToken(app)); + } + + /** + * Sends message {@link Message} + * + *

If the {@code validateOnly} option is set to true, the message will not be actually sent. Instead + * HCM performs all the necessary validations, and emulates the send operation. + * + * @param message message {@link Message} to be sent. + * @param validateOnly a boolean indicating whether to send message for test or not. + * @param review A list of {@link Review} objects. + * @return {@link SendResponse}. + * @throws HuaweiMesssagingException exception. + */ + public SendResponse sendMessage(Message message, boolean validateOnly, List review) throws HuaweiMesssagingException { + ValidatorUtils.checkArgument(message != null, "message must not be null"); + final HuaweiMessageClient messagingClient = getMessagingClient(); + return messagingClient.send(message, validateOnly, review, ImplHuaweiTrampolines.getAccessToken(app)); } /**