<commit_before>package org.lantern.http;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.lantern.Censored;
import org.lantern.JsonUtils;
import org.lantern.LanternClientConstants;
import org.lantern.LanternFeedback;
import org.lantern.XmppHandler;
import org.lantern.event.Events;
import org.lantern.event.InvitesChangedEvent;
import org.lantern.event.ResetEvent;
import org.lantern.state.InternalState;
import org.lantern.state.JsonModelModifier;
import org.lantern.state.LocationChangedEvent;
import org.lantern.state.Modal;
import org.lantern.state.Model;
import org.lantern.state.ModelIo;
import org.lantern.state.ModelService;
import org.lantern.state.Notification.MessageType;
import org.lantern.state.Settings.Mode;
import org.lantern.state.SyncPath;
import org.lantern.util.Desktop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class InteractionServlet extends HttpServlet {

    private final InternalState internalState;

    private enum Interaction {
        GET,
        GIVE,
        INVITE,
        CONTINUE,
        SETTINGS,
        CLOSE,
        RESET,
        SET,
        PROXIEDSITES,
        CANCEL,
        LANTERNFRIENDS,
        RETRY,
        REQUESTINVITE,
        CONTACT,
        ABOUT,
        ACCEPT,
        DECLINE,
        EXCEPTION
    }

    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * Generated serialization ID.
     */
    private static final long serialVersionUID = -8820179746803371322L;

    private final ModelService modelService;

    private final Model model;

    private final ModelIo modelIo;

    private final XmppHandler xmppHandler;

    private final Censored censored;

    private final LanternFeedback lanternFeedback;

    @Inject
    public InteractionServlet(final Model model,
        final ModelService modelService,
        final InternalState internalState,
        final ModelIo modelIo, final XmppHandler xmppHandler,
        final Censored censored, final LanternFeedback lanternFeedback) {
        this.model = model;
        this.modelService = modelService;
        this.internalState = internalState;
        this.modelIo = modelIo;
        this.xmppHandler = xmppHandler;
        this.censored = censored;
        this.lanternFeedback = lanternFeedback;
        Events.register(this);
    }

    @Override
    protected void doGet(final HttpServletRequest req,
        final HttpServletResponse resp) throws ServletException,
        IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(final HttpServletRequest req,
        final HttpServletResponse resp) throws ServletException,
        IOException {
        processRequest(req, resp);
    }

    protected void processRequest(final HttpServletRequest req,
        final HttpServletResponse resp) {
        final String uri = req.getRequestURI();
        log.debug("Received URI: {}", uri);
        final Map<String, String> params = HttpUtils.toParamMap(req);
        log.debug("Params: {}", params);
        final String interactionStr = StringUtils.substringAfterLast(uri, "/");//params.get("interaction");
        if (StringUtils.isBlank(interactionStr)) {
            log.debug("No interaction!!");
            HttpUtils.sendClientError(resp, "interaction argument required!");
            return;
        }

        log.debug("Headers: "+HttpUtils.getRequestHeaders(req));

        if (!"XMLHttpRequest".equals(req.getHeader("X-Requested-With"))) {
            HttpUtils.sendClientError(resp, "X-Requested-With header not set");
            return;
        }

        final int cl = req.getContentLength();
        String json = "";
        if (cl > 0) {
            try {
                json = IOUtils.toString(req.getInputStream());
            } catch (final IOException e) {
                log.error("Could not parse json?");
            }
        }

        log.debug("Body: '"+json+"'");

        final Interaction inter =
            Interaction.valueOf(interactionStr.toUpperCase());

        if (inter == Interaction.CLOSE) {
            handleClose(json);
        }

        final Modal modal = this.model.getModal();

        log.debug("processRequest: modal = {}, inter = {}, mode = {}", 
            modal, inter, this.model.getSettings().getMode());

        switch (modal) {
        case welcome:
            this.model.getSettings().setMode(Mode.none);
            switch (inter) {
            case GET:
                log.debug("Setting get mode");
                handleSetModeWelcome(Mode.get);<commit_msg>log exceptions sent to exception endpoint from ui #570<commit_after>package org.lantern.http;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.lantern.Censored;
import org.lantern.JsonUtils;
import org.lantern.LanternClientConstants;
import org.lantern.LanternFeedback;
import org.lantern.XmppHandler;
import org.lantern.event.Events;
import org.lantern.event.InvitesChangedEvent;
import org.lantern.event.ResetEvent;
import org.lantern.state.InternalState;
import org.lantern.state.JsonModelModifier;
import org.lantern.state.LocationChangedEvent;
import org.lantern.state.Modal;
import org.lantern.state.Model;
import org.lantern.state.ModelIo;
import org.lantern.state.ModelService;
import org.lantern.state.Notification.MessageType;
import org.lantern.state.Settings.Mode;
import org.lantern.state.SyncPath;
import org.lantern.util.Desktop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class InteractionServlet extends HttpServlet {

    private final InternalState internalState;

    private enum Interaction {
        GET,
        GIVE,
        INVITE,
        CONTINUE,
        SETTINGS,
        CLOSE,
        RESET,
        SET,
        PROXIEDSITES,
        CANCEL,
        LANTERNFRIENDS,
        RETRY,
        REQUESTINVITE,
        CONTACT,
        ABOUT,
        ACCEPT,
        DECLINE,
        EXCEPTION
    }

    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * Generated serialization ID.
     */
    private static final long serialVersionUID = -8820179746803371322L;

    private final ModelService modelService;

    private final Model model;

    private final ModelIo modelIo;

    private final XmppHandler xmppHandler;

    private final Censored censored;

    private final LanternFeedback lanternFeedback;

    @Inject
    public InteractionServlet(final Model model,
        final ModelService modelService,
        final InternalState internalState,
        final ModelIo modelIo, final XmppHandler xmppHandler,
        final Censored censored, final LanternFeedback lanternFeedback) {
        this.model = model;
        this.modelService = modelService;
        this.internalState = internalState;
        this.modelIo = modelIo;
        this.xmppHandler = xmppHandler;
        this.censored = censored;
        this.lanternFeedback = lanternFeedback;
        Events.register(this);
    }

    @Override
    protected void doGet(final HttpServletRequest req,
        final HttpServletResponse resp) throws ServletException,
        IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(final HttpServletRequest req,
        final HttpServletResponse resp) throws ServletException,
        IOException {
        processRequest(req, resp);
    }

    protected void processRequest(final HttpServletRequest req,
        final HttpServletResponse resp) {
        final String uri = req.getRequestURI();
        log.debug("Received URI: {}", uri);
        final Map<String, String> params = HttpUtils.toParamMap(req);
        log.debug("Params: {}", params);
        final String interactionStr = StringUtils.substringAfterLast(uri, "/");//params.get("interaction");
        if (StringUtils.isBlank(interactionStr)) {
            log.debug("No interaction!!");
            HttpUtils.sendClientError(resp, "interaction argument required!");
            return;
        }

        log.debug("Headers: "+HttpUtils.getRequestHeaders(req));

        if (!"XMLHttpRequest".equals(req.getHeader("X-Requested-With"))) {
            HttpUtils.sendClientError(resp, "X-Requested-With header not set");
            return;
        }

        final int cl = req.getContentLength();
        String json = "";
        if (cl > 0) {
            try {
                json = IOUtils.toString(req.getInputStream());
            } catch (final IOException e) {
                log.error("Could not parse json?");
            }
        }

        log.debug("Body: '"+json+"'");

        final Interaction inter =
            Interaction.valueOf(interactionStr.toUpperCase());

        if (inter == Interaction.CLOSE) {
            handleClose(json);
        }

        final Modal modal = this.model.getModal();

        log.debug("processRequest: modal = {}, inter = {}, mode = {}", 
            modal, inter, this.model.getSettings().getMode());

        if(inter == Interaction.EXCEPTION) {
            final ObjectMapper om = new ObjectMapper();
            Map<String, Object> map;
            StringBuilder logMessage = new StringBuilder();
            try {
                map = om.readValue(json, Map.class);
                for(Map.Entry<String, Object> entry : map.entrySet()) {
                    logMessage.append(
                        String.format("\t%s: %s\n", 
                            entry.getKey(), entry.getValue()
                        )
                    );
                }
                log.error("UI Exception:\n {}", logMessage.toString());
            } catch(Exception e) {
                log.error(e.toString());
            }
            return;
        }

        switch (modal) {
        case welcome:
            this.model.getSettings().setMode(Mode.none);
            switch (inter) {
            case GET:
                log.debug("Setting get mode");
                handleSetModeWelcome(Mode.get);
{'fraction_non_alphanumeric': 0.09751536200908362, 'fraction_numerical': 0.0043637011310000895, 'mean_word_length': 2.6120939208748792, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1559446', 'n_tokens_mistral': 3169, 'n_tokens_neox': 3054, 'n_words': 671}