<commit_before>package ro.pub.cs.vmchecker.client.service;

import java.util.HashMap;
import java.util.Iterator;

import ro.pub.cs.vmchecker.client.event.AuthenticationEvent;
import ro.pub.cs.vmchecker.client.event.ErrorDisplayEvent;
import ro.pub.cs.vmchecker.client.model.ErrorResponse;
import ro.pub.cs.vmchecker.client.service.json.ErrorResponseDecoder;
import ro.pub.cs.vmchecker.client.service.json.JSONDecoder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
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.user.client.rpc.AsyncCallback;
import com.google.gwt.http.client.URL; 

public class Delegate<T> {
	
	private HandlerManager eventBus; 
	private RequestBuilder rb; 
	private boolean isGet;
	private String url; 
	
	public Delegate(HandlerManager eventBus, String url, boolean isGet) {
		this.eventBus = eventBus;
		this.isGet = isGet;
		this.url = url; 
		if (isGet) {
			rb = new RequestBuilder(RequestBuilder.GET, url);
		} else {
			rb = new RequestBuilder(RequestBuilder.POST, url);
		}
		rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
		rb.setTimeoutMillis(2000);
	}
	
	private String packParameters(HashMap<String, String> params) {
		StringBuffer result = new StringBuffer();  
		for (Iterator<String> it = params.keySet().iterator(); it.hasNext(); ) {
			String key = it.next(); 
			result.append(URL.encodeComponent(key, true) + "=" + URL.encodeComponent(params.get(key), true));
			if (it.hasNext()) {
				result.append('&'); 
			}
		}
		return result.toString();  		
	}

	public void parseError(AsyncCallback<T> callback, String text) {
		try {
			ErrorResponseDecoder decoder = new ErrorResponseDecoder(); 
			ErrorResponse serviceError = decoder.decode(text);
			if (serviceError.isAuthError()) {
				eventBus.fireEvent(new AuthenticationEvent(AuthenticationEvent.EventType.ERROR));  
			} else {
				eventBus.fireEvent(new ErrorDisplayEvent("[Service Error]" + serviceError.message, serviceError.trace)); 
			}
		} catch (Exception e) { 
			GWT.log("[parseError()]", e);
			/* unexpected format */
			eventBus.fireEvent(new ErrorDisplayEvent("[Service Error] Unknown format in response", 
					"<b>Service URL</b>: " + url + "<br/><b>Content</b>:<br/>" + text)); 
		}
	}
		<commit_msg>request timeout set to 10 seconds<commit_after>package ro.pub.cs.vmchecker.client.service;

import java.util.HashMap;
import java.util.Iterator;

import ro.pub.cs.vmchecker.client.event.AuthenticationEvent;
import ro.pub.cs.vmchecker.client.event.ErrorDisplayEvent;
import ro.pub.cs.vmchecker.client.model.ErrorResponse;
import ro.pub.cs.vmchecker.client.service.json.ErrorResponseDecoder;
import ro.pub.cs.vmchecker.client.service.json.JSONDecoder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
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.user.client.rpc.AsyncCallback;
import com.google.gwt.http.client.URL; 

public class Delegate<T> {
	
	public static final int requestTimeoutMillis = 10000; /* 10 seconds */
	
	private HandlerManager eventBus; 
	private RequestBuilder rb; 
	private boolean isGet;
	private String url; 
	
	public Delegate(HandlerManager eventBus, String url, boolean isGet) {
		this.eventBus = eventBus;
		this.isGet = isGet;
		this.url = url; 
		if (isGet) {
			rb = new RequestBuilder(RequestBuilder.GET, url);
		} else {
			rb = new RequestBuilder(RequestBuilder.POST, url);
		}
		rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
		rb.setTimeoutMillis(requestTimeoutMillis);
	}
	
	private String packParameters(HashMap<String, String> params) {
		StringBuffer result = new StringBuffer();  
		for (Iterator<String> it = params.keySet().iterator(); it.hasNext(); ) {
			String key = it.next(); 
			result.append(URL.encodeComponent(key, true) + "=" + URL.encodeComponent(params.get(key), true));
			if (it.hasNext()) {
				result.append('&'); 
			}
		}
		return result.toString();  		
	}

	public void parseError(AsyncCallback<T> callback, String text) {
		try {
			ErrorResponseDecoder decoder = new ErrorResponseDecoder(); 
			ErrorResponse serviceError = decoder.decode(text);
			if (serviceError.isAuthError()) {
				eventBus.fireEvent(new AuthenticationEvent(AuthenticationEvent.EventType.ERROR));  
			} else {
				eventBus.fireEvent(new ErrorDisplayEvent("[Service Error]" + serviceError.message, serviceError.trace)); 
			}
		} catch (Exception e) { 
			GWT.log("[parseError()]", e);
			/* unexpected format */
			eventBus.fireEvent(new ErrorDisplayEvent("[Service Error] Unknown format in response", 
					"<b>Service URL</b>: " + url + "<br/><b>Content</b>:<br/>" + text)); 
		}
	}
		
{'fraction_non_alphanumeric': 0.13522012578616352, 'fraction_numerical': 0.0025550314465408804, 'mean_word_length': 4.5556768558951966, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1919626', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1622, 'n_words': 303}