001/*
002 *  Copyright 2014 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.core.impl.checker;
017
018import java.util.List;
019
020//import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.core.util.mail.SendMailHelper;
026import org.ametys.runtime.model.checker.ItemChecker;
027import org.ametys.runtime.model.checker.ItemCheckerTestFailureException;
028
029/**
030 * Tests the connection to the mail box and sends an e-mail to the system administrator.
031 */
032public class SendMailChecker implements ItemChecker, Configurable
033{
034    @Override
035    public void configure(Configuration configuration) throws ConfigurationException
036    {
037        Configuration[] config = configuration.getChild("linked-params").getChildren();
038        if (config.length != 7)
039        {
040            throw new ConfigurationException("The MailConnectionChecker should have 5 linked params in the right order: host, port, security.protocol, user, password, fromEmail, toEmail");
041        }
042    }
043    
044    @Override 
045    public void check(List<String> values) throws ItemCheckerTestFailureException
046    {
047        String host = values.get(0);
048        String portAsString = values.get(1);
049        String protocol = values.get(2);
050        String user = values.get(3);
051        String password = values.get(4);
052        String sender = values.get(5);
053        String recipient = values.get(6);
054
055        long port = Integer.parseInt(portAsString);
056        try
057        {
058            SendMailHelper.newMail()
059                          .withSubject("[Ametys] Test email")
060                          .withTextBody("You are receving this email because a send mail test was launch on the Ametys server at: '" + /*request.getRequestURI()* +*/ "', and the following email address is set as the administrator address :'" + recipient + "'.")
061                          .withRecipient(recipient)
062                          .withSender(sender)
063                          .withHost(host)
064                          .withPort(port)
065                          .withSecurityProtocol(protocol)
066                          .withUser(user)
067                          .withPassword(password)
068                          .sendMail();
069        }
070        catch (Exception e)
071        {
072            throw new ItemCheckerTestFailureException(e.getMessage(), e);
073        }
074    }
075}