String body = XmlUtil.getCurrentLevelTextValue(messageElement, "Body");
String id = XmlUtil.getCurrentLevelTextValue(messageElement, "MessageId").trim();
String handle = XmlUtil.getCurrentLevelTextValue(messageElement, "ReceiptHandle").trim();
CQSMessage msg = new CQSMessage(id, body);
msg.setReceiptHandle(handle);
List<Element> attributeElements = XmlUtil.getCurrentLevelChildNodes(messageElement, "Attribute");
Map<String, String> attributes = new HashMap<String, String>();
for (Element attribute : attributeElements) {
String name = XmlUtil.getCurrentLevelTextValue(attribute, "Name");
String value = XmlUtil.getCurrentLevelTextValue(attribute, "Value");
if (name != null && value != null) {
attributes.put(name, value);
}
}
msg.setAttributes(attributes);
availableMessages.add(msg);
}
//retrieve first messageId from queue. this is for pagination
String peekRequestFirstMessageUrl = cqsServiceBaseUrl + user.getUserId() + "/" + queueName + "?Action=PeekMessage&AWSAccessKeyId=" + user.getAccessKey() + "&MaxNumberOfMessages=1&Shard=" + shard;
String peekFirstMessageXml = httpPOST(cqsServiceBaseUrl, peekRequestFirstMessageUrl,awsCredentials);
Element rootFirstMessage = XmlUtil.buildDoc(peekFirstMessageXml);
List<Element> messageElementsForFirstMessage = XmlUtil.getCurrentLevelChildNodes(XmlUtil.getCurrentLevelChildNodes(rootFirstMessage, "ReceiveMessageResult").get(0), "Message");
if (messageElementsForFirstMessage.size() == 1){
queueFirstMessageHandle = XmlUtil.getCurrentLevelTextValue(messageElementsForFirstMessage.get(0), "ReceiptHandle").trim();
}
}
} catch (Exception ex) {
logger.error("event=peek_message queue_url=" + queueUrl, ex);
throw new ServletException(ex);
}
String previousHandle = null;
nextHandle = null;
out.println("<p><hr width='100%' align='left' /><p>");
out.println("<h3>Available Messages</h3>");
if ((availableMessages==null) || (availableMessages.size()==0)) {
out.println("<p><i>no messages</i></p>");
}
for (int i = 0; availableMessages != null && i < availableMessages.size(); i++) {
CQSMessage message = availableMessages.get(i);
Map<String, String> attributes = message.getAttributes();
String timeSent = "";
if (attributes.get("SentTimestamp") != null) {
try { timeSent = new Date(Long.parseLong(attributes.get("SentTimestamp"))).toString(); } catch (Exception ex) {}
}
String timeReceived = "";
if (attributes.get("ApproximateFirstReceiveTimestamp") != null) {
try { timeReceived = new Date(Long.parseLong(attributes.get("ApproximateFirstReceiveTimestamp"))).toString(); } catch (Exception ex) {}
}
if (i == 0) {
out.println("<table class = 'alternatecolortable'>");
out.println("<tr><th></th><th>Receipt Handle</th><th>MD5</th><th>Body</th><th>Time Sent</th><th>Time First Received (Appr.)</th><th>Receive Count (Appr.)</th><th>Sender</th><th> </th></tr>");
previousHandle = message.getReceiptHandle();
}
out.println("<tr>");
out.println("<td>" + i + "</td>");
out.println("<td>" + message.getReceiptHandle() + "</td>");
out.println("<td>" + message.getMD5OfBody() + "</td>");
String messageBody=message.getBody();
String messageBodyPart1=null;
String messageBodyPart2=null;
if((messageBody!=null)&&(messageBody.length()>300)){
messageBodyPart1=messageBody.substring(0, 299);
messageBodyPart2=messageBody.substring(299);
out.println("<td>");
out.println(messageBodyPart1);
out.println("<div id='detail"+i+"' style=\"display: none;\">"+messageBodyPart2+"</div>");
out.println("<input type=button name=type id='bt"+i+"' value='More' onclick=\"setVisibility('detail"+ i+"', 'bt"+i+"');\";> ");
out.println("</td>");
} else {
out.println("<td>"+ message.getBody() + "</td>");
}
out.println("<td>"+ timeSent + "</td>");
out.println("<td>"+ timeReceived + "</td>");
out.println("<td>"+ attributes.get("ApproximateReceiveCount") + "</td>");
out.println("<td>"+ attributes.get("SenderId") + "</td>");
out.println("<td></td></tr>");
if (i == availableMessages.size() - 1) {
nextHandle = message.getReceiptHandle();
}
}
out.println("</table>");