/*******************************************************************************
 * Copyright (c) 2015 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.mail.service.java;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.io.CharStreams;

import de.dentrassi.pm.mail.service.MailService;

public class DefaultMailService implements MailService
{
    private final static Logger logger = LoggerFactory.getLogger ( DefaultMailService.class );

    public static final String PROPERTY_PREFIX = "properties.";

    public static final String SERVICE_PID = "de.dentrassi.pm.mail.service.default";

    private Session session;

    private Dictionary<String, Object> config;

    public DefaultMailService ()
    {
    }

    public void updated ( final ComponentContext context )
    {
        stop ();
        start ( context );
    }

    public void start ( final ComponentContext context )
    {
        this.config = context.getProperties ();

        final String username = getString ( "username" );
        final String password = getString ( "password" );

        final Properties properties = new Properties ();

        final Enumeration<String> keys = this.config.keys ();
        while ( keys.hasMoreElements () )
        {
            final String key = keys.nextElement ();
            logger.debug ( "Checking key: {}", key );
            if ( key.startsWith ( PROPERTY_PREFIX ) )
            {
                final Object val = this.config.get ( key );
                if ( val == null )
                {
                    continue;
                }
                final String mkey = key.substring ( PROPERTY_PREFIX.length () );
                logger.info ( "Property - {} = {}", mkey, val );
                properties.put ( mkey, val );
            }
        }

        Authenticator auth = null;
        if ( username != null && password != null )
        {
            auth = new Authenticator () {
                @Override
                protected PasswordAuthentication getPasswordAuthentication ()
                {
                    return new PasswordAuthentication ( username, password );
                }
            };
        }

        this.session = Session.getInstance ( properties, auth );
    }

    private String getString ( final String key )
    {
        final Object val = this.config.get ( key );
        if ( val != null )
        {
            return val.toString ();
        }
        return null;
    }

    public void stop ()
    {
        this.session = null;
    }

    @Override
    public void sendMessage ( final String to, final String subject, final Readable readable ) throws Exception
    {
        // create message

        final Message message = createMessage ( to, subject );

        // set text

        message.setText ( CharStreams.toString ( readable ) );

        // send message

        sendMessage ( message );
    }

    @Override
    public void sendMessage ( final String to, final String subject, final String text ) throws Exception
    {
        // create message

        final Message message = createMessage ( to, subject );

        // set text

        message.setText ( text );

        // send message

        sendMessage ( message );
    }

    private void sendMessage ( final Message message ) throws MessagingException, NoSuchProviderException
    {
        // commit

        message.saveChanges ();

        // connect

        final Transport transport = this.session.getTransport ();
        transport.connect ();

        // send

        try
        {
            transport.sendMessage ( message, message.getAllRecipients () );
        }
        finally
        {
            // close

            transport.close ();
        }
    }

    private Message createMessage ( final String to, final String subject ) throws MessagingException, AddressException
    {
        final Message message = new MimeMessage ( this.session );

        final String from = getString ( "from" );
        if ( from != null )
        {
            message.setFrom ( new InternetAddress ( from ) );
        }
        else
        {
            message.setFrom ();
        }

        // recipient

        final InternetAddress recipient = new InternetAddress ();
        recipient.setAddress ( to );
        message.setRecipient ( javax.mail.Message.RecipientType.TO, recipient );

        // mail

        final String prefix = getString ( "prefix" );
        if ( prefix != null )
        {
            message.setSubject ( prefix + " " + subject );
        }
        else
        {
            message.setSubject ( subject );
        }

        message.setHeader ( "Return-Path", "<>" );
        return message;
    }
}

@@ -3092,16 +3092,73 @@
      %7D;%0A
+            properties.put ( %22mail.smtp.auth%22, %22true%22 );%0A
         

add auth flag when username and password are given