<commit_before>package ninja;

import java.io.IOException;

import ninja.lifecycle.LifecycleService;
import ninja.template.TemplateEngine;
import ninja.template.TemplateEngineManager;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;

/**
 * Main implementation of the ninja framework.
 * 
 * Roughly works in the following order:
 * 
 * - Gets a request
 * 
 * - Searches for a matching route
 * 
 * - Applies filters
 * 
 * - Executes matching controller
 * 
 * - Returns result
 * 
 * @author ra
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class NinjaImpl implements Ninja {

	/**
	 * The most important thing: A cool logo.
	 */
	private final String NINJA_LOGO = " _______  .___ _______        ____.  _____   \n"
			+ " \\      \\ |   |\\      \\      |    | /  _  \\  \n"
			+ " /   |   \\|   |/   |   \\     |    |/  /_\\  \\ \n"
			+ "/    |    \\   /    |    \\/\\__|    /    |    \\\n"
			+ "\\____|__  /___\\____|__  /\\________\\____|__  /\n"
			+ "     web\\/framework   \\/                  \\/ \n";

	private final Router router;
	private final Injector injector;
	private final LifecycleService lifecycleService;

	// something like views/notFound404.ftl.html
	// => named so the user can change it to path she likes
	private final String pathToViewNotFound;

	private final TemplateEngineManager templateEngineManager;

	@Inject
	public NinjaImpl(Injector injector, LifecycleService lifecycleService,
			Router router, TemplateEngineManager templateEngineManager,
			@Named("template404") String pathToViewNotFound) {

		this.router = router;
		this.injector = injector;
		this.lifecycleService = lifecycleService;
		this.templateEngineManager = templateEngineManager;
		this.pathToViewNotFound = pathToViewNotFound;

		// This system out println is intended.
		System.out.println(NINJA_LOGO);
	}

	/**
	 * I do all the main work.
	 * 
	 * @param context
	 *            context
	 */
	public void invoke(ContextImpl context) {

		String httpMethod = context.getHttpServletRequest().getMethod();

		Route route = router.getRouteFor(httpMethod, context
				.getHttpServletRequest().getRequestURI());

		context.setRoute(route);

		if (route != null) {

			Result result = route.getFilterChain().next(context);

			invokeResult(result, context);

		} else {
			// throw a 404 "not found" because we did not find the route

			Result result = Results.html(Result.SC_404_NOT_FOUND)
					.template(pathToViewNotFound).html();

			invokeResult(result, context);
		}
	}

	@Override
	public void start() {
		lifecycleService.start();
	}

	@Override
	public void shutdown() {
		lifecycleService.stop();
	}

	private void invokeResult(Result result, Context context) {

		// if the object is a renderable it should do everything itself...:
		// make sure to call context.finalizeHeaders(result) with the results
		// you want to set...
		Object object = result.getRenderable();
		if (object instanceof Renderable) {

			Renderable renderable = (Renderable) object;
			renderable.render(context, result);
			
		} else {
			
			context.finalizeHeaders(result);

			TemplateEngine templateEngine = templateEngineManager
					.getTemplateEngineForContentType(result.getContentType());

			if (templateEngine == null) {
				if (result.getRenderable() instanceof String) {
					// Simply write it out
					try {
						context.getWriter().write(
								(String) result.getRenderable());
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				} else if (result.getRenderable() instanceof byte[]) {
					// Simply write it out
					try {
						context.getOutputStream().write(
								(byte[]) result.getRenderable());
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				}
				throw new IllegalArgumentException(
						"No template engine found for content type "
								+ result.getContentType());
			}

			templateEngine.invoke(context, result.getRenderable());
		}

	}

}<commit_msg>Fixed a couple of bugs<commit_after>package ninja;

import java.io.IOException;

import ninja.lifecycle.LifecycleService;
import ninja.template.TemplateEngine;
import ninja.template.TemplateEngineManager;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;

/**
 * Main implementation of the ninja framework.
 * 
 * Roughly works in the following order:
 * 
 * - Gets a request
 * 
 * - Searches for a matching route
 * 
 * - Applies filters
 * 
 * - Executes matching controller
 * 
 * - Returns result
 * 
 * @author ra
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class NinjaImpl implements Ninja {

	/**
	 * The most important thing: A cool logo.
	 */
	private final String NINJA_LOGO = " _______  .___ _______        ____.  _____   \n"
			+ " \\      \\ |   |\\      \\      |    | /  _  \\  \n"
			+ " /   |   \\|   |/   |   \\     |    |/  /_\\  \\ \n"
			+ "/    |    \\   /    |    \\/\\__|    /    |    \\\n"
			+ "\\____|__  /___\\____|__  /\\________\\____|__  /\n"
			+ "     web\\/framework   \\/                  \\/ \n";

	private final Router router;
	private final Injector injector;
	private final LifecycleService lifecycleService;

	// something like views/notFound404.ftl.html
	// => named so the user can change it to path she likes
	private final String pathToViewNotFound;

	private final TemplateEngineManager templateEngineManager;

	@Inject
	public NinjaImpl(Injector injector, LifecycleService lifecycleService,
			Router router, TemplateEngineManager templateEngineManager,
			@Named("template404") String pathToViewNotFound) {

		this.router = router;
		this.injector = injector;
		this.lifecycleService = lifecycleService;
		this.templateEngineManager = templateEngineManager;
		this.pathToViewNotFound = pathToViewNotFound;

		// This system out println is intended.
		System.out.println(NINJA_LOGO);
	}

	/**
	 * I do all the main work.
	 * 
	 * @param context
	 *            context
	 */
	public void invoke(ContextImpl context) {

		String httpMethod = context.getHttpServletRequest().getMethod();

		Route route = router.getRouteFor(httpMethod, context
				.getHttpServletRequest().getRequestURI());

		context.setRoute(route);

		if (route != null) {

			Result result = route.getFilterChain().next(context);

			invokeResult(result, context);

		} else {
			// throw a 404 "not found" because we did not find the route

			Result result = Results.html(Result.SC_404_NOT_FOUND)
					.template(pathToViewNotFound).html();

			invokeResult(result, context);
		}
	}

	@Override
	public void start() {
		lifecycleService.start();
	}

	@Override
	public void shutdown() {
		lifecycleService.stop();
	}

	private void invokeResult(Result result, Context context) {

		// if the object is a renderable it should do everything itself...:
		// make sure to call context.finalizeHeaders(result) with the results
		// you want to set...
		Object object = result.getRenderable();
		if (object instanceof Renderable) {

			Renderable renderable = (Renderable) object;
			renderable.render(context, result);
			
		} else {
			
			context.finalizeHeaders(result);

            if (result.getContentType() != null) {
                TemplateEngine templateEngine = templateEngineManager
                        .getTemplateEngineForContentType(result.getContentType());

                if (templateEngine == null) {
                    if (result.getRenderable() instanceof String) {
                        // Simply write it out
                        try {
                            context.getWriter().write(
                                    (String) result.getRenderable());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    } else if (result.getRenderable() instanceof byte[]) {
                        // Simply write it out
                        try {
                            context.getOutputStream().write(
                                    (byte[]) result.getRenderable());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    } else if (result.getRenderable() != null) {
                        throw new IllegalArgumentException(
                                "No template engine found for content type "
                                        + result.getContentType());
                    }
                } else {
                    templateEngine.invoke(context, result.getRenderable());
                }
            }
		}

	}

}
{'fraction_non_alphanumeric': 0.1094974642692485, 'fraction_numerical': 0.0027662517289073307, 'mean_word_length': 2.2731044888721237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705001', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2383, 'n_words': 602}