Example usage for com.liferay.portal.kernel.util PortalUtil getLayoutFullURL

List of usage examples for com.liferay.portal.kernel.util PortalUtil getLayoutFullURL

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util PortalUtil getLayoutFullURL.

Prototype

public static String getLayoutFullURL(long groupId, String portletId, boolean secure) throws PortalException 

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 {/*from   w w  w  .  j  av  a 2  s .  co  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);
    }
}