<commit_before>import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


class CustomHttpHandler implements HttpHandler {

    private final Logger LOG = LoggerFactory.getLogger(CustomHttpHandler.class);
    private CustomKaaClient kaaClient;

    CustomHttpHandler(CustomKaaClient kaaClient) {
        this.kaaClient = kaaClient;
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        String context = exchange.getRequestURI().toString();

        switch (context) {
            case "/lamp/turn":
                try {
                    exchange.sendResponseHeaders(200, Constants.code200.length());
                    exchange.getResponseBody().write(Constants.code200.getBytes());
                    kaaClient.sendTurnEvent(Parser.getTurnEvent(readFromInputStream(exchange.getRequestBody())));
                } catch (ParseException e) {
                    exchange.sendResponseHeaders(400, Constants.code400.length());
                    exchange.getResponseBody().write(Constants.code400.getBytes());
                    e.printStackTrace();
                }
                break;
            case "/lamp/list":
                exchange.sendResponseHeaders(501, Constants.code501.length());
                exchange.getResponseBody().write(Constants.code501.getBytes());
                break;
            case "/lamp/OnI":
                exchange.sendResponseHeaders(200, Constants.code200.length());
                exchange.getResponseBody().write(Constants.code200.getBytes());
                try {
                    String ans = readFromInputStream(exchange.getRequestBody());
                    kaaClient.sendOnIEvent(Parser.getOnIEvent(ans));
                } catch (ParseException e) {
                    exchange.sendResponseHeaders(400, Constants.code400.length());
                    exchange.getResponseBody().write(Constants.code400.getBytes());
                    e.printStackTrace();
                }
                break;
            default:
                exchange.sendResponseHeaders(404, Constants.code404.length());
                exchange.getResponseBody().write(Constants.code404.getBytes());
        }

        exchange.getResponseBody().close();
    }

    private String readFromInputStream(InputStream inputStream) {
        String ans = "";
        String read;
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        try {
            while ((read = br.readLine()) != null) {<commit_msg>Improves HttpHandler<commit_after>import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class CustomHttpHandler implements HttpHandler {

    private final Logger LOG = LoggerFactory.getLogger(CustomHttpHandler.class);
    private CustomKaaClient kaaClient;
    private Request request;

    public CustomHttpHandler(CustomKaaClient kaaClient, Request request) {
        this.kaaClient = kaaClient;
        this.request = request;
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        String context = exchange.getRequestURI().toString();

        // Interface implementation based:
        // Response response = request.requestOnSuccess(context);
        // exchange.sendResponseHeaders(response.getStatusCode(), response.getResponse().length());
        // exchange.getResponseBody().write(response.getResponse().getBytes());
        switch (context) {
            case "/lamp/turn":
                try {
                    kaaClient.sendTurnEvent(Parser.getTurnEvent(readFromInputStream(exchange.getRequestBody())));
                    exchange.sendResponseHeaders(200, Constants.code200.length());
                    exchange.getResponseBody().write(Constants.code200.getBytes());
                } catch (ParseException e) {
                    exchange.sendResponseHeaders(400, Constants.code400.length());
                    exchange.getResponseBody().write(Constants.code400.getBytes());
                    e.printStackTrace();
                }
                break;
            case "/lamp/list":
                exchange.sendResponseHeaders(501, Constants.code501.length());
                exchange.getResponseBody().write(Constants.code501.getBytes());
                break;
            case "/lamp/OnI":
                try {
                    String ans = readFromInputStream(exchange.getRequestBody());
                    kaaClient.sendOnIEvent(Parser.getOnIEvent(ans));
                    exchange.sendResponseHeaders(200, Constants.code200.length());
                    exchange.getResponseBody().write(Constants.code200.getBytes());
                } catch (ParseException e) {
                    exchange.sendResponseHeaders(400, Constants.code400.length());
                    exchange.getResponseBody().write(Constants.code400.getBytes());
                    e.printStackTrace();
                }
                break;
            default:
                exchange.sendResponseHeaders(404, Constants.code404.length());
                exchange.getResponseBody().write(Constants.code404.getBytes());
        }

        exchange.getResponseBody().close();
    }

    private String readFromInputStream(InputStream inputStream) {
        String ans = "";
        String read;
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        try {
            while ((read = br.readLine()) != null) {
{'fraction_non_alphanumeric': 0.10383064516129033, 'fraction_numerical': 0.01881720430107527, 'mean_word_length': 2.292588495575221, 'pattern_counts': {'":': 6, '<': 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': '3862608', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1407, 'n_words': 235}