<commit_before>package nl.sense_os.commonsense.client.auth.registration;

import java.util.logging.Logger;

import nl.sense_os.commonsense.client.auth.login.LoginEvents;
import nl.sense_os.commonsense.client.common.ajax.AjaxEvents;
import nl.sense_os.commonsense.client.common.constants.Urls;
import nl.sense_os.commonsense.client.utility.Md5Hasher;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;

public class RegisterController extends Controller {

    private static final Logger LOG = Logger.getLogger(RegisterController.class.getName());
    private View form;

    public RegisterController() {
        registerEventTypes(RegisterEvents.Show);
        registerEventTypes(RegisterEvents.RegisterRequest, RegisterEvents.AjaxRegisterSuccess,
                RegisterEvents.AjaxRegisterFailure);
    }

    @Override
    public void handleEvent(AppEvent event) {
        final EventType type = event.getType();

        if (type.equals(RegisterEvents.RegisterRequest)) {
            // LOG.fine("RegisterRequest");
            final String username = event.<String> getData("username");
            final String password = event.<String> getData("password");
            final String name = event.<String> getData("name");
            final String surname = event.<String> getData("surname");
            final String email = event.<String> getData("email");
            final String mobile = event.<String> getData("mobile");
            register(username, password, name, surname, email, mobile);

        } else if (type.equals(RegisterEvents.AjaxRegisterSuccess)) {
            // LOG.fine("AjaxRegisterSuccess");
            final String response = event.<String> getData("response");
            final String username = event.<String> getData("username");
            final String password = event.<String> getData("password");
            onRegisterSuccess(response, username, password);

        } else if (type.equals(RegisterEvents.AjaxRegisterFailure)) {
            LOG.warning("AjaxRegisterFailure");
            onRegisterFailure();

        } else {
            forwardToView(form, event);
        }

    }

    @Override
    protected void initialize() {
        super.initialize();
        form = new RegisterPanel(this);
    }

    private void onRegisterFailure() {
        forwardToView(form, new AppEvent(RegisterEvents.RegisterFailure));
    }

    private void onRegisterSuccess(String response, String username, String password) {
        AppEvent loginRequest = new AppEvent(LoginEvents.LoginRequest);
        loginRequest.setData("username", username);
        loginRequest.setData("password", password);
        Dispatcher.forwardEvent(loginRequest);

        forwardToView(form, new AppEvent(RegisterEvents.RegisterSuccess));
    }

    private void register(final String username, final String password, String name,
            String surname, String email, String mobile) {

        // prepare request properties
        final String url = Urls.USERS + ".json";

        // create request body
        String userJson = "\"user\":{";
        userJson += "\"username\":\"" + username + "\"";
        userJson += "," + "\"password\":\"" + <PASSWORD>(password) + "\"";
        userJson += "," + "\"name\":\"" + name + "\"";
        userJson += "," + "\"surname\":\"" + surname + "\"";
        userJson += "," + "\"email\":\"" + email + "\"";
        userJson += "," + "\"mobile\":\"" + mobile + "\"";
        userJson += "}";
        final String body = "{" + userJson + "}";

        // prepare request callback
        RequestCallback callback = new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                LOG.warning("POST registration onError callback: " + exception.getMessage());
                onRegisterFailure();
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
                LOG.finest("POST registration response received: " + response.getStatusText());
                int statusCode = response.getStatusCode();
                if (201 == statusCode) {
                    onRegisterSuccess(response.getText(), username, password);
                } else {
                    LOG.warning("POST registration returned incorrect status: " + statusCode);
                    onRegisterFailure();
                }
            }
        };

        // send request
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
        try {
            builder.sendRequest(body, callback);
        } catch (RequestException e) {
            LOG.warning("POST registration request threw exception: " + e.getMessage());
            onRegisterFailure();
        }
    }

    private void xhrRegister(String username, String password, String name, String surname,
            String email, String mobile) {<commit_msg>Changed registration code to use regular RequestBuilder instead of the old complicated XHR JSNI solution.<commit_after>package nl.sense_os.commonsense.client.auth.registration;

import java.util.logging.Logger;

import nl.sense_os.commonsense.client.auth.login.LoginEvents;
import nl.sense_os.commonsense.client.common.ajax.AjaxEvents;
import nl.sense_os.commonsense.client.common.constants.Urls;
import nl.sense_os.commonsense.client.utility.Md5Hasher;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;

public class RegisterController extends Controller {

    private static final Logger LOG = Logger.getLogger(RegisterController.class.getName());
    private View form;

    public RegisterController() {
        registerEventTypes(RegisterEvents.Show);
        registerEventTypes(RegisterEvents.RegisterRequest, RegisterEvents.AjaxRegisterSuccess,
                RegisterEvents.AjaxRegisterFailure);
    }

    @Override
    public void handleEvent(AppEvent event) {
        final EventType type = event.getType();

        if (type.equals(RegisterEvents.RegisterRequest)) {
            // LOG.fine("RegisterRequest");
            final String username = event.<String> getData("username");
            final String password = event.<String> getData("password");
            final String name = event.<String> getData("name");
            final String surname = event.<String> getData("surname");
            final String email = event.<String> getData("email");
            final String mobile = event.<String> getData("mobile");
            register(username, password, name, surname, email, mobile);

        } else if (type.equals(RegisterEvents.AjaxRegisterSuccess)) {
            // LOG.fine("AjaxRegisterSuccess");
            final String response = event.<String> getData("response");
            final String username = event.<String> getData("username");
            final String password = event.<String> getData("password");
            onRegisterSuccess(response, username, password);

        } else if (type.equals(RegisterEvents.AjaxRegisterFailure)) {
            LOG.warning("AjaxRegisterFailure");
            onRegisterFailure();

        } else {
            forwardToView(form, event);
        }

    }

    @Override
    protected void initialize() {
        super.initialize();
        form = new RegisterPanel(this);
    }

    private void onRegisterFailure() {
        forwardToView(form, new AppEvent(RegisterEvents.RegisterFailure));
    }

    private void onRegisterSuccess(String response, String username, String password) {
        AppEvent loginRequest = new AppEvent(LoginEvents.LoginRequest);
        loginRequest.setData("username", username);
        loginRequest.setData("password", password);
        Dispatcher.forwardEvent(loginRequest);

        forwardToView(form, new AppEvent(RegisterEvents.RegisterSuccess));
    }

    private void register(final String username, final String password, String name,
            String surname, String email, String mobile) {

        // prepare request properties
        final String url = Urls.USERS + ".json";

        // create request body
        String userJson = "\"user\":{";
        userJson += "\"username\":\"" + username + "\"";
        userJson += "," + "\"password\":\"" + <PASSWORD>(password) + "\"";
        userJson += "," + "\"name\":\"" + name + "\"";
        userJson += "," + "\"surname\":\"" + surname + "\"";
        userJson += "," + "\"email\":\"" + email + "\"";
        userJson += "," + "\"mobile\":\"" + mobile + "\"";
        userJson += "}";
        final String body = "{" + userJson + "}";

        // prepare request callback
        RequestCallback callback = new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                LOG.warning("POST registration onError callback: " + exception.getMessage());
                onRegisterFailure();
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
                LOG.finest("POST registration response received: " + response.getStatusText());
                int statusCode = response.getStatusCode();
                if (Response.SC_CREATED == statusCode) {
                    onRegisterSuccess(response.getText(), username, password);
                } else {
                    LOG.warning("POST registration returned incorrect status: " + statusCode);
                    onRegisterFailure();
                }
            }
        };

        // send request
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
        try {
            builder.sendRequest(body, callback);
        } catch (RequestException e) {
            LOG.warning("POST registration request threw exception: " + e.getMessage());
            onRegisterFailure();
        }
    }

    private void xhrRegister(String username, String password, String name, String surname,
            String email, String mobile) {
{'fraction_non_alphanumeric': 0.12015575746337845, 'fraction_numerical': 0.00046356387910254034, 'mean_word_length': 2.6430260047281324, 'pattern_counts': {'":': 14, '<': 23, '<?xml version=': 0, '>': 23, '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': '1893351', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2688, 'n_words': 614}