<commit_before>package view;

import java.io.IOException;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.BasicAuthenticator;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;

import service.SessionService;
import service.UserService;
import view.filter.ParameterFilter;
import view.filter.SessionFilter;
import view.handler.AuthenticationHandler;
import view.handler.HomeHandler;
import view.handler.LoginHandler;
import view.handler.LogoutHandler;
import view.handler.Page1Handler;
import view.handler.Page2Handler;
import view.handler.Page3Handler;
import view.handler.UserResourceHandler;

public class SimpleHttpServer {

    private HttpServer server;

    private UserService userService;

    public SimpleHttpServer(int port, SessionService sessionService, UserService userService) {
        this.userService = userService;

        try {
            server = HttpServer.create(new InetSocketAddress(port), 0);
        } catch (IOException e) {
            e.printStackTrace();
        }

        createLoginContext();

        createHomeContext(sessionService);

        createAuthenticateContext(sessionService);

        creatLogoutContext(sessionService);

        createPrivatePagesContext(sessionService);

        createUserResourceContext();

        server.setExecutor(null);
    }

    private void createLoginContext() {
        server.createContext("/login", new LoginHandler());
    }

    private void createHomeContext(SessionService sessionService) {
        HttpContext home = server.createContext("/home", new HomeHandler());
        home.getFilters().add(new SessionFilter(sessionService));
    }

    private void creatLogoutContext(SessionService sessionService) {
        HttpContext logout = server.createContext("/logout", new LogoutHandler(sessionService));
        logout.getFilters().add(new SessionFilter(sessionService));
    }

    private void createAuthenticateContext(SessionService sessionService) {
        HttpContext auth = server.createContext("/authenticate", new AuthenticationHandler(sessionService));
        auth.setAuthenticator(new BasicAuthenticator("") {
            @Override
            public boolean checkCredentials(String user, String pwd) {
                return userService.authenticate(user, pwd);
            }

            @Override
            public Result authenticate(HttpExchange var1) {
                Result result = super.authenticate(var1);
                var1.getResponseHeaders().remove("WWW-Authenticate");
                return result;
            }
        });
    }

    private void createPrivatePagesContext(SessionService sessionService) {
        HttpContext page1 = server.createContext("/page_1", new Page1Handler(userService));
        page1.getFilters().add(new SessionFilter(sessionService));
        HttpContext page2 = server.createContext("/page_2", new Page2Handler(userService));
        page2.getFilters().add(new SessionFilter(sessionService));
        HttpContext page3 = server.createContext("/page_3", new Page3Handler(userService));
        page3.getFilters().add(new SessionFilter(sessionService));
    }

    private void createUserResourceContext() {<commit_msg>simple http server bit refactor<commit_after>package view;

import java.io.IOException;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.BasicAuthenticator;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;

import service.SessionService;
import service.UserService;
import view.filter.ParameterFilter;
import view.filter.SessionFilter;
import view.handler.AuthenticationHandler;
import view.handler.HomeHandler;
import view.handler.LoginHandler;
import view.handler.LogoutHandler;
import view.handler.Page1Handler;
import view.handler.Page2Handler;
import view.handler.Page3Handler;
import view.handler.UserResourceHandler;

public class SimpleHttpServer {

    private HttpServer server;

    private UserService userService;

    public SimpleHttpServer(int port, SessionService sessionService, UserService userService) throws IOException {
        this.userService = userService;

        server = HttpServer.create(new InetSocketAddress(port), 0);

        createLoginContext();

        createHomeContext(sessionService);

        createAuthenticateContext(sessionService);

        createLogoutContext(sessionService);

        createPrivatePagesContext(sessionService);

        createUserResourceContext();

        server.setExecutor(null);
    }

    private void createLoginContext() {
        server.createContext("/login", new LoginHandler());
    }

    private void createHomeContext(SessionService sessionService) {
        HttpContext home = server.createContext("/home", new HomeHandler());
        home.getFilters().add(new SessionFilter(sessionService));
    }

    private void createLogoutContext(SessionService sessionService) {
        HttpContext logout = server.createContext("/logout", new LogoutHandler(sessionService));
        logout.getFilters().add(new SessionFilter(sessionService));
    }

    private void createAuthenticateContext(SessionService sessionService) {
        HttpContext auth = server.createContext("/authenticate", new AuthenticationHandler(sessionService));
        auth.setAuthenticator(new BasicAuthenticator("") {
            @Override
            public boolean checkCredentials(String user, String pwd) {
                return userService.authenticate(user, pwd);
            }

            @Override
            public Result authenticate(HttpExchange var1) {
                Result result = super.authenticate(var1);
                var1.getResponseHeaders().remove("WWW-Authenticate");
                return result;
            }
        });
    }

    private void createPrivatePagesContext(SessionService sessionService) {
        HttpContext page1 = server.createContext("/page_1", new Page1Handler(userService));
        page1.getFilters().add(new SessionFilter(sessionService));
        HttpContext page2 = server.createContext("/page_2", new Page2Handler(userService));
        page2.getFilters().add(new SessionFilter(sessionService));
        HttpContext page3 = server.createContext("/page_3", new Page3Handler(userService));
        page3.getFilters().add(new SessionFilter(sessionService));
    }

    private void createUserResourceContext() {
{'fraction_non_alphanumeric': 0.09163838321505709, 'fraction_numerical': 0.005862388151804999, 'mean_word_length': 3.708061002178649, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '119080', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1591, 'n_words': 321}