Example usage for com.liferay.portal.kernel.pop MessageListenerException MessageListenerException

List of usage examples for com.liferay.portal.kernel.pop MessageListenerException MessageListenerException

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.pop MessageListenerException MessageListenerException.

Prototype

public MessageListenerException(Throwable cause) 

Source Link

Usage

From source file:com.liferay.message.boards.internal.pop.MessageListenerImpl.java

License:Open Source License

@Override
public void deliver(String from, String recipient, Message message) throws MessageListenerException {

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = null;

    try {//w  ww  . j a  v  a2  s .  c o  m
        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        if (_log.isDebugEnabled()) {
            _log.debug("Deliver message from " + from + " to " + recipient);
        }

        String messageIdString = getMessageIdString(recipient, message);

        Company company = getCompany(messageIdString);

        if (_log.isDebugEnabled()) {
            _log.debug("Message id " + messageIdString);
        }

        long parentMessageId = MBUtil.getMessageId(messageIdString);

        if (_log.isDebugEnabled()) {
            _log.debug("Parent message id " + parentMessageId);
        }

        MBMessage parentMessage = null;

        if (parentMessageId > 0) {
            parentMessage = _mbMessageLocalService.fetchMBMessage(parentMessageId);
        }

        if (_log.isDebugEnabled()) {
            _log.debug("Parent message " + parentMessage);
        }

        long groupId = 0;
        long categoryId = MBUtil.getCategoryId(messageIdString);

        MBCategory category = _mbCategoryLocalService.fetchMBCategory(categoryId);

        if (category == null) {
            categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;

            if (parentMessage != null) {
                groupId = parentMessage.getGroupId();
            }
        } else {
            groupId = category.getGroupId();
        }

        if (_log.isDebugEnabled()) {
            _log.debug("Group id " + groupId);
            _log.debug("Category id " + categoryId);
        }

        User user = _userLocalService.getUserByEmailAddress(company.getCompanyId(), from);

        String subject = null;

        if (parentMessage != null) {
            subject = MBUtil.getSubjectForEmail(parentMessage);
        }

        MBMailMessage mbMailMessage = new MBMailMessage();

        MBUtil.collectPartContent(message, mbMailMessage);

        inputStreamOVPs = mbMailMessage.getInputStreamOVPs();

        PermissionCheckerUtil.setThreadValues(user);

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAttribute("propagatePermissions", Boolean.TRUE);

        String portletId = PortletProviderUtil.getPortletId(MBMessage.class.getName(),
                PortletProvider.Action.VIEW);

        serviceContext.setLayoutFullURL(PortalUtil.getLayoutFullURL(groupId, portletId,
                StringUtil.equalsIgnoreCase(Http.HTTPS, PropsValues.WEB_SERVER_PROTOCOL)));

        serviceContext.setScopeGroupId(groupId);

        if (parentMessage == null) {
            _mbMessageService.addMessage(groupId, categoryId, subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false, 0.0, true, serviceContext);
        } else {
            _mbMessageService.addMessage(parentMessage.getMessageId(), subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false, 0.0, true, serviceContext);
        }

        if (_log.isDebugEnabled()) {
            _log.debug("Delivering message takes " + stopWatch.getTime() + " ms");
        }
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug("Prevented unauthorized post from " + from);
        }

        throw new MessageListenerException(pe);
    } catch (Exception e) {
        _log.error(e, e);

        throw new MessageListenerException(e);
    } finally {
        if (inputStreamOVPs != null) {
            for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

                InputStream inputStream = inputStreamOVP.getValue();

                StreamUtil.cleanUp(inputStream);
            }
        }

        PermissionCheckerUtil.setThreadValues(null);
    }
}

From source file:com.liferay.portlet.messageboards.pop.MessageListenerImpl.java

License:Open Source License

public void deliver(String from, String recipient, Message message) throws MessageListenerException {

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = null;

    try {// w  ww. j a va2  s.  co  m
        StopWatch stopWatch = null;

        if (_log.isDebugEnabled()) {
            stopWatch = new StopWatch();

            stopWatch.start();

            _log.debug("Deliver message from " + from + " to " + recipient);
        }

        String messageId = getMessageId(recipient, message);

        Company company = getCompany(messageId);

        if (_log.isDebugEnabled()) {
            _log.debug("Message id " + messageId);
        }

        long groupId = 0;
        long categoryId = getCategoryId(messageId);

        try {
            MBCategory category = MBCategoryLocalServiceUtil.getCategory(categoryId);

            groupId = category.getGroupId();
        } catch (NoSuchCategoryException nsce) {
            groupId = categoryId;
            categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
        }

        if (_log.isDebugEnabled()) {
            _log.debug("Group id " + groupId);
            _log.debug("Category id " + categoryId);
        }

        User user = UserLocalServiceUtil.getUserByEmailAddress(company.getCompanyId(), from);

        long parentMessageId = getParentMessageId(recipient, message);

        if (_log.isDebugEnabled()) {
            _log.debug("Parent message id " + parentMessageId);
        }

        MBMessage parentMessage = null;

        try {
            if (parentMessageId > 0) {
                parentMessage = MBMessageLocalServiceUtil.getMessage(parentMessageId);
            }
        } catch (NoSuchMessageException nsme) {

            // If the parent message does not exist we ignore it and post
            // the message as a new thread.

        }

        if (_log.isDebugEnabled()) {
            _log.debug("Parent message " + parentMessage);
        }

        String subject = MBUtil.getSubjectWithoutMessageId(message);

        MBMailMessage mbMailMessage = new MBMailMessage();

        MBUtil.collectPartContent(message, mbMailMessage);

        inputStreamOVPs = mbMailMessage.getInputStreamOVPs();

        PermissionCheckerUtil.setThreadValues(user);

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setLayoutFullURL(PortalUtil.getLayoutFullURL(groupId, PortletKeys.MESSAGE_BOARDS));
        serviceContext.setScopeGroupId(groupId);

        if (parentMessage == null) {
            MBMessageServiceUtil.addMessage(groupId, categoryId, subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false, 0.0, true, serviceContext);
        } else {
            MBMessageServiceUtil.addMessage(groupId, categoryId, parentMessage.getThreadId(),
                    parentMessage.getMessageId(), subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false, 0.0, true, serviceContext);
        }

        if (_log.isDebugEnabled()) {
            _log.debug("Delivering message takes " + stopWatch.getTime() + " ms");
        }
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug("Prevented unauthorized post from " + from);
        }

        throw new MessageListenerException(pe);
    } catch (Exception e) {
        _log.error(e, e);

        throw new MessageListenerException(e);
    } finally {
        if (inputStreamOVPs != null) {
            for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

                InputStream inputStream = inputStreamOVP.getValue();

                StreamUtil.cleanUp(inputStream);
            }
        }

        PermissionCheckerUtil.setThreadValues(null);
    }
}