<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.mail.script;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.context.Execution;
import org.xwiki.mail.MimeBodyPartFactory;
import org.xwiki.mail.internal.ExtendedMimeMessage;
import org.xwiki.stability.Unstable;

/**
 * Extends {@link javax.mail.internet.MimeMessage} with additional helper methods for scripts.
 *
 * @version $Id$
 * @since 7.1M2
 */
@Unstable
public class ScriptMimeMessage extends ExtendedMimeMessage
{
    private ComponentManager componentManager;

    private Execution execution;

    /**
     * @param execution used to get the Execution Context and store an error in it if the send fails
     * @param componentManager used to dynamically load all {@link MimeBodyPartFactory} components
     */
    // Note: This method is package private voluntarily so that it's not part of the API (as this class is public),
    // since it's only needed by the MailSenderScriptService and nobody else should be able to construct an instance
    // of it!
    ScriptMimeMessage(MimeMessage sourceMessage, Execution execution, ComponentManager componentManager)
        throws MessagingException
    {
        super(sourceMessage);
        this.execution = execution;
        this.componentManager = componentManager;
    }

    /**
     * @param execution used to get the Execution Context and store an error in it if the send fails
     * @param componentManager used to dynamically load all {@link MimeBodyPartFactory} components
     */
    // Note: This method is package private voluntarily so that it's not part of the API (as this class is public),
    // since it's only needed by the MailSenderScriptService and nobody else should be able to construct an instance
    // of it!
    ScriptMimeMessage(Execution execution, ComponentManager componentManager)
    {
        super();
        this.execution = execution;
        this.componentManager = componentManager;
    }

    /**
     * Add some content to the mail to be sent. Can be called several times to add different content type to the mail.
     *
     * @return the Mime Body Part object that was added. Returning it allows script to make modifications to that body
     *         part after it's been set (get/set some headers, etc)
     * @param mimeType the mime type of the content parameter
     * @param content the content to include in the mail
     */
    public BodyPart addPart(String mimeType, Object content)
    {
        return addPart(mimeType, content, Collections.<String, Object>emptyMap());
    }

    /**
     * Add some content to the mail to be sent. Can be called several times to add different content type to the mail.
     *
     * @return the Mime Body Part object that was added. Returning it allows script to make modifications to that body
     *         part after it's been set (get/set some headers, etc)
     * @param mimeType the mime type of the content parameter
     * @param content the content to include in the mail
     * @param parameters the list of extra parameters. This is used for example to pass alternate content for the mail
     *        using the {@code alternate} key in the HTML Mime Body Part Factory. Mail headers can also be passed using
     *        the {@code headers} key with a {@code Map&lt;String, String&gt;} value containing header keys and values
     */
    public BodyPart addPart(String mimeType, Object content, Map<String, Object> parameters)
    {
        BodyPart bodyPart;

        try {
            MimeBodyPartFactory factory = getBodyPartFactory(mimeType, content.getClass());

            // Pass the mime type in the parameters so that generic Mime Body Part factories can use it.
            // Note that if the use has already passed a "mimetype" parameter then we don't override it!
            Map<String, Object> enhancedParameters = new HashMap<>();
            enhancedParameters.put("mimetype", mimeType);
            enhancedParameters.putAll(parameters);

            Multipart multipart = getMultipart();
            bodyPart = factory.create(content, enhancedParameters);
            multipart.addBodyPart(bodyPart);
        } catch (Exception e) {
            // Save the exception for reporting through the script services's getError() API
            setError(e);
            bodyPart = null;
        }

        return bodyPart;
    }<commit_msg>[Misc] Typo<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.mail.script;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.context.Execution;
import org.xwiki.mail.MimeBodyPartFactory;
import org.xwiki.mail.internal.ExtendedMimeMessage;
import org.xwiki.stability.Unstable;

/**
 * Extends {@link javax.mail.internet.MimeMessage} with additional helper methods for scripts.
 *
 * @version $Id$
 * @since 7.1M2
 */
@Unstable
public class ScriptMimeMessage extends ExtendedMimeMessage
{
    private ComponentManager componentManager;

    private Execution execution;

    /**
     * @param execution used to get the Execution Context and store an error in it if the send fails
     * @param componentManager used to dynamically load all {@link MimeBodyPartFactory} components
     */
    // Note: This method is package private voluntarily so that it's not part of the API (as this class is public),
    // since it's only needed by the MailSenderScriptService and nobody else should be able to construct an instance
    // of it!
    ScriptMimeMessage(MimeMessage sourceMessage, Execution execution, ComponentManager componentManager)
        throws MessagingException
    {
        super(sourceMessage);
        this.execution = execution;
        this.componentManager = componentManager;
    }

    /**
     * @param execution used to get the Execution Context and store an error in it if the send fails
     * @param componentManager used to dynamically load all {@link MimeBodyPartFactory} components
     */
    // Note: This method is package private voluntarily so that it's not part of the API (as this class is public),
    // since it's only needed by the MailSenderScriptService and nobody else should be able to construct an instance
    // of it!
    ScriptMimeMessage(Execution execution, ComponentManager componentManager)
    {
        super();
        this.execution = execution;
        this.componentManager = componentManager;
    }

    /**
     * Add some content to the mail to be sent. Can be called several times to add different content type to the mail.
     *
     * @return the Mime Body Part object that was added. Returning it allows script to make modifications to that body
     *         part after it's been set (get/set some headers, etc)
     * @param mimeType the mime type of the content parameter
     * @param content the content to include in the mail
     */
    public BodyPart addPart(String mimeType, Object content)
    {
        return addPart(mimeType, content, Collections.<String, Object>emptyMap());
    }

    /**
     * Add some content to the mail to be sent. Can be called several times to add different content type to the mail.
     *
     * @return the Mime Body Part object that was added. Returning it allows script to make modifications to that body
     *         part after it's been set (get/set some headers, etc)
     * @param mimeType the mime type of the content parameter
     * @param content the content to include in the mail
     * @param parameters the list of extra parameters. This is used for example to pass alternate content for the mail
     *        using the {@code alternate} key in the HTML Mime Body Part Factory. Mail headers can also be passed using
     *        the {@code headers} key with a {@code Map&lt;String, String&gt;} value containing header keys and values
     */
    public BodyPart addPart(String mimeType, Object content, Map<String, Object> parameters)
    {
        BodyPart bodyPart;

        try {
            MimeBodyPartFactory factory = getBodyPartFactory(mimeType, content.getClass());

            // Pass the mime type in the parameters so that generic Mime Body Part factories can use it.
            // Note that if the user has already passed a "mimetype" parameter then we don't override it!
            Map<String, Object> enhancedParameters = new HashMap<>();
            enhancedParameters.put("mimetype", mimeType);
            enhancedParameters.putAll(parameters);

            Multipart multipart = getMultipart();
            bodyPart = factory.create(content, enhancedParameters);
            multipart.addBodyPart(bodyPart);
        } catch (Exception e) {
            // Save the exception for reporting through the script services's getError() API
            setError(e);
            bodyPart = null;
        }

        return bodyPart;
    }
{'fraction_non_alphanumeric': 0.0677078788936393, 'fraction_numerical': 0.0027920774801500742, 'mean_word_length': 3.1954612005856515, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383392', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2821, 'n_words': 1277}