<commit_before>/*
 * Copyright 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.footlights.ui.web;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.collect.Maps;

import me.footlights.core.Footlights;


/** Acts as a master server for Basic UI, JavaScript and Ajax */
public class MasterServer implements Runnable
{
	public MasterServer(int port, Footlights footlights,
		AjaxServer ajaxServer, StaticContentServer staticServer)
	{
		this.port = port;
		this.servers = Maps.newHashMap();

		servers.put("", staticServer);
		servers.put("static", staticServer);

		servers.put("ajax", ajaxServer);
	}


	public void run()
	{
		log.entering(getClass().getName(), "run");

		try
		{
			ServerSocket serverSocket = new ServerSocket(port);

			done = false;
			while (!done)
			{
				log.fine("Waiting for connection...");
				Socket socket = serverSocket.accept();
				log.fine("Accepted conncetion from " + socket);

				BufferedReader in =
					new BufferedReader(
						new InputStreamReader(
							socket.getInputStream()));

				String rawRequest = in.readLine();
				if (rawRequest == null) continue;

				Request request = Request.parse(rawRequest);
				log.info("Request: " + request.toString());

				Response response;
				try
				{
					WebServer server = servers.get(request.prefix());
					if (server == null)
						throw new FileNotFoundException(request.path());

					else response = server.handle(request.shift());
				}
				catch(FileNotFoundException e) { response = Response.error(e); }
				catch(SecurityException e) { response = Response.error(e); }
				catch(Throwable t) { response = Response.error(t); }

				if (response.isError())
					log.warning("Error handling " + request + ": " + response.statusMessage());
				else
					log.fine("Response: " + response);

				try
				{
					response.write(socket.getOutputStream());
					log.finer("Sent response: " + response);
					socket.close();
				}
				catch(SocketException e)
				{
					log.log(Level.SEVERE, "Uncaught SocketException", e);
				}
			}
		}
		catch(Exception e)
		{
			System.err.println("Web UI dying:");
			e.printStackTrace(System.err);
		}
	}


	private static final Logger log = Logger.getLogger(MasterServer.class.getName());

	private final int port;
	private boolean done;

	private final Map<String,WebServer> servers;
}<commit_msg>Improve error logging in MasterServer.<commit_after>/*
 * Copyright 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.footlights.ui.web;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.collect.Maps;

import me.footlights.core.Footlights;


/** Acts as a master server for Basic UI, JavaScript and Ajax */
public class MasterServer implements Runnable
{
	public MasterServer(int port, Footlights footlights,
		AjaxServer ajaxServer, StaticContentServer staticServer)
	{
		this.port = port;
		this.servers = Maps.newHashMap();

		servers.put("", staticServer);
		servers.put("static", staticServer);

		servers.put("ajax", ajaxServer);
	}


	public void run()
	{
		log.entering(getClass().getName(), "run");

		try
		{
			ServerSocket serverSocket = new ServerSocket(port);

			done = false;
			while (!done)
			{
				log.fine("Waiting for connection...");
				Socket socket = serverSocket.accept();
				log.fine("Accepted conncetion from " + socket);

				BufferedReader in =
					new BufferedReader(
						new InputStreamReader(
							socket.getInputStream()));

				String rawRequest = in.readLine();
				if (rawRequest == null) continue;

				Request request = Request.parse(rawRequest);
				log.info("Request: " + request.toString());

				Response response;
				WebServer server = servers.get(request.prefix());
				log.fine(request + " being handler by: " + server);

				if (server == null)
					response = Response.error(new FileNotFoundException(request.path()));

				else
					try { response = server.handle(request.shift()); }
					catch(SecurityException e)
					{
						response = Response.error(e);
						log.log(Level.WARNING,
								"Error handling " + request + ":" + response,
								response.errorCause());
					}
					catch(Throwable t)
					{
						response = Response.error(t);
						log.log(Level.SEVERE,
								"Error handling " + request + ":" + response,
								response.errorCause());
					}
				log.fine("Response: " + response);

				try
				{
					response.write(socket.getOutputStream());
					log.finer("Sent response: " + response);
					socket.close();
				}
				catch(SocketException e)
				{
					log.log(Level.SEVERE, "Uncaught SocketException", e);
				}
			}
		}
		catch(Exception e)
		{
			System.err.println("Web UI dying:");
			e.printStackTrace(System.err);
		}
	}


	private static final Logger log = Logger.getLogger(MasterServer.class.getName());

	private final int port;
	private boolean done;

	private final Map<String,WebServer> servers;
}
{'fraction_non_alphanumeric': 0.11978832584990379, 'fraction_numerical': 0.0025657472738935213, 'mean_word_length': 3.4454739843193156, 'pattern_counts': {'":': 3, '<': 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': '3109320', 'n_tokens_mistral': 2190, 'n_tokens_neox': 1780, 'n_words': 512}