Search in sources :

Example 1 with EmailMessageHandler

use of org.alfresco.email.server.handler.EmailMessageHandler in project alfresco-repository by Alfresco.

the class EmailServiceImpl method getMessageHandler.

/**
 * @param nodeRef           Target node
 * @return                  Handler that can process message addressed to specific node (target node).
 * @throws                  EmailMessageException is thrown if a suitable message handler isn't found.
 */
private EmailMessageHandler getMessageHandler(NodeRef nodeRef) {
    ParameterCheck.mandatory("nodeRef", nodeRef);
    QName nodeTypeQName = nodeService.getType(nodeRef);
    String prefixedNodeTypeStr = nodeTypeQName.toPrefixString(namespaceService);
    EmailMessageHandler handler = emailMessageHandlerMap.get(prefixedNodeTypeStr);
    if (handler == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("did not find a handler for type:" + prefixedNodeTypeStr);
        }
        // not a direct match on type
        // need to check the super-types (if any) of the target node
        TypeDefinition typeDef = dictionaryService.getType(nodeTypeQName);
        while (typeDef != null) {
            QName parentName = typeDef.getParentName();
            if (parentName != null) {
                String prefixedSubTypeStr = parentName.toPrefixString(namespaceService);
                handler = emailMessageHandlerMap.get(prefixedSubTypeStr);
                if (handler != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("found a handler for a subtype:" + prefixedSubTypeStr);
                    }
                    return handler;
                }
            }
            typeDef = dictionaryService.getType(parentName);
        }
    }
    if (handler == null) {
        throw new EmailMessageException(ERR_HANDLER_NOT_FOUND, prefixedNodeTypeStr);
    }
    return handler;
}
Also used : QName(org.alfresco.service.namespace.QName) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) EmailMessageHandler(org.alfresco.email.server.handler.EmailMessageHandler) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition)

Example 2 with EmailMessageHandler

use of org.alfresco.email.server.handler.EmailMessageHandler in project alfresco-repository by Alfresco.

the class EmailServiceImpl method processMessage.

/**
 * Process the message. Method is called after filtering by sender's address.
 * @param delivery - who gets the message and who is it from (may be different from the contents of the message)
 * @param nodeRef Addressed node (target node).
 * @param message Email message
 * @throws EmailMessageException Any exception occured inside the method will be converted and thrown as <code>EmailMessageException</code>
 */
private void processMessage(final EmailDelivery delivery, final NodeRef nodeRef, final EmailMessage message) {
    if (!emailInboundEnabled) {
        throw new EmailMessageException(ERR_INBOUND_EMAIL_DISABLED);
    }
    try {
        // Get the username for the process using the system account
        final RetryingTransactionCallback<String> getUsernameCallback = new RetryingTransactionCallback<String>() {

            public String execute() throws Throwable {
                String userName = null;
                userName = getUsername(delivery.getFrom());
                if (userName == null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("unable to find user for from: " + delivery.getFrom() + ",trying message.from next");
                    }
                    userName = getUsername(message.getFrom());
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("userName = : " + userName);
                }
                if (userName == null) {
                    if (unknownUser.isEmpty()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("unable to find user for from: " + message.getFrom());
                        }
                        throw new EmailMessageException(ERR_UNKNOWN_SOURCE_ADDRESS, message.getFrom());
                    } else {
                        if (logger.isDebugEnabled()) {
                            logger.debug("unable to find user for from - return anonymous: ");
                        }
                        userName = unknownUser;
                    }
                }
                // Ensure that the user is part of the Email Contributors group
                if (userName == null || !isEmailContributeUser(userName)) {
                    throw new EmailMessageException(ERR_USER_NOT_EMAIL_CONTRIBUTOR, userName);
                }
                return userName;
            }
        };
        RunAsWork<String> getUsernameRunAsWork = new RunAsWork<String>() {

            public String doWork() throws Exception {
                return retryingTransactionHelper.doInTransaction(getUsernameCallback, false);
            }
        };
        String username;
        if (delivery.getAuth() != null) {
            // The user has authenticated.
            username = delivery.getAuth();
            logger.debug("user has already authenticated as:" + username);
        } else {
            // Need to faff with old message stuff.
            username = AuthenticationUtil.runAs(getUsernameRunAsWork, AuthenticationUtil.SYSTEM_USER_NAME);
        }
        // Process the message using the username's account
        final RetryingTransactionCallback<Object> processMessageCallback = new RetryingTransactionCallback<Object>() {

            public Object execute() throws Throwable {
                // String recipient = message.getTo();
                String recipient = delivery.getRecipient();
                NodeRef targetNodeRef = null;
                if (nodeRef == null) {
                    targetNodeRef = getTargetNode(recipient);
                } else {
                    targetNodeRef = nodeRef;
                }
                EmailMessageHandler messageHandler = getMessageHandler(targetNodeRef);
                try {
                    messageHandler.processMessage(targetNodeRef, message);
                } catch (DuplicateChildNodeNameException e) {
                    throw new ConcurrencyFailureException(e.getMessage());
                }
                return null;
            }
        };
        RunAsWork<Object> processMessageRunAsWork = new RunAsWork<Object>() {

            public Object doWork() throws Exception {
                return retryingTransactionHelper.doInTransaction(processMessageCallback, false);
            }
        };
        AuthenticationUtil.runAs(processMessageRunAsWork, username);
    } catch (EmailMessageException e) {
        // These are email-specific errors
        throw e;
    } catch (AccessDeniedException e) {
        throw new EmailMessageException(ERR_ACCESS_DENIED, delivery.getFrom(), delivery.getRecipient());
    } catch (IntegrityException e) {
        throw new EmailMessageException(ERR_INVALID_SUBJECT);
    } catch (Throwable e) {
        throw new AlfrescoRuntimeException("Email message processing failed", e);
    }
}
Also used : DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) EmailMessageHandler(org.alfresco.email.server.handler.EmailMessageHandler) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

EmailMessageHandler (org.alfresco.email.server.handler.EmailMessageHandler)2 EmailMessageException (org.alfresco.service.cmr.email.EmailMessageException)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)1 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)1 DuplicateChildNodeNameException (org.alfresco.service.cmr.repository.DuplicateChildNodeNameException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 QName (org.alfresco.service.namespace.QName)1 ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)1