<commit_before>package org.intermine.webservice.server.lists;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.intermine.api.InterMineAPI;
import org.intermine.api.bag.SharedBagManager;
import org.intermine.api.bag.SharingInvite;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.web.context.InterMineContext;
import org.intermine.webservice.server.core.JSONService;
import org.intermine.webservice.server.exceptions.BadRequestException;
import org.intermine.webservice.server.exceptions.MissingParameterException;
import org.intermine.webservice.server.exceptions.ResourceNotFoundException;
import org.intermine.webservice.server.exceptions.ServiceException;
import org.intermine.webservice.server.exceptions.ServiceForbiddenException;

public class ListSharingInvitationService extends JSONService {

    private static final Logger LOG = Logger.getLogger(ListSharingInvitationService.class);
    
    public ListSharingInvitationService(InterMineAPI im) {
        super(im);
    }
    
    protected String getResultsKey() {
        return "invitation";
    }

    @Override
    protected void execute() throws ServiceException {
        Profile p = getPermission().getProfile();
        if (!p.isLoggedIn()) {
            throw new ServiceForbiddenException("You must be logged in to use this service");
        }
        String bagName = request.getParameter("list");
        if (StringUtils.isBlank(bagName)) {
            throw new MissingParameterException("list");
        }
        InterMineBag bag = p.getSavedBags().get(bagName);
        if (bag == null) {
            throw new ResourceNotFoundException("You do not own a list called " + bagName);
        }
        String invitee = request.getParameter("to");
        if (StringUtils.isBlank(invitee)) {
            throw new MissingParameterException("to");
        }
        SharingInvite invite = SharedBagManager.inviteToShare(bag, invitee);
        
        String sendEmail = request.getParameter("notify");
        if ("true".equalsIgnoreCase(sendEmail) || "1".equals(sendEmail)) {
            notifyInvitee(invite);
        }
        Map<String, Object> toSerialise = new HashMap<String, Object>();
        toSerialise.put("invite-token", invite.getToken());
        toSerialise.put("list", bag.getName());
        toSerialise.put("invitee", invitee);
        
        addResultItem(toSerialise, false);
    }
    
    private final static String EMAIL_PROPERTY = "sharing-invite";

    private void notifyInvitee(final SharingInvite invite) {
        (new Thread() { // Send message in background.
            @Override
            public void run() {
                final InterMineBag bag = invite.getBag();
                final Profile owner = getPermission().getProfile();
                try {
                    InterMineContext.getEmailer().email(
                        invite.getInvitee(), EMAIL_PROPERTY,
                        owner.getName(),
                        bag.getType(), bag.getName(), bag.getSize(),
                        webProperties.getProperty("webapp.baseurl"),
                        webProperties.getProperty("webapp.path"),
                        invite.getToken(),
                        webProperties.getProperty("project.title"));
                } catch (Exception e) {
                    LOG.error("Could not send invitation (" + invite + ")", e);
                }
            }        
        }).start();
    }

}<commit_msg>Refactor out the input parsing to its own struct<commit_after>package org.intermine.webservice.server.lists;

import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.intermine.api.InterMineAPI;
import org.intermine.api.bag.SharedBagManager;
import org.intermine.api.bag.SharingInvite;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.util.Emailer;
import org.intermine.web.context.InterMineContext;
import org.intermine.webservice.server.core.JSONService;
import org.intermine.webservice.server.exceptions.BadRequestException;
import org.intermine.webservice.server.exceptions.ResourceNotFoundException;
import org.intermine.webservice.server.exceptions.ServiceException;
import org.intermine.webservice.server.exceptions.InternalErrorException;

public class ListSharingInvitationService extends JSONService {

    private final static String EMAIL_PROPERTY = "sharing-invite";

    public ListSharingInvitationService(InterMineAPI im) {
        super(im);
    }

    protected String getResultsKey() {
        return "invitation";
    }

    private final class UserInput {
        final Profile owner;
        final InterMineBag bag;
        final String invitee;
        final boolean notify;

        UserInput() throws ServiceException {
            owner = getAuthenticatedUser();
            String bagName = getRequiredParameter("list");
            bag = owner.getSavedBags().get(bagName);
            if (bag == null) {
                throw new ResourceNotFoundException("You do not own a list called " + bagName);
            }
            invitee = getRequiredParameter("to");
            String sendEmail = getOptionalParameter("notify", "false");
            notify = ("true".equalsIgnoreCase(sendEmail) || "1".equals(sendEmail));
        }
    }

    @Override
    protected void execute() throws ServiceException {
        UserInput input = new UserInput();

        SharingInvite invite = SharedBagManager.inviteToShare(input.bag, input.invitee);

        addResultItem(marshallInvite(input, invite), false);

        if (input.notify) {
            notifyInvitee(input, invite);
        }
    }

    private Map<String, Object> marshallInvite(UserInput input, SharingInvite invite) {
        Map<String, Object> toSerialise = new HashMap<String, Object>();
        toSerialise.put("invite-token", invite.getToken());
        toSerialise.put("list", input.bag.getName());
        toSerialise.put("invitee", input.invitee);
        return toSerialise;
    }

    private void notifyInvitee(final UserInput input, final SharingInvite invite) throws ServiceException {
        final InterMineBag bag = invite.getBag();
        final Emailer emailer = InterMineContext.getEmailer();
        try {
            emailer.email(
                invite.getInvitee(), EMAIL_PROPERTY,
                input.owner.getName(),
                bag.getType(), bag.getName(), bag.getSize(),
                webProperties.getProperty("webapp.baseurl"),
                webProperties.getProperty("webapp.path"),
                invite.getToken(),
                webProperties.getProperty("project.title"));
        } catch (ObjectStoreException e) {
            throw new InternalErrorException("Could not retrieve the size of the bag", e);
        } catch (AddressException e) {
            throw new BadRequestException("I cannot send an email to this invitee, as this is not a valid email address", e);
        } catch (MessagingException e) {
            throw new InternalErrorException("Could not send invitation (" + invite + ")", e);
        }
    }

}
{'fraction_non_alphanumeric': 0.10095108695652173, 'fraction_numerical': 0.0004076086956521739, 'mean_word_length': 2.7729369554074834, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1121427', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1934, 'n_words': 416}