<commit_before>/*
 * $Log: Browse.java,v $
 * Revision 1.5  2007-10-08 12:26:12  europe\L190409
 * corrected date formatting
 *
 * Revision 1.4  2007/09/24 13:05:02  <NAME> <<EMAIL>>
 * ability to download file, using correct filename
 *
 * Revision 1.3  2007/06/14 09:45:00  <NAME> <<EMAIL>>
 * strip slash from context path
 *
 * Revision 1.2  2007/05/29 11:13:25  <NAME> <<EMAIL>>
 * add message logs
 *
 * Revision 1.1  2005/09/29 14:57:11  <NAME> <<EMAIL>>
 * split transactional storage browser in browse-only and processing-options
 *
 */
package nl.nn.adapterframework.webcontrol.action;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.nn.adapterframework.core.Adapter;
import nl.nn.adapterframework.core.IListener;
import nl.nn.adapterframework.core.IMessageBrowser;
import nl.nn.adapterframework.core.IMessageBrowsingIterator;
import nl.nn.adapterframework.pipes.MessageSendingPipe;
import nl.nn.adapterframework.receivers.ReceiverBase;
import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.XmlBuilder;
import nl.nn.adapterframework.util.XmlUtils;
import nl.nn.adapterframework.webcontrol.FileViewerServlet;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Basic browser for transactional storage.
 * 
 * @version Id
 * @author  <NAME>
 * @since   4.4
 */
public class Browse extends ActionBase {
	public static final String version="$RCSfile: Browse.java,v $ $Revision: 1.5 $ $Date: 2007-10-08 12:26:12 $";

	protected void performAction(Adapter adapter, ReceiverBase receiver, String action, IMessageBrowser mb, String messageId) {
		// allow for extensions
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		// Initialize action
		initAction(request);
		if (null == config) {
			return (mapping.findForward("noconfig"));
		}

		String action = request.getParameter("action");
		if (null == action) {
			action = mapping.getParameter();
		}
        
		String storageType  = request.getParameter("storageType");
		String adapterName  = request.getParameter("adapterName");
		String receiverName = request.getParameter("receiverName");
		String pipeName     = request.getParameter("pipeName");
		String messageId    = request.getParameter("messageId");

		//commandIssuedBy containes information about the location the
		// command is sent from
		String commandIssuedBy= getCommandIssuedBy(request);
		log.debug("storageType ["+storageType+"] action ["+action+"] adapterName ["+adapterName+"] receiverName ["+receiverName+"] pipeName ["+pipeName+"] issued by ["+commandIssuedBy+"]");

		
		Adapter adapter = (Adapter) config.getRegisteredAdapter(adapterName);

		IMessageBrowser mb;
		IListener listener=null;
		if ("messagelog".equals(storageType)) {
			MessageSendingPipe pipe=(MessageSendingPipe)adapter.getPipeLine().getPipe(pipeName);
			mb=pipe.getMessageLog();	
		} else {
			ReceiverBase receiver = (ReceiverBase) adapter.getReceiverByName(receiverName);
			mb = receiver.getErrorStorage();
			performAction(adapter, receiver, action, mb, messageId);
			listener = receiver.getListener();
		}

		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;
				}
				String type = request.getParameter("type");
				FileViewerServlet.showReaderContents(new StringReader(msg),"msg"+messageId,type,response, request.getContextPath().substring(1),"message ["+messageId+"]");
			} else {
				IMessageBrowsingIterator mbi=mb.getIterator();
				try {
					XmlBuilder messages=new XmlBuilder("messages");
					messages.addAttribute("storageType",storageType);
					messages.addAttribute("action",action);
					messages.addAttribute("adapterName",XmlUtils.encodeChars(adapterName));
					if ("messagelog".equals(storageType)) {
						messages.addAttribute("object","pipe ["+XmlUtils.encodeChars(pipeName)+"] of adapter ["+XmlUtils.encodeChars(adapterName)+"]");
						messages.addAttribute("pipeName",XmlUtils.encodeChars(pipeName));
 					} else {
						messages.addAttribute("object","receiver ["+XmlUtils.encodeChars(receiverName)+"] of adapter ["+XmlUtils.encodeChars(adapterName)+"]");
						messages.addAttribute("receiverName",XmlUtils.encodeChars(receiverName));
 					}
					int messageCount;
					for (messageCount=0; mbi.hasNext(); messageCount++) {
						Object iterItem = mbi.next();
						XmlBuilder message=new XmlBuilder("message");
						message.addAttribute("id",mb.getId(iterItem));
						message.addAttribute("pos",Integer.toString(messageCount+1));
						message.addAttribute("originalId",mb.getOriginalId(iterItem));
						message.addAttribute("correlationId",mb.getCorrelationId(iterItem));
						message.addAttribute("insertDate",DateUtils.format(mb.getInsertDate(iterItem), DateUtils.FORMAT_FULL_GENERIC));
						message.addAttribute("comment",XmlUtils.encodeChars(mb.getCommentString(iterItem)));
						messages.addSubElement(message);
					}
					messages.addAttribute("messageCount",Integer.toString(messageCount));
					request.setAttribute("messages",messages.toXML());
				} finally {
					mbi.close();
				}
			}
		} catch (Throwable e) {
			log.error(e);
			throw new ServletException(e);
		}
		
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}		
		log.debug("forward to success");
		return (mapping.findForward("success"));

	}

}
<commit_msg>configurable order for jdbc transactional storage browsing + max number of messages<commit_after>/*
 * $Log: Browse.java,v $
 * Revision 1.6  2007-11-15 12:36:10  europe\L190409
 * configurable order for jdbc transactional storage browsing
 * + max number of messages
 *
 * Revision 1.5  2007/10/08 12:26:12  <NAME> <<EMAIL>>
 * corrected date formatting
 *
 * Revision 1.4  2007/09/24 13:05:02  <NAME> <<EMAIL>>
 * ability to download file, using correct filename
 *
 * Revision 1.3  2007/06/14 09:45:00  <NAME> <<EMAIL>>
 * strip slash from context path
 *
 * Revision 1.2  2007/05/29 11:13:25  <NAME> <<EMAIL>>
 * add message logs
 *
 * Revision 1.1  2005/09/29 14:57:11  <NAME> <<EMAIL>>
 * split transactional storage browser in browse-only and processing-options
 *
 */
package nl.nn.adapterframework.webcontrol.action;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.nn.adapterframework.core.Adapter;
import nl.nn.adapterframework.core.IListener;
import nl.nn.adapterframework.core.IMessageBrowser;
import nl.nn.adapterframework.core.IMessageBrowsingIterator;
import nl.nn.adapterframework.pipes.MessageSendingPipe;
import nl.nn.adapterframework.receivers.ReceiverBase;
import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.XmlBuilder;
import nl.nn.adapterframework.util.XmlUtils;
import nl.nn.adapterframework.webcontrol.FileViewerServlet;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Basic browser for transactional storage.
 * 
 * @version Id
 * @author  <NAME>
 * @since   4.4
 */
public class Browse extends ActionBase {
	public static final String version="$RCSfile: Browse.java,v $ $Revision: 1.6 $ $Date: 2007-11-15 12:36:10 $";

	public int maxMessages = AppConstants.getInstance().getInt("browse.messages.max",0); 

	protected void performAction(Adapter adapter, ReceiverBase receiver, String action, IMessageBrowser mb, String messageId) {
		// allow for extensions
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		// Initialize action
		initAction(request);
		if (null == config) {
			return (mapping.findForward("noconfig"));
		}

		String action = request.getParameter("action");
		if (null == action) {
			action = mapping.getParameter();
		}
        
		String storageType  = request.getParameter("storageType");
		String adapterName  = request.getParameter("adapterName");
		String receiverName = request.getParameter("receiverName");
		String pipeName     = request.getParameter("pipeName");
		String messageId    = request.getParameter("messageId");

		//commandIssuedBy containes information about the location the
		// command is sent from
		String commandIssuedBy= getCommandIssuedBy(request);
		log.debug("storageType ["+storageType+"] action ["+action+"] adapterName ["+adapterName+"] receiverName ["+receiverName+"] pipeName ["+pipeName+"] issued by ["+commandIssuedBy+"]");

		
		Adapter adapter = (Adapter) config.getRegisteredAdapter(adapterName);

		IMessageBrowser mb;
		IListener listener=null;
		if ("messagelog".equals(storageType)) {
			MessageSendingPipe pipe=(MessageSendingPipe)adapter.getPipeLine().getPipe(pipeName);
			mb=pipe.getMessageLog();	
		} else {
			ReceiverBase receiver = (ReceiverBase) adapter.getReceiverByName(receiverName);
			mb = receiver.getErrorStorage();
			performAction(adapter, receiver, action, mb, messageId);
			listener = receiver.getListener();
		}

		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");
				FileViewerServlet.showReaderContents(new StringReader(msg),"msg"+messageId,type,response, request.getContextPath().substring(1),"message ["+messageId+"]");
			} else {
				IMessageBrowsingIterator mbi=mb.getIterator();
				try {
					XmlBuilder messages=new XmlBuilder("messages");
					messages.addAttribute("storageType",storageType);
					messages.addAttribute("action",action);
					messages.addAttribute("adapterName",XmlUtils.encodeChars(adapterName));
					if ("messagelog".equals(storageType)) {
						messages.addAttribute("object","pipe ["+XmlUtils.encodeChars(pipeName)+"] of adapter ["+XmlUtils.encodeChars(adapterName)+"]");
						messages.addAttribute("pipeName",XmlUtils.encodeChars(pipeName));
 					} else {
						messages.addAttribute("object","receiver ["+XmlUtils.encodeChars(receiverName)+"] of adapter ["+XmlUtils.encodeChars(adapterName)+"]");
						messages.addAttribute("receiverName",XmlUtils.encodeChars(receiverName));
 					}
					int messageCount;
					for (messageCount=0; mbi.hasNext(); messageCount++) {
						Object iterItem = mbi.next();
						XmlBuilder message=new XmlBuilder("message");
						message.addAttribute("id",mb.getId(iterItem));
						message.addAttribute("pos",Integer.toString(messageCount+1));
						message.addAttribute("originalId",mb.getOriginalId(iterItem));
						message.addAttribute("correlationId",mb.getCorrelationId(iterItem));
						message.addAttribute("insertDate",DateUtils.format(mb.getInsertDate(iterItem), DateUtils.FORMAT_FULL_GENERIC));
						message.addAttribute("comment",XmlUtils.encodeChars(mb.getCommentString(iterItem)));
						messages.addSubElement(message);
						messageCount++;
						if (getMaxMessages()>0 && messageCount>=getMaxMessages()) {
							log.warn("stopped iterating messages after ["+messageCount+"]: limit reached");
							break;
						}
					}
					messages.addAttribute("messageCount",Integer.toString(messageCount));
					request.setAttribute("messages",messages.toXML());
				} finally {
					mbi.close();
				}
			}
		} catch (Throwable e) {
			log.error(e);
			throw new ServletException(e);
		}
		
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}		
		log.debug("forward to success");
		return (mapping.findForward("success"));

	}

	public void setMaxMessages(int i) {
		maxMessages = i;
	}
	public int getMaxMessages() {
		return maxMessages;
	}

}

{'fraction_non_alphanumeric': 0.13088497006956804, 'fraction_numerical': 0.018767189775117293, 'mean_word_length': 4.909655831739962, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078493', 'n_tokens_mistral': 4299, 'n_tokens_neox': 3651, 'n_words': 707}