<commit_before>package com.codeandstrings.niohttp.response;

import java.nio.ByteBuffer;
import java.util.Date;

import com.codeandstrings.niohttp.data.DateUtils;
import com.codeandstrings.niohttp.data.Parameters;
import com.codeandstrings.niohttp.enums.HttpProtocol;
import com.codeandstrings.niohttp.enums.RequestMethod;
import com.codeandstrings.niohttp.exceptions.http.HttpException;
import com.codeandstrings.niohttp.request.Request;

public class ResponseFactory {

    private static void addVaryTransferEncoding(Response r) {

        String vary = r.getHeaderCaseInsensitive("vary");

        // no vary
        if (vary == null) {
            r.addHeader("Vary", "Accept-Encoding");
            return;
        }

        vary = vary.trim();

        // already exists
        if (vary.toLowerCase().indexOf("Accept-Encoding") != -1) {
            return;
        }

        r.removeHeader("vary");

        StringBuilder replacementVary = new StringBuilder();

        if (vary.length() > 0) {
            replacementVary.append(vary);
            replacementVary.append(", ");
        }

        replacementVary.append("Accept-Encoding");

        r.addHeader("Vary", replacementVary.toString());

    }

    public static Response createResponseNotModified(Request request, Date lastModified, String etag) {

        HttpProtocol protocol = request.getRequestProtocol();
        RequestMethod method = request.getRequestMethod();

        if (protocol == HttpProtocol.HTTP0_9) {
            return null;
        }

        Response r = new Response(protocol, method);

        r.setCode(304);
        r.setDescription("Not Modified");
        r.addHeader("Date", DateUtils.getRfc822DateStringGMT(new Date()));
        r.addHeader("Last-Modified", DateUtils.getRfc822DateStringGMT(lastModified));
        r.addHeader("ETag", etag);
        r.addHeader("Server", request.getServerParameters().getServerString());

        ResponseFactory.addVaryTransferEncoding(r);

        if (request.isKeepAlive()) {
            r.addHeader("Connection", "Keep-Alive");
        } else {
            r.addHeader("Connection", "close");
        }

        return r;
    }

    private static Response createBasicResponse(Request request) {

        HttpProtocol protocol = request.getRequestProtocol();
        RequestMethod requestMethod = request.getRequestMethod();

        Response r = new Response(protocol, requestMethod);

        if (protocol != HttpProtocol.HTTP0_9) {
            r.setCode(200);
            r.setDescription("OK");
            r.addHeader("Date", DateUtils.getRfc822DateStringGMT(new Date()));
            r.addHeader("Server", request.getServerParameters().getServerString());

            ResponseFactory.addVaryTransferEncoding(r);

            if (request.isKeepAlive()) {
                r.addHeader("Connection", "keep-alive");
            } else {
                r.addHeader("Connection", "close");
            }
        }

        return r;

    }

    public static Response createStreamingResponse(String contentType, Request request) {

        Response r = createBasicResponse(request);

        if (request.getRequestProtocol() != HttpProtocol.HTTP1_1) {
            // for non 1.1 requests we must close after the connection as
            // we can't rely on chunked to mark the end of the request
            r.removeHeader("Connection");
            r.addHeader("Connection", "close");
            r.addHeader("Content-Type", contentType);
        }
        else {
            r.addHeader("Transfer-Encoding", "chunked");
        }

        return r;

    }

    public static Response createResponse(String contentType, long contentSize, Request request) {

        HttpProtocol protocol = request.getRequestProtocol();
        Response r = createBasicResponse(request);

        if (protocol != HttpProtocol.HTTP0_9) {
            r.setCode(200);
            r.setDescription("OK");
            r.addHeader("Content-Type", contentType);
            r.addHeader("Content-Length", String.valueOf(contentSize));
        }

        return r;

    }

//	public static Response createResponse(String content, String contentType,
//			Request request) {
//
//        byte bytes[] = content.getBytes();
//		ByteBuffer contentBuffer = ByteBuffer.allocate(bytes.length);
//		contentBuffer.put(bytes);
//
//		contentBuffer.flip();
//
//        Response r = createResponse(contentType, bytes.length, request);
//		r.setBody(contentBuffer);
//
//		return r;
//
//	}

	public static Response createResponse(HttpException e, Parameters parameters) {
		return (new ExceptionResponseFactory(e)).create(parameters);
	}

}<commit_msg>Removed commented out method.<commit_after>package com.codeandstrings.niohttp.response;

import java.nio.ByteBuffer;
import java.util.Date;

import com.codeandstrings.niohttp.data.DateUtils;
import com.codeandstrings.niohttp.data.Parameters;
import com.codeandstrings.niohttp.enums.HttpProtocol;
import com.codeandstrings.niohttp.enums.RequestMethod;
import com.codeandstrings.niohttp.exceptions.http.HttpException;
import com.codeandstrings.niohttp.request.Request;

public class ResponseFactory {

    private static void addVaryTransferEncoding(Response r) {

        String vary = r.getHeaderCaseInsensitive("vary");

        // no vary
        if (vary == null) {
            r.addHeader("Vary", "Accept-Encoding");
            return;
        }

        vary = vary.trim();

        // already exists
        if (vary.toLowerCase().indexOf("Accept-Encoding") != -1) {
            return;
        }

        r.removeHeader("vary");

        StringBuilder replacementVary = new StringBuilder();

        if (vary.length() > 0) {
            replacementVary.append(vary);
            replacementVary.append(", ");
        }

        replacementVary.append("Accept-Encoding");

        r.addHeader("Vary", replacementVary.toString());

    }

    public static Response createResponseNotModified(Request request, Date lastModified, String etag) {

        HttpProtocol protocol = request.getRequestProtocol();
        RequestMethod method = request.getRequestMethod();

        if (protocol == HttpProtocol.HTTP0_9) {
            return null;
        }

        Response r = new Response(protocol, method);

        r.setCode(304);
        r.setDescription("Not Modified");
        r.addHeader("Date", DateUtils.getRfc822DateStringGMT(new Date()));
        r.addHeader("Last-Modified", DateUtils.getRfc822DateStringGMT(lastModified));
        r.addHeader("ETag", etag);
        r.addHeader("Server", request.getServerParameters().getServerString());

        ResponseFactory.addVaryTransferEncoding(r);

        if (request.isKeepAlive()) {
            r.addHeader("Connection", "Keep-Alive");
        } else {
            r.addHeader("Connection", "close");
        }

        return r;
    }

    private static Response createBasicResponse(Request request) {

        HttpProtocol protocol = request.getRequestProtocol();
        RequestMethod requestMethod = request.getRequestMethod();

        Response r = new Response(protocol, requestMethod);

        if (protocol != HttpProtocol.HTTP0_9) {
            r.setCode(200);
            r.setDescription("OK");
            r.addHeader("Date", DateUtils.getRfc822DateStringGMT(new Date()));
            r.addHeader("Server", request.getServerParameters().getServerString());

            ResponseFactory.addVaryTransferEncoding(r);

            if (request.isKeepAlive()) {
                r.addHeader("Connection", "keep-alive");
            } else {
                r.addHeader("Connection", "close");
            }
        }

        return r;

    }

    public static Response createStreamingResponse(String contentType, Request request) {

        Response r = createBasicResponse(request);

        if (request.getRequestProtocol() != HttpProtocol.HTTP1_1) {
            // for non 1.1 requests we must close after the connection as
            // we can't rely on chunked to mark the end of the request
            r.removeHeader("Connection");
            r.addHeader("Connection", "close");
            r.addHeader("Content-Type", contentType);
        }
        else {
            r.addHeader("Transfer-Encoding", "chunked");
        }

        return r;

    }

    public static Response createResponse(String contentType, long contentSize, Request request) {

        HttpProtocol protocol = request.getRequestProtocol();
        Response r = createBasicResponse(request);

        if (protocol != HttpProtocol.HTTP0_9) {
            r.setCode(200);
            r.setDescription("OK");
            r.addHeader("Content-Type", contentType);
            r.addHeader("Content-Length", String.valueOf(contentSize));
        }

        return r;

    }

	public static Response createResponse(HttpException e, Parameters parameters) {
		return (new ExceptionResponseFactory(e)).create(parameters);
	}

}
{'fraction_non_alphanumeric': 0.11561729778176115, 'fraction_numerical': 0.0067219359175442524, 'mean_word_length': 2.560829676904667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1218379', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2325, 'n_words': 474}