use of nl.nn.adapterframework.core.IMessageBrowsingIterator in project iaf by ibissource.
the class BrowseQueue method putBrowseQueue.
@POST
@RolesAllowed({ "IbisTester" })
@Path("jms/browse")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response putBrowseQueue(LinkedHashMap<String, Object> json) throws ApiException {
initBase(servletConfig);
Map<String, Object> returnMap = new HashMap<String, Object>();
String jmsRealm = null, destination = null, type = null;
for (Entry<String, Object> entry : json.entrySet()) {
String key = entry.getKey();
if (key.equalsIgnoreCase("realm")) {
jmsRealm = entry.getValue().toString();
}
if (key.equalsIgnoreCase("destination")) {
destination = entry.getValue().toString();
}
if (key.equalsIgnoreCase("type")) {
type = entry.getValue().toString();
}
}
if (jmsRealm == null)
throw new ApiException("No realm provided");
if (destination == null)
throw new ApiException("No destination provided");
if (type == null)
throw new ApiException("No type provided");
IMessageBrowsingIterator it = null;
try {
JmsMessageBrowser jmsBrowser = new JmsMessageBrowser();
jmsBrowser.setName("BrowseQueueAction");
jmsBrowser.setJmsRealm(jmsRealm);
jmsBrowser.setDestinationName(destination);
jmsBrowser.setDestinationType(type);
IMessageBrowser browser = jmsBrowser;
it = browser.getIterator();
List<Map<String, Object>> messages = new ArrayList<Map<String, Object>>();
while (it.hasNext()) {
IMessageBrowsingIteratorItem item = it.next();
Map<String, Object> message = new HashMap<String, Object>();
message.put("comment", item.getCommentString());
message.put("correlationId", item.getCorrelationId());
message.put("expiryDate", item.getExpiryDate());
message.put("host", item.getHost());
message.put("id", item.getId());
message.put("insertDate", item.getInsertDate());
message.put("type", item.getType());
message.put("label", item.getLabel());
messages.add(message);
}
log.debug("Browser returned " + messages.size() + " messages");
returnMap.put("numberOfMessages", messages.size());
returnMap.put("messages", messages);
} catch (Exception e) {
throw new ApiException("Error occured browsing messages: " + e.getMessage());
} finally {
try {
if (it != null)
it.close();
} catch (ListenerException e) {
log.error(e);
}
}
return Response.status(Response.Status.OK).entity(returnMap).build();
}
use of nl.nn.adapterframework.core.IMessageBrowsingIterator in project iaf by ibissource.
the class Browse method executeSub.
public ActionForward executeSub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Initialize action
initAction(request);
DynaActionForm browseForm = getPersistentForm(mapping, form, request);
String submit = request.getParameter("submit");
log.debug("submit param [" + submit + "]");
String maxMessagesStr = getAndSetProperty(request, browseForm, "maxMessages", getMaxMessages() + "");
String skipMessagesStr = getAndSetProperty(request, browseForm, "skipMessages", "0");
String action = getAndSetProperty(request, browseForm, "action");
String storageType = getAndSetProperty(request, browseForm, "storageType");
String adapterName = getAndSetProperty(request, browseForm, "adapterName");
String receiverName = getAndSetProperty(request, browseForm, "receiverName");
String pipeName = getAndSetProperty(request, browseForm, "pipeName");
String messageId = getAndSetProperty(request, browseForm, "messageId");
String typeMask = getAndSetProperty(request, browseForm, "typeMask");
String hostMask = getAndSetProperty(request, browseForm, "hostMask");
String currentIdMask = getAndSetProperty(request, browseForm, "currentIdMask");
String messageIdMask = getAndSetProperty(request, browseForm, "messageIdMask");
String correlationIdMask = getAndSetProperty(request, browseForm, "correlationIdMask");
String commentMask = getAndSetProperty(request, browseForm, "commentMask");
String messageTextMask = getAndSetProperty(request, browseForm, "messageTextMask");
String labelMask = getAndSetProperty(request, browseForm, "labelMask");
String startDateStr = getAndSetProperty(request, browseForm, "insertedAfter");
String startDateClipStr = getAndSetProperty(request, browseForm, "insertedAfterClip");
// not yet supported in actionForm
String endDateStr = request.getParameter("insertedBefore");
// not yet supported in actionForm
String forceDescStr = request.getParameter("forceDescending");
String viewAs = getAndSetProperty(request, browseForm, "viewAs", request.getParameter("type"));
String[] selected = (String[]) browseForm.get("selected");
boolean startDateClip = "on".equals(startDateClipStr);
if (StringUtils.isNotEmpty(submit)) {
action = submit;
}
Date startDate = null;
Date endDate = null;
String formattedStartDate = null;
if (StringUtils.isNotEmpty(startDateStr)) {
try {
startDate = DateUtils.parseAnyDate(startDateStr);
if (startDate != null) {
formattedStartDate = DateUtils.formatOptimal(startDate);
log.debug("parsed start date to [" + formattedStartDate + "]");
browseForm.set("insertedAfter", formattedStartDate);
if (startDateClip) {
endDate = DateUtils.nextHigherValue(startDate);
}
} else {
warn("could not parse date from [" + startDateStr + "]");
}
} catch (CalendarParserException e) {
warn("could not parse date from [" + startDateStr + "]", e);
}
}
if (StringUtils.isNotEmpty(endDateStr)) {
try {
endDate = DateUtils.parseAnyDate(endDateStr);
if (startDate == null) {
warn("could not parse date from [" + endDateStr + "]");
}
} catch (CalendarParserException e) {
warn("could not parse date from [" + startDateStr + "]", e);
}
}
ArrayList viewAsList = new ArrayList();
viewAsList.add("html");
viewAsList.add("text");
browseForm.set("viewAsList", viewAsList);
log.debug("selected [" + browseForm.get("selected") + "]");
// ArrayList selected=(ArrayList)browseForm.get("selected");
// for (int i=0; i<selected.size(); i++) {
// log.debug("selected "+i+" = ["+selected.get(i));
// }
maxMessages = Integer.parseInt(maxMessagesStr);
skipMessages = Integer.parseInt(skipMessagesStr);
// commandIssuedBy containes information about the location the
// command is sent from
String commandIssuedBy = HttpUtils.getCommandIssuedBy(request);
log.debug("storageType [" + storageType + "] action [" + action + "] submit [" + submit + "] adapterName [" + adapterName + "] receiverName [" + receiverName + "] pipeName [" + pipeName + "] issued by [" + commandIssuedBy + "]");
Adapter adapter = (Adapter) ibisManager.getRegisteredAdapter(adapterName);
IMessageBrowser mb;
IListener listener = null;
String logCount;
if ("messagelog".equals(storageType)) {
if (StringUtils.isNotEmpty(pipeName)) {
MessageSendingPipe pipe = (MessageSendingPipe) adapter.getPipeLine().getPipe(pipeName);
mb = pipe.getMessageLog();
} else {
ReceiverBase receiver = (ReceiverBase) adapter.getReceiverByName(receiverName);
mb = receiver.getMessageLog();
}
// actions 'deletemessage' and 'resendmessage' not allowed for messageLog
if ("export selected".equalsIgnoreCase(action)) {
performAction(adapter, null, action, mb, messageId, selected, request, response);
}
} else {
ReceiverBase receiver = (ReceiverBase) adapter.getReceiverByName(receiverName);
if (receiver == null) {
error("cannot find Receiver [" + receiverName + "]", null);
return null;
}
mb = receiver.getErrorStorage();
if (performAction(adapter, receiver, action, mb, messageId, selected, request, response))
return null;
listener = receiver.getListener();
}
try {
logCount = "(" + ((ITransactionalStorage) mb).getMessageCount() + ")";
} catch (Exception e) {
log.warn(e);
logCount = "(?)";
}
try {
if ("showmessage".equalsIgnoreCase(action)) {
Object rawmsg = mb.browseMessage(messageId);
String msg = null;
if (listener != null) {
msg = listener.getStringFromRawMessage(rawmsg, null);
} else {
msg = (String) rawmsg;
}
if (StringUtils.isEmpty(msg)) {
msg = "<no message found>";
}
String type = request.getParameter("type");
if (StringUtils.isEmpty(type)) {
type = viewAs;
}
FileViewerServlet.showReaderContents(new StringReader(msg), "msg" + messageId, type, response, "message [" + messageId + "]");
return null;
} else {
IMessageBrowsingIterator mbi = mb.getIterator(startDate, endDate, "true".equals(forceDescStr));
try {
XmlBuilder messages = new XmlBuilder("messages");
messages.addAttribute("storageType", storageType);
messages.addAttribute("action", action);
messages.addAttribute("adapterName", XmlUtils.encodeChars(adapterName));
if ("messagelog".equals(storageType) && StringUtils.isNotEmpty(pipeName)) {
messages.addAttribute("object", "pipe [" + XmlUtils.encodeChars(pipeName) + "] of adapter [" + XmlUtils.encodeChars(adapterName) + "] " + logCount);
messages.addAttribute("pipeName", XmlUtils.encodeChars(pipeName));
} else {
messages.addAttribute("object", "receiver [" + XmlUtils.encodeChars(receiverName) + "] of adapter [" + XmlUtils.encodeChars(adapterName) + "] " + logCount);
messages.addAttribute("receiverName", XmlUtils.encodeChars(receiverName));
}
int messageCount;
for (messageCount = 0; mbi.hasNext(); ) {
IMessageBrowsingIteratorItem iterItem = mbi.next();
try {
String cType = iterItem.getType();
String cHost = iterItem.getHost();
String cId = iterItem.getId();
String cMessageId = iterItem.getOriginalId();
String cCorrelationId = iterItem.getCorrelationId();
String comment = iterItem.getCommentString();
Date insertDate = iterItem.getInsertDate();
String cLabel = iterItem.getLabel();
if (StringUtils.isNotEmpty(typeMask) && !cType.startsWith(typeMask)) {
continue;
}
if (StringUtils.isNotEmpty(hostMask) && !cHost.startsWith(hostMask)) {
continue;
}
if (StringUtils.isNotEmpty(currentIdMask) && !cId.startsWith(currentIdMask)) {
continue;
}
if (StringUtils.isNotEmpty(messageIdMask) && !cMessageId.startsWith(messageIdMask)) {
continue;
}
if (StringUtils.isNotEmpty(correlationIdMask) && !cCorrelationId.startsWith(correlationIdMask)) {
continue;
}
if (startDate != null && insertDate != null) {
if (insertDate.before(startDate)) {
continue;
}
if (startDateClip) {
String formattedInsertDate = DateUtils.formatOptimal(insertDate);
if (!formattedInsertDate.startsWith(formattedStartDate)) {
continue;
}
}
}
if (StringUtils.isNotEmpty(commentMask) && (StringUtils.isEmpty(comment) || comment.indexOf(commentMask) < 0)) {
continue;
}
if (StringUtils.isNotEmpty(messageTextMask)) {
Object rawmsg = mb.browseMessage(cId);
String msg = null;
if (listener != null) {
msg = listener.getStringFromRawMessage(rawmsg, new HashMap());
} else {
msg = (String) rawmsg;
}
if (msg == null || msg.indexOf(messageTextMask) < 0) {
continue;
}
}
if (StringUtils.isNotEmpty(labelMask) && (StringUtils.isEmpty(cLabel) || !cLabel.startsWith(labelMask))) {
continue;
}
messageCount++;
if (messageCount > skipMessages) {
XmlBuilder message = new XmlBuilder("message");
message.addAttribute("id", cId);
message.addAttribute("pos", Integer.toString(messageCount));
message.addAttribute("originalId", cMessageId);
message.addAttribute("correlationId", cCorrelationId);
message.addAttribute("type", cType);
message.addAttribute("host", cHost);
message.addAttribute("insertDate", DateUtils.format(insertDate, DateUtils.FORMAT_FULL_GENERIC));
if (iterItem.getExpiryDate() != null) {
message.addAttribute("expiryDate", DateUtils.format(iterItem.getExpiryDate(), DateUtils.FORMAT_FULL_GENERIC));
}
message.addAttribute("comment", XmlUtils.encodeChars(iterItem.getCommentString()));
message.addAttribute("label", cLabel);
messages.addSubElement(message);
}
if (getMaxMessages() > 0 && messageCount >= (getMaxMessages() + skipMessages)) {
log.warn("stopped iterating messages after [" + messageCount + "]: limit reached");
break;
}
} finally {
iterItem.release();
}
}
messages.addAttribute("messageCount", Integer.toString(messageCount - skipMessages));
request.setAttribute("messages", messages.toXML());
} finally {
mbi.close();
}
}
} catch (Throwable e) {
error("Caught Exception", e);
throw new ServletException(e);
}
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
log.debug("forward to success");
return (mapping.findForward("success"));
}
use of nl.nn.adapterframework.core.IMessageBrowsingIterator in project iaf by ibissource.
the class BrowseQueueExecute method executeSub.
public ActionForward executeSub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Initialize action
initAction(request);
// -------------------------------
if (isCancelled(request)) {
log.debug("browseQueue was cancelled");
removeFormBean(mapping, request);
return (mapping.findForward("cancel"));
}
// Retrieve form content
// ---------------------
IniDynaActionForm browseQueueForm = (IniDynaActionForm) form;
String form_jmsRealm = (String) browseQueueForm.get("jmsRealm");
String form_destinationName = (String) browseQueueForm.get("destinationName");
String form_destinationType = (String) browseQueueForm.get("destinationType");
boolean form_numberOfMessagesOnly = false;
boolean form_showPayload = false;
if (browseQueueForm.get("numberOfMessagesOnly") != null)
form_numberOfMessagesOnly = ((Boolean) browseQueueForm.get("numberOfMessagesOnly")).booleanValue();
if (browseQueueForm.get("showPayload") != null)
form_showPayload = ((Boolean) browseQueueForm.get("showPayload")).booleanValue();
// initiate MessageSender
JmsMessageBrowser jmsBrowser = new JmsMessageBrowser();
jmsBrowser.setName("BrowseQueueAction");
jmsBrowser.setJmsRealm(form_jmsRealm);
jmsBrowser.setDestinationName(form_destinationName);
jmsBrowser.setDestinationType(form_destinationType);
IMessageBrowser browser = jmsBrowser;
IMessageBrowsingIterator it = null;
try {
it = browser.getIterator();
List messages = new ArrayList();
while (it.hasNext()) {
messages.add(it.next());
}
log.debug("Browser returned " + messages.size() + " messages");
browseQueueForm.set("numberOfMessages", Integer.toString(messages.size()));
if (!form_numberOfMessagesOnly) {
/*
try {
for (int i = 0; i < messages.size(); i++) {
Message msg = (Message) messages.get(i);
if (msg instanceof TextMessage) {
TextMessage tm = (TextMessage) msg;
if (log.isDebugEnabled())
log.debug("Found message " + tm.getText());
}
}
} catch (JMSException je) {
log.error(je);
errors.add(
"",
new ActionError(
"errors.generic",
"error occured browsing messages:" + je.getMessage()));
}
*/
browseQueueForm.set("messages", messages);
} else
browseQueueForm.set("messages", new ArrayList());
} catch (ListenerException e) {
error("Error occured browsing messages", e);
} finally {
try {
if (it != null) {
it.close();
}
} catch (ListenerException e1) {
log.error(e1);
}
}
// Report any errors we have discovered back to the original form
if (!errors.isEmpty()) {
StoreFormData(browseQueueForm);
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// Successfull: store cookie
String cookieValue = "";
cookieValue += "jmsRealm=\"" + form_jmsRealm + "\"";
// separator
cookieValue += " ";
cookieValue += "destinationName=\"" + form_destinationName + "\"";
// separator
cookieValue += " ";
cookieValue += "destinationType=\"" + form_destinationType + "\"";
// separator
cookieValue += " ";
cookieValue += "showPayload=\"" + form_showPayload + "\"";
log.debug("*** value : " + AppConstants.getInstance().getString("WEB_QBROWSECOOKIE_NAME", "WEB_QBROWSECOOKIE"));
Cookie sendJmsCookie = new Cookie(AppConstants.getInstance().getString("WEB_QBROWSECOOKIE_NAME", "WEB_QBROWSECOOKIE"), cookieValue);
sendJmsCookie.setMaxAge(Integer.MAX_VALUE);
log.debug("Store cookie for " + request.getServletPath() + " cookieName[" + sendJmsCookie.getName() + "] " + " cookieValue[" + new StringTagger(cookieValue).toString() + "]");
try {
response.addCookie(sendJmsCookie);
} catch (Throwable e) {
log.warn("unable to add cookie to request. cookie value [" + sendJmsCookie.getValue() + "]");
}
// Forward control to the specified success URI
log.debug("forward to success");
return (mapping.findForward("success"));
}
use of nl.nn.adapterframework.core.IMessageBrowsingIterator in project iaf by ibissource.
the class XmlJmsBrowserSender method sendMessage.
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
Element queueBrowserElement;
String root = null;
String jmsRealm = null;
String queueConnectionFactoryName = null;
String destinationName = null;
String destinationType = null;
try {
queueBrowserElement = XmlUtils.buildElement(message);
root = queueBrowserElement.getTagName();
jmsRealm = XmlUtils.getChildTagAsString(queueBrowserElement, "jmsRealm");
queueConnectionFactoryName = XmlUtils.getChildTagAsString(queueBrowserElement, "queueConnectionFactoryName");
destinationName = XmlUtils.getChildTagAsString(queueBrowserElement, "destinationName");
destinationType = XmlUtils.getChildTagAsString(queueBrowserElement, "destinationType");
} catch (DomBuilderException e) {
throw new SenderException(getLogPrefix() + "got exception parsing [" + message + "]", e);
}
JmsMessageBrowser jmsBrowser = new JmsMessageBrowser();
jmsBrowser.setName("XmlQueueBrowserSender");
if (jmsRealm != null) {
jmsBrowser.setJmsRealm(jmsRealm);
}
if (queueConnectionFactoryName != null) {
jmsBrowser.setQueueConnectionFactoryName(queueConnectionFactoryName);
}
jmsBrowser.setDestinationName(destinationName);
jmsBrowser.setDestinationType(destinationType);
IMessageBrowser browser = jmsBrowser;
IMessageBrowsingIterator it = null;
boolean remove = false;
if (root.equalsIgnoreCase("browse")) {
// OK
} else {
if (root.equalsIgnoreCase("remove")) {
remove = true;
} else {
throw new SenderException(getLogPrefix() + "unknown root element [" + root + "]");
}
}
XmlBuilder result = new XmlBuilder("result");
XmlBuilder items;
if (remove) {
items = new XmlBuilder("itemsRemoved");
} else {
items = new XmlBuilder("items");
}
try {
int count = 0;
it = browser.getIterator();
while (it.hasNext()) {
count++;
JmsMessageBrowserIteratorItem jmsMessageBrowserIteratorItem = (JmsMessageBrowserIteratorItem) it.next();
if (remove) {
jmsBrowser.deleteMessage(jmsMessageBrowserIteratorItem.getJMSMessageID());
} else {
// browse
XmlBuilder item = new XmlBuilder("item");
XmlBuilder timestamp = new XmlBuilder("timestamp");
timestamp.setValue(new java.util.Date(jmsMessageBrowserIteratorItem.getJMSTimestamp()).toString());
item.addSubElement(timestamp);
XmlBuilder messageId = new XmlBuilder("messageId");
messageId.setValue(jmsMessageBrowserIteratorItem.getJMSMessageID());
item.addSubElement(messageId);
XmlBuilder correlationId = new XmlBuilder("correlationId");
correlationId.setValue(jmsMessageBrowserIteratorItem.getCorrelationId());
item.addSubElement(correlationId);
XmlBuilder msg = new XmlBuilder("message");
msg.setCdataValue(jmsMessageBrowserIteratorItem.getText());
item.addSubElement(msg);
items.addSubElement(item);
}
}
if (remove) {
items.setValue(Integer.toString(count));
} else {
items.addAttribute("count", count);
}
result.addSubElement(items);
} catch (ListenerException e) {
throw new SenderException(getLogPrefix() + "got exception browsing messages", e);
} finally {
try {
if (it != null) {
it.close();
}
} catch (ListenerException e) {
log.warn(getLogPrefix() + "exception on closing message browser iterator", e);
}
}
return result.toXML();
}
Aggregations