<commit_before>package com.itmill.toolkit.terminal.gwt.client;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import com.google.gwt.core.client.EntryPoint;
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;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HasFocus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.itmill.toolkit.terminal.gwt.client.ui.ContextMenu;
import com.itmill.toolkit.terminal.gwt.client.ui.IView;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ApplicationConnection implements FocusListener {

	private String appUri;

	private HashMap resourcesMap = new HashMap();

	private static Console console;

	private Vector pendingVariables = new Vector();

	private HashMap idToPaintable = new HashMap();

	private HashMap paintableToId = new HashMap();

	private final WidgetSet widgetSet;

	private ContextMenu contextMenu = null;

	private IView view = new IView();

	public ApplicationConnection(WidgetSet widgetSet) {
		this.widgetSet = widgetSet;
		appUri = getAppUri();

		if (isDebugMode()) {
			console = new DebugConsole();
		} else {
			console = new NullConsole();
		}

		makeUidlRequest("repaintAll=1");

		// TODO remove hardcoded id name
		RootPanel.get("itmtk-ajax-window").add(view);
	}

	public static Console getConsole() {
		return console;
	}

	private native static boolean isDebugMode() /*-{
	 var uri = $wnd.location;
	 var re = /debug[^\/]*$/;
	 return re.test(uri);
	 }-*/;

	public native String getAppUri()/*-{
	 return $wnd.itmtk.appUri;
	 }-*/;

	private native String getPathInfo()/*-{
	 return $wnd.itmtk.pathInfo;
	 }-*/;

	private void makeUidlRequest(String requestData) {
		console.log("Making UIDL Request with params: " + requestData);
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, appUri
				+ "/UIDL" + getPathInfo());
		rb.setHeader("Content-Type",
				"application/x-www-form-urlencoded; charset=utf-8");
		try {
			rb.sendRequest(requestData, new RequestCallback() {
				public void onError(Request request, Throwable exception) {
					// TODO Better reporting to user
					console.error("Got error");
				}

				public void onResponseReceived(Request request,
						Response response) {
					handleReceivedJSONMessage(response);
				}

			});

		} catch (RequestException e) {
			// TODO Better reporting to user
			console.error(e.getMessage());
		}
	}

	private void handleReceivedJSONMessage(Response response) {
		Date start = new Date();
		String jsonText = response.getText().substring(3) + "}";
		// TODO This should be a console message, right?
		System.out.println(jsonText);
		JSONValue json;
		try {
			json = JSONParser.parse(jsonText);
		} catch (com.google.gwt.json.client.JSONException e) {
			console.log(e.getMessage() + " - Original JSON-text:");
			console.log(jsonText);
			return;
		}
		// Handle redirect
		JSONObject redirect = (JSONObject) ((JSONObject) json).get("redirect");
		if (redirect != null) {
			JSONString url = (JSONString) redirect.get("url");
			if (url != null) {
				console.log("redirecting to " + url.stringValue());
				redirect(url.stringValue());
				return;
			}
		}

		// Store resources
		JSONObject resources = (JSONObject) ((JSONObject) json)
				.get("resources");
		for (Iterator i = resources.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			resourcesMap.put(key, ((JSONString) resources.get(key))
					.stringValue());<commit_msg>Removed JSON debug message from console<commit_after>package com.itmill.toolkit.terminal.gwt.client;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import com.google.gwt.core.client.EntryPoint;
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;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HasFocus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.itmill.toolkit.terminal.gwt.client.ui.ContextMenu;
import com.itmill.toolkit.terminal.gwt.client.ui.IView;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ApplicationConnection implements FocusListener {

	private String appUri;

	private HashMap resourcesMap = new HashMap();

	private static Console console;

	private Vector pendingVariables = new Vector();

	private HashMap idToPaintable = new HashMap();

	private HashMap paintableToId = new HashMap();

	private final WidgetSet widgetSet;

	private ContextMenu contextMenu = null;

	private IView view = new IView();

	public ApplicationConnection(WidgetSet widgetSet) {
		this.widgetSet = widgetSet;
		appUri = getAppUri();

		if (isDebugMode()) {
			console = new DebugConsole();
		} else {
			console = new NullConsole();
		}

		makeUidlRequest("repaintAll=1");

		// TODO remove hardcoded id name
		RootPanel.get("itmtk-ajax-window").add(view);
	}

	public static Console getConsole() {
		return console;
	}

	private native static boolean isDebugMode() /*-{
	 var uri = $wnd.location;
	 var re = /debug[^\/]*$/;
	 return re.test(uri);
	 }-*/;

	public native String getAppUri()/*-{
	 return $wnd.itmtk.appUri;
	 }-*/;

	private native String getPathInfo()/*-{
	 return $wnd.itmtk.pathInfo;
	 }-*/;

	private void makeUidlRequest(String requestData) {
		console.log("Making UIDL Request with params: " + requestData);
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, appUri
				+ "/UIDL" + getPathInfo());
		rb.setHeader("Content-Type",
				"application/x-www-form-urlencoded; charset=utf-8");
		try {
			rb.sendRequest(requestData, new RequestCallback() {
				public void onError(Request request, Throwable exception) {
					// TODO Better reporting to user
					console.error("Got error");
				}

				public void onResponseReceived(Request request,
						Response response) {
					handleReceivedJSONMessage(response);
				}

			});

		} catch (RequestException e) {
			// TODO Better reporting to user
			console.error(e.getMessage());
		}
	}

	private void handleReceivedJSONMessage(Response response) {
		Date start = new Date();
		String jsonText = response.getText().substring(3) + "}";
		JSONValue json;
		try {
			json = JSONParser.parse(jsonText);
		} catch (com.google.gwt.json.client.JSONException e) {
			console.log(e.getMessage() + " - Original JSON-text:");
			console.log(jsonText);
			return;
		}
		// Handle redirect
		JSONObject redirect = (JSONObject) ((JSONObject) json).get("redirect");
		if (redirect != null) {
			JSONString url = (JSONString) redirect.get("url");
			if (url != null) {
				console.log("redirecting to " + url.stringValue());
				redirect(url.stringValue());
				return;
			}
		}

		// Store resources
		JSONObject resources = (JSONObject) ((JSONObject) json)
				.get("resources");
		for (Iterator i = resources.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			resourcesMap.put(key, ((JSONString) resources.get(key))
					.stringValue());
{'fraction_non_alphanumeric': 0.13455619844884895, 'fraction_numerical': 0.000738643358365136, 'mean_word_length': 4.14828897338403, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '679804', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2524, 'n_words': 557}