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 //, Contextualizable cf Runtime-1065
033{
034//    private Context _context;
035
036//    public void contextualize(Context context) throws org.apache.avalon.framework.context.ContextException 
037//    {
038//        _context = context;
039//    }
040    
041    @Override
042    public void configure(Configuration configuration) throws ConfigurationException
043    {
044        Configuration[] config = configuration.getChild("linked-params").getChildren();
045        if (config.length != 7)
046        {
047            throw new ConfigurationException("The MailConnectionChecker should have 5 linked params in the right order: host, port, security.protocol, user, password, fromEmail, toEmail");
048        }
049    }
050
051    
052    @Override 
053    public void check(List<String> values) throws ItemCheckerTestFailureException
054    {
055        String host = values.get(0);
056        String portAsString = values.get(1);
057        String protocol = values.get(2);
058        String user = values.get(3);
059        String password = values.get(4);
060        String sender = values.get(5);
061        String recipient = values.get(6);
062        
063        // Request request = ContextHelper.getRequest(_context);
064        // reconstruire url back office a la place de host
065
066        long port = Integer.parseInt(portAsString);
067        try
068        {
069            SendMailHelper.sendMail("[Ametys] Test email", null, 
070                    "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 + "'.", 
071                    null, recipient, sender, null, null, false, false, host, port, protocol, user, password, false);
072        }
073        catch (Exception e)
074        {
075            throw new ItemCheckerTestFailureException(e.getMessage(), e);
076        }
077    }
078}