📄 inboxaction.java
字号:
* @param id e-mail ID * @param inboxMessage InboxMessage * @throws MessagingException the message has wrong format * @throws IOException saving error */ private void process(int id, InboxMessage inboxMessage) throws MessagingException, IOException { inboxMessage.setAccountId(accountID); inboxMessage.setRoutingInfo(account.getDefaultOwner(), account.getDefaultWorkgroup()); // Init. List attachments = inboxMessage.getAttachments(); int size = (attachments == null) ? 0 : attachments.size(); int length = (inboxMessage.getBody() == null) ? 0 : inboxMessage.getBody().length(); logger.DEBUG(logName + "Body size: " + length); logger.DEBUG(logName + "Attachments: " + size); // Storing attachments. if (attachments != null && size > 0) { long processID = attachMgr.getUniqueProcessID(); inboxMessage.setProcessId(new Long(processID)); for (int i = 0; i < size; i++) { Attachment attach = (Attachment)attachments.get(i); attachMgr.addTempAttachment(ls, processID, attach.getFilename(), attach.getFiletype(), attach.getData()); } // for (int i = 0; i < size; i++) // Reset list before making async call. // Large binary data may affect total system performance. inboxMessage.resetAttachmentList(); inboxMessage.setIsAttachmentSaved(false); } // if (size > 0) // set HTML indicator if (isHTMLContent(inboxMessage.getBody())) { inboxMessage.setBody(inboxMessage.getBody() + StringHelper.HTML_INDICATOR); } else if (inboxMessage.getBody() != null){ inboxMessage.setBody(StringHelper.text2htmlNoTag(inboxMessage.getBody()) + StringHelper.HTML_INDICATOR); } else { inboxMessage.setBody(StringHelper.HTML_INDICATOR); } // if (isHTMLContent(inboxMessage.getBody())) InboxManagerLocal inboxManager = InboxHelper.getInboxManager(); // change the reference HTML tag <a> inboxManager.changeReferenceHTMLATAG(ls, inboxMessage); // convert email body, set image loader if (inboxMessage.getProcessId() != null) { if (!inboxManager.setImageAttachmentLoader(ls, inboxMessage)) { logger.INFO("The image loader for the email body wasn't set. The inserted images won't be showed."); } } // if (inboxMessage.getProcessId() != null) // Sending the JMS request to process message on server. /* logger.DEBUG(logName + "Calling async client for message #" + id); AsyncAction async = new AsyncAction(account, logName); asyncClient.sendMessage(async, inboxMessage); */ List nameList = InboxPropertyFactory.getInstance().getMailFilterNames(); if (nameList == null) { throw new NullPointerException("No any MailFilter found in config!"); } // if (nameList == null) int filterCount = nameList.size(); for (int i = 0; i < filterCount; i++) { String name = (String)nameList.get(i); // Init Mail filter. Class clazz = InboxPropertyFactory.getInstance().getMailFilterClass(name); MailFilter filter = (MailFilter)InboxPropertyFactory.getInstance().getInstance(clazz, account, logName, publisher); // Call MailFilter#filterMail logger.DEBUG("AsyncAction#process(): " + hashCode()); logger.DEBUG(" try to apply filter '" + name + "', class: " + clazz); logger.DEBUG(" current pos = " + i + " [" + filterCount + "]"); RuntimeException error = null; try { if (!filter.filterMail(inboxMessage)) { logger.WARN(" filter '" + name + "' returned false - break processing of this email message."); break; } // if (!filter.filterMail(im)) } catch(RuntimeException ex) { error = ex; } catch(Exception ex) { error = new GenericSystemException(ex); } // try if (error != null) { // Caught exception. // -> call error handler and throw error. String msg = "Filter '" + name + "' failed: " + error.getMessage(); //sysPublisher.ERROR(msg, new Long(account.getAccountID())); logger.ERROR(msg); logger.ERROR(error); try { localProcessOnError(inboxMessage); } catch(Exception exsp) { logger.ERROR("The Error filter is failed due to: " + exsp.getMessage()); logger.ERROR(exsp); return; } // try throw error; } else { logger.DEBUG(" filter '" + name + "' applied ok"); } // if (error != null) } // for (int i = 0; i < size; i++) // Ok. logger.INFO("\n=== " + logName + "Message # " + id + " went to process routine. ===\n"); } // process(int, InboxMessage) /** * The method gets the initial parameters. */ private void init() { // Get context. XAActionContext context = (XAActionContext)getContext(); ls = context.getLogonSession(); // Init system logger. publisher = (SystemLogPublisher)context.getLog(); if (publisher == null) { publisher = new SystemLogPublisher(ls); } // if (publisher == null) // Read required parameters. // These parameters should be passed: // 1. 'account' - Account instance // 2. 'im' - InboxMessage instance (optional) account = (Account)context.getParameter("account"); if (account == null) { throw new NullPointerException("Account not passed!"); } accountID = new Long(account.getAccountID()); inboxMessage = (InboxMessage)context.getParameter("im"); // Read optional parameters. batchSize = context.getParamAsInt("batchSize"); updateAccount = context.getParamAsBoolean("updateAccount"); // Init async client. asyncClient = new AsyncClient(); // Init AttachmentManager EJB. attachMgr = (AttachmentManagerLocal)context.getCOM(). getLocalObject(JNDINames.AttachmentManager, AttachmentManagerLocalHome.class); // Construct service name. serviceName = InboxHelper.toURI(account); logName = "[" + serviceName + "] "; // Logging... String msg = "Service '" + serviceName + "' started."; publisher.INFO(msg, accountID); INFO(logName + msg); } // init() private boolean localProcessOnError(InboxMessage im) { // Init Mail filter. Class errClazz = InboxPropertyFactory.getInstance().getErrorMailFilterClass(); MailFilter errFilter = (MailFilter)InboxPropertyFactory.getInstance().getInstance(errClazz, account, logName, publisher); DEBUG(" try to apply error filter class: " + errClazz); return errFilter.filterMail(im); } // processOnError(InboxMessage) /** * The method checks - is given string . * @param s the string to check * @return <b>true</b> if <br> presents */ public boolean isHTMLContent(String s) { if (StringHelper.isEmpty(s)) { return false; } boolean returnValue = false; try { RE re = new RE("<html((\\s+[^>]*)|(>))", RE.MATCH_CASEINDEPENDENT); if (re.match(s)) returnValue = true; else returnValue = false; if (!returnValue) { RE reBr = new RE("<br((\\s+[^>]*)|(>))", RE.MATCH_CASEINDEPENDENT); if (reBr.match(s)) returnValue = true; else returnValue = false; } } catch(RESyntaxException rex) { throw new GenericSystemException("Regexp exception: " + rex.getMessage(), rex); } return returnValue; } // isHTMLContent(String) : boolean // ============================================================= Inner classes /** * Inner class to make Inbox async action. * * @author Konstantin Mironov * @since 8 Dec 2006 */ private static class AsyncClient extends JMSClient { /** * Constructor */ public AsyncClient() { super(JNDINames.JmsConnectionFactory, JNDINames.JmsAsyncQueue); } /** * Send message to message driven bean * @param obj ASyncObject object * @param request param for process method ASyncObject interface */ private void sendMessage(ASyncObject obj, ASyncRequest request) { HashMap map = new HashMap(); map.put(AsyncMDB.OBJECT_PARAM, obj); if (request != null) { map.put(AsyncMDB.REQUEST_PARAM, request); } send(map); } // sendMessage(ASyncObject, ASyncRequest) } // static class AsyncClient extends JMSClient /** * Inner class represents Inbox async action. * * @author Konstantin Mironov * @since 8 Dec 2006 */ public static class AsyncAction implements ASyncObject { private Account account; private SystemLogPublisher sysPublisher; private String logName; // Logger. private AbstractLogger logger = Log.getLog(getClass()); // // Constructor // AsyncAction(Account account, String logName, SystemLogPublisher sysPublisher) { this.account = account; this.logName = logName; this.sysPublisher = sysPublisher; } // AsyncAction(Account, String, SystemLogPublisher) /** * Make async process. * @param request async request */ public void process(ASyncRequest request) { logger.DEBUG("Starting AsyncAction#process(): " + hashCode()); // Get InboxMessage from request. InboxMessage im = (InboxMessage)request; List nameList = InboxPropertyFactory.getInstance().getMailFilterNames(); if (nameList == null) { throw new NullPointerException("No any MailFilter found in config!"); } // if (nameList == null) int size = nameList.size(); for (int i = 0; i < size; i++) { String name = (String)nameList.get(i); // Init Mail filter. Class clazz = InboxPropertyFactory.getInstance().getMailFilterClass(name); MailFilter filter = (MailFilter)InboxPropertyFactory.getInstance().getInstance(clazz, account, logName, sysPublisher); // Call MailFilter#filterMail logger.DEBUG("AsyncAction#process(): " + hashCode()); logger.DEBUG(" try to apply filter '" + name + "', class: " + clazz); logger.DEBUG(" current pos = " + i + " [" + size + "]"); RuntimeException error = null; try { if (!filter.filterMail(im)) { logger.WARN(" filter '" + name + "' returned false - break cycle"); break; } // if (!filter.filterMail(im)) } catch(RuntimeException ex) { error = ex; } catch(Exception ex) { error = new GenericSystemException(ex); } // try if (error != null) { // Caught exception. // -> call error handler and throw error. String msg = "Filter '" + name + "' failed: " + error.getMessage(); //sysPublisher.ERROR(msg, new Long(account.getAccountID())); logger.ERROR(msg); logger.ERROR(error); processOnError(im); throw error; } else { logger.DEBUG(" filter '" + name + "' applied ok"); } // if (error != null) } // for (int i = 0; i < size; i++) logger.DEBUG("AsyncAction#process() finished: " + hashCode()); } // process(ASyncRequest) /** * Default error handler. * Calls error filter. * @param im InboxMessage * @return boolean */ private boolean processOnError(InboxMessage im) { // Init Mail filter. Class errClazz = InboxPropertyFactory.getInstance().getErrorMailFilterClass(); MailFilter errFilter = (MailFilter)InboxPropertyFactory.getInstance().getInstance(errClazz, account, logName, sysPublisher); // Call MailFilter#filterMail logger.DEBUG(" try to apply error filter class: " + errClazz); return errFilter.filterMail(im); } // processOnError(InboxMessage) : boolean } // static class AsyncAction implements ASyncObject} // class InboxAction extends XAAction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -