<commit_before>package com.elibom.jogger.http.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.elibom.jogger.asset.Asset;
import com.elibom.jogger.http.Cookie;
import com.elibom.jogger.http.HttpException;
import com.elibom.jogger.http.Response;
import com.elibom.jogger.template.TemplateEngine;
import com.elibom.jogger.template.TemplateException;
import com.elibom.jogger.util.Preconditions;

/**
 * A {@link Response} implementation based on the Servlet API.
 *
 * @author <NAME>
 */
public class ServletResponse implements Response {

	/**
	 * The underlying Servlet Response.
	 */
	private HttpServletResponse response;

	private TemplateEngine templateEngine;

	/**
	 * The attributes that we are passing to the view.
	 */
	private Map<String,Object> attributes = new HashMap<String,Object>();
	
	private boolean written = false;

	/**
	 * Constructor. Initializes the object with the underlying Servlet Response and the FreeMarker configuration.
	 *
	 * @param response the Servlet response object.
	 * @param templateEngine the {@link TemplateEngine} implementation to use.
	 */
	public ServletResponse(HttpServletResponse response, TemplateEngine templateEngine) {
		Preconditions.notNull(response, "no response provided.");
		Preconditions.notNull(templateEngine, "no templateEngine provided.");

		this.response = response;
		response.setStatus(Response.NOT_FOUND);
		
		this.templateEngine = templateEngine;
	}

	@Override
	public int getStatus() {
		return response.getStatus();
	}

	@Override
	public Response status(int status) {
		response.setStatus(status);
		return this;
	}

	@Override
	public Response badRequest() {
		response.setStatus(Response.BAD_REQUEST);
		return this;
	}

	@Override
	public Response unauthorized() {
		response.setStatus(Response.UNAUTHORIZED);
		return this;
	}

	@Override
	public Response notFound() {
		response.setStatus(Response.NOT_FOUND);
		return this;
	}



	@Override
	public Response conflict() {
		response.setStatus(Response.CONFLICT);
		return this;
	}

	@Override
	public String getContentType() {
		return response.getContentType();
	}

	@Override
	public Response contentType(String contentType) {
		response.setContentType(contentType);
		return this;
	}

	@Override
	public String getHeader(String name) {
		return response.getHeader(name);
	}

	@Override
	public Response setHeader(String name, String value) {
		response.setHeader(name, value);
		return this;
	}

	@Override
	public Response setCookie(Cookie cookie) {
		response.addCookie(map(cookie));
		return this;
	}

	@Override
	public Response removeCookie(Cookie cookie) {
		cookie.setMaxAge(0);

		setCookie(cookie);

		return this;
	}

	javax.servlet.http.Cookie map(Cookie cookie) {
		javax.servlet.http.Cookie servletCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());
		servletCookie.setMaxAge(cookie.getMaxAge());

		if (cookie.getPath() != null) {
			servletCookie.setPath(cookie.getPath());
		}

		if (cookie.getDomain() != null) {
			servletCookie.setDomain(cookie.getDomain());
		}

		servletCookie.setHttpOnly(cookie.isHttpOnly());

		return servletCookie;
	}

	@Override
	public Map<String, Object> getAttributes() {
		return attributes;
	}

	@Override
	public Response setAttribute(String name, Object object) {
		Preconditions.notEmpty(name, "no name provided.");
		Preconditions.notNull(object, "no object specified");

		attributes.put(name, object);

		return this;<commit_msg>Add secure flag to servlet cookie<commit_after>package com.elibom.jogger.http.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.elibom.jogger.asset.Asset;
import com.elibom.jogger.http.Cookie;
import com.elibom.jogger.http.HttpException;
import com.elibom.jogger.http.Response;
import com.elibom.jogger.template.TemplateEngine;
import com.elibom.jogger.template.TemplateException;
import com.elibom.jogger.util.Preconditions;

/**
 * A {@link Response} implementation based on the Servlet API.
 *
 * @author <NAME>
 */
public class ServletResponse implements Response {

	/**
	 * The underlying Servlet Response.
	 */
	private HttpServletResponse response;

	private TemplateEngine templateEngine;

	/**
	 * The attributes that we are passing to the view.
	 */
	private Map<String,Object> attributes = new HashMap<String,Object>();
	
	private boolean written = false;

	/**
	 * Constructor. Initializes the object with the underlying Servlet Response and the FreeMarker configuration.
	 *
	 * @param response the Servlet response object.
	 * @param templateEngine the {@link TemplateEngine} implementation to use.
	 */
	public ServletResponse(HttpServletResponse response, TemplateEngine templateEngine) {
		Preconditions.notNull(response, "no response provided.");
		Preconditions.notNull(templateEngine, "no templateEngine provided.");

		this.response = response;
		response.setStatus(Response.NOT_FOUND);
		
		this.templateEngine = templateEngine;
	}

	@Override
	public int getStatus() {
		return response.getStatus();
	}

	@Override
	public Response status(int status) {
		response.setStatus(status);
		return this;
	}

	@Override
	public Response badRequest() {
		response.setStatus(Response.BAD_REQUEST);
		return this;
	}

	@Override
	public Response unauthorized() {
		response.setStatus(Response.UNAUTHORIZED);
		return this;
	}

	@Override
	public Response notFound() {
		response.setStatus(Response.NOT_FOUND);
		return this;
	}



	@Override
	public Response conflict() {
		response.setStatus(Response.CONFLICT);
		return this;
	}

	@Override
	public String getContentType() {
		return response.getContentType();
	}

	@Override
	public Response contentType(String contentType) {
		response.setContentType(contentType);
		return this;
	}

	@Override
	public String getHeader(String name) {
		return response.getHeader(name);
	}

	@Override
	public Response setHeader(String name, String value) {
		response.setHeader(name, value);
		return this;
	}

	@Override
	public Response setCookie(Cookie cookie) {
		response.addCookie(map(cookie));
		return this;
	}

	@Override
	public Response removeCookie(Cookie cookie) {
		cookie.setMaxAge(0);

		setCookie(cookie);

		return this;
	}

	javax.servlet.http.Cookie map(Cookie cookie) {
		javax.servlet.http.Cookie servletCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());
		servletCookie.setMaxAge(cookie.getMaxAge());

		if (cookie.getPath() != null) {
			servletCookie.setPath(cookie.getPath());
		}

		if (cookie.getDomain() != null) {
			servletCookie.setDomain(cookie.getDomain());
		}

		servletCookie.setHttpOnly(cookie.isHttpOnly());
                servletCookie.setSecure(cookie.isSecure());

		return servletCookie;
	}

	@Override
	public Map<String, Object> getAttributes() {
		return attributes;
	}

	@Override
	public Response setAttribute(String name, Object object) {
		Preconditions.notEmpty(name, "no name provided.");
		Preconditions.notNull(object, "no object specified");

		attributes.put(name, object);

		return this;
{'fraction_non_alphanumeric': 0.10777385159010601, 'fraction_numerical': 0.0002718129926610492, 'mean_word_length': 4.771764705882353, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4740315', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2176, 'n_words': 517}