String id = rand.nextLong() + "";
String rawEndPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id;
String rawEndPointLastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true";
SubscribeRequest rawEndPointSubscribeRequest = new SubscribeRequest();
rawEndPointSubscribeRequest.setEndpoint(rawEndPointUrl);
rawEndPointSubscribeRequest.setProtocol("http");
rawEndPointSubscribeRequest.setTopicArn(topicArn);
SubscribeResult subscribeResult = cns1.subscribe(rawEndPointSubscribeRequest);
String rawEndPointsubscriptionArn = subscribeResult.getSubscriptionArn();
if (rawEndPointsubscriptionArn.equals("pending confirmation")) {
Thread.sleep(500);
String response = CNSTestingUtils.sendHttpMessage(rawEndPointLastMessageUrl, "");
logger.info(response);
JSONObject o = new JSONObject(response);
if (!o.has("SubscribeURL")) {
throw new Exception("message is not a confirmation messsage");
}
String subscriptionUrl = o.getString("SubscribeURL");
response = CNSTestingUtils.sendHttpMessage(subscriptionUrl, "");
String startTag = "<SubscriptionArn>";
String endTag = "</SubscriptionArn>";
int startIndex = response.indexOf(startTag);
int endIndex = response.indexOf(endTag);
String subArn = response.substring(startIndex + startTag.length(), endIndex);
if (subArn != null && !subArn.isEmpty()) {
rawEndPointsubscriptionArn = subArn;
}
logger.info("Raw-message EndPoint subscription Arn after confirmation: " + rawEndPointsubscriptionArn);
}
// set subscription attribute for raw message delivery
Boolean rawMessageDelivery = true;
try {
SetSubscriptionAttributesRequest setSubscriptionAttributesRequest = new SetSubscriptionAttributesRequest(rawEndPointsubscriptionArn, "RawMessageDelivery", rawMessageDelivery.toString());
cns1.setSubscriptionAttributes(setSubscriptionAttributesRequest);
Map<String, String> attributes = null;
GetSubscriptionAttributesRequest getSubscriptionAttributesRequest = new GetSubscriptionAttributesRequest(rawEndPointsubscriptionArn);
GetSubscriptionAttributesResult getSubscriptionAttributesResult = cns1.getSubscriptionAttributes(getSubscriptionAttributesRequest);
attributes = getSubscriptionAttributesResult.getAttributes();
String rawMessageDeliveryStr = attributes.get("RawMessageDelivery");
if (rawMessageDeliveryStr != null && !rawMessageDeliveryStr.isEmpty()) {
rawMessageDelivery = Boolean.parseBoolean(rawMessageDeliveryStr);
assertTrue("Set raw message delivery successful", rawMessageDelivery);
} else {
fail("no raw message delivery flag found");
}
logger.info("Raw Message Delivery attribute:" + rawMessageDeliveryStr);
} catch (Exception ex) {
throw new Exception("Can't set raw message delivery attribute to subscription arn " + rawEndPointsubscriptionArn);
}
// subscribe and confirm http endpoint to receive JSON message
id = rand.nextLong() + "";
String jsonEndPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id;
String jsonEndPointLastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true";
SubscribeRequest jsonEndPointSubscribeRequest = new SubscribeRequest();
jsonEndPointSubscribeRequest.setEndpoint(jsonEndPointUrl);
jsonEndPointSubscribeRequest.setProtocol("http");
jsonEndPointSubscribeRequest.setTopicArn(topicArn);
SubscribeResult jsonSubscribeResult = cns1.subscribe(jsonEndPointSubscribeRequest);
String jsonEndPointsubscriptionArn = jsonSubscribeResult.getSubscriptionArn();
logger.info("JSON EndPoint subscription arn:" + jsonEndPointsubscriptionArn);
if (jsonEndPointsubscriptionArn.equals("pending confirmation")) {