Example usage for com.liferay.portal.security.permission PermissionCheckerUtil setThreadValues

List of usage examples for com.liferay.portal.security.permission PermissionCheckerUtil setThreadValues

Introduction

In this page you can find the example usage for com.liferay.portal.security.permission PermissionCheckerUtil setThreadValues.

Prototype

public static void setThreadValues(User user) 

Source Link

Usage

From source file:com.liferay.message.boards.internal.messaging.MailingListMessageListener.java

License:Open Source License

protected void processMessage(MailingListRequest mailingListRequest, Message mailMessage) throws Exception {

    if (MBUtil.hasMailIdHeader(mailMessage)) {
        return;//w  w  w . j  a va2  s  . c om
    }

    String from = null;

    Address[] addresses = mailMessage.getFrom();

    if (ArrayUtil.isNotEmpty(addresses)) {
        Address address = addresses[0];

        if (address instanceof InternetAddress) {
            from = ((InternetAddress) address).getAddress();
        } else {
            from = address.toString();
        }
    }

    long companyId = mailingListRequest.getCompanyId();
    long groupId = mailingListRequest.getGroupId();
    long categoryId = mailingListRequest.getCategoryId();

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

    boolean anonymous = false;

    User user = _userLocalService.fetchUserByEmailAddress(companyId, from);

    if (user == null) {
        if (!mailingListRequest.isAllowAnonymous()) {
            return;
        }

        anonymous = true;

        user = _userLocalService.getUserById(companyId, mailingListRequest.getUserId());
    }

    long parentMessageId = MBUtil.getParentMessageId(mailMessage);

    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);
    }

    MBMailMessage mbMailMessage = new MBMailMessage();

    MBMailUtil.collectPartContent(mailMessage, mbMailMessage);

    PermissionCheckerUtil.setThreadValues(user);

    MailingListThreadLocal.setSourceMailingList(true);

    String subject = MBUtil.getSubjectWithoutMessageId(mailMessage);

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

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

    serviceContext.setLayoutFullURL(_portal.getLayoutFullURL(groupId, portletId));

    serviceContext.setScopeGroupId(groupId);

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = mbMailMessage.getInputStreamOVPs();

    try {
        if (parentMessage == null) {
            _mbMessageService.addMessage(groupId, categoryId, subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, anonymous, 0.0, true, serviceContext);
        } else {
            _mbMessageService.addMessage(parentMessage.getMessageId(), subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, anonymous, 0.0, true, serviceContext);
        }
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            try (InputStream inputStream = inputStreamOVP.getValue()) {
            } catch (IOException ioe) {
                if (_log.isWarnEnabled()) {
                    _log.warn(ioe, ioe);
                }
            }
        }
    }
}

From source file:com.liferay.message.boards.internal.messaging.MailingListMessageListener.java

License:Open Source License

protected void processMessages(MailingListRequest mailingListRequest, Message[] messages) throws Exception {

    for (Message message : messages) {
        try {/*from  ww w . j ava 2 s. co m*/
            processMessage(mailingListRequest, message);
        } finally {
            PermissionCheckerUtil.setThreadValues(null);
        }
    }
}

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.messaging.MailingListMessageListener.java

License:Open Source License

protected void processMessage(MailingListRequest mailingListRequest, Message mailMessage) throws Exception {

    if (MBUtil.hasMailIdHeader(mailMessage)) {
        return;//ww  w  .  ja va2  s . co  m
    }

    String from = null;

    Address[] addresses = mailMessage.getFrom();

    if ((addresses != null) && (addresses.length > 0)) {
        Address address = addresses[0];

        if (address instanceof InternetAddress) {
            from = ((InternetAddress) address).getAddress();
        } else {
            from = address.toString();
        }
    }

    long companyId = mailingListRequest.getCompanyId();
    long groupId = mailingListRequest.getGroupId();
    long categoryId = mailingListRequest.getCategoryId();

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

    boolean anonymous = false;

    User user = UserLocalServiceUtil.getUserById(companyId, mailingListRequest.getUserId());

    try {
        user = UserLocalServiceUtil.getUserByEmailAddress(companyId, from);
    } catch (NoSuchUserException nsue) {
        anonymous = true;

        if (!mailingListRequest.isAllowAnonymous()) {
            return;
        }
    }

    long parentMessageId = MBUtil.getParentMessageId(mailMessage);

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

    MBMessage parentMessage = null;

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

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

    MBMailMessage mbMailMessage = new MBMailMessage();

    MBUtil.collectPartContent(mailMessage, mbMailMessage);

    PermissionCheckerUtil.setThreadValues(user);

    MailingListThreadLocal.setSourceMailingList(true);

    String subject = MBUtil.getSubjectWithoutMessageId(mailMessage);

    ServiceContext serviceContext = new ServiceContext();

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

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = mbMailMessage.getInputStreamOVPs();

    try {
        if (parentMessage == null) {
            MBMessageServiceUtil.addMessage(groupId, categoryId, subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, anonymous, 0.0, true, serviceContext);
        } else {
            MBMessageServiceUtil.addMessage(groupId, categoryId, parentMessage.getThreadId(),
                    parentMessage.getMessageId(), subject, mbMailMessage.getBody(),
                    MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, anonymous, 0.0, true, serviceContext);
        }
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            InputStream inputStream = inputStreamOVP.getValue();

            StreamUtil.cleanUp(inputStream);
        }
    }
}

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. java2  s.  c  o  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);
    }
}