<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.markup.html.form.persistence;

import javax.servlet.http.Cookie;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.time.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * This class implements IValuePersister by means of HTTP cookies.
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class CookieValuePersister implements IValuePersister
{
	private static final long serialVersionUID = 1L;

	/** Logging */
	private final static Logger log = LoggerFactory.getLogger(CookieValuePersister.class);

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#clear(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void clear(final FormComponent<?> component)
	{
		final Cookie cookie = getCookie(component);
		if (cookie != null)
		{
			clear(cookie);
			if (log.isDebugEnabled())
			{
				log.debug("Cookie for " + component + " removed");
			}
		}
	}

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#load(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void load(final FormComponent<?> component)
	{
		final Cookie cookie = getCookie(component);
		if (cookie != null)
		{
			final String value = cookie.getValue();
			if (value != null)
			{
				// Assign the retrieved/persisted value to the component
				component.setModelValue(value.split(FormComponent.VALUE_SEPARATOR));
			}
		}
	}

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#save(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void save(final FormComponent<?> component)
	{
		final String name = getName(component);
		final String value = component.getValue();

		Cookie cookie = getCookie(component);
		if (cookie == null)
		{
			cookie = new Cookie(name, value == null ? "" : value);
		}
		else
		{
			cookie.setValue(value == null ? "" : value);
		}
		cookie.setSecure(false);
		cookie.setMaxAge(getSettings().getMaxAge());

		save(cookie);
	}

	/**
	 * @param component
	 *            Component to get name for
	 * @return The name of the component.
	 */
	protected String getName(final FormComponent<?> component)
	{
		return component.getPageRelativePath();
	}

	/**
	 * Convenience method for deleting a cookie by name. Delete the cookie by setting its maximum
	 * age to zero.
	 * 
	 * @param cookie
	 *            The cookie to delete
	 */
	private void clear(final Cookie cookie)
	{
		if (cookie != null)
		{
			// Delete the cookie by setting its maximum age to zero
			cookie.setMaxAge(0);
			cookie.setValue(null);

			save(cookie);
		}
	}

	/**
	 * Gets debug info as a string for the given cookie.
	 * 
	 * @param cookie
	 *            the cookie to debug.
	 * @return a string that represents the internals of the cookie.
	 */
	private String cookieToDebugString(final Cookie cookie)
	{
		return "[Cookie " + " name = " + cookie.getName() + ", value = " + cookie.getValue() +<commit_msg>WICKET-1834<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.markup.html.form.persistence;

import javax.servlet.http.Cookie;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.time.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * This class implements IValuePersister by means of HTTP cookies.
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class CookieValuePersister implements IValuePersister
{
	private static final long serialVersionUID = 1L;

	/** Logging */
	private final static Logger log = LoggerFactory.getLogger(CookieValuePersister.class);

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#clear(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void clear(final FormComponent<?> component)
	{
		final Cookie cookie = getCookie(component);
		if (cookie != null)
		{
			clear(cookie);
			if (log.isDebugEnabled())
			{
				log.debug("Cookie for " + component + " removed");
			}
		}
	}

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#load(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void load(final FormComponent<?> component)
	{
		final Cookie cookie = getCookie(component);
		if (cookie != null)
		{
			final String value = cookie.getValue();
			if (value != null)
			{
				// Assign the retrieved/persisted value to the component
				component.setModelValue(value.split(FormComponent.VALUE_SEPARATOR));
			}
		}
	}

	/**
	 * @see org.apache.wicket.markup.html.form.persistence.IValuePersister#save(org.apache.wicket.markup.html.form.FormComponent)
	 */
	public void save(final FormComponent<?> component)
	{
		final String name = getName(component);
		final String value = component.getValue();

		Cookie cookie = getCookie(component);
		if (cookie == null)
		{
			cookie = new Cookie(name, value == null ? "" : value);
		}
		else
		{
			cookie.setValue(value == null ? "" : value);
		}
		cookie.setSecure(false);
		cookie.setMaxAge(getSettings().getMaxAge());

		save(cookie);
	}

	/**
	 * @param component
	 *            Component to get name for
	 * @return The name of the component.
	 */
	protected String getName(final FormComponent<?> component)
	{
		String name = component.getPageRelativePath();
		// cookie names cannot contian ':',
		// we replace ':' with '.' but first we have to encode '.' as '..'
		name = name.replace(".", "..");
		name = name.replace(":", ".");
		return name;
	}

	/**
	 * Convenience method for deleting a cookie by name. Delete the cookie by setting its maximum
	 * age to zero.
	 * 
	 * @param cookie
	 *            The cookie to delete
	 */
	private void clear(final Cookie cookie)
	{
		if (cookie != null)
		{
			// Delete the cookie by setting its maximum age to zero
			cookie.setMaxAge(0);
			cookie.setValue(null);

			save(cookie);
		}
	}

	/**
	 * Gets debug info as a string for the given cookie.
	 * 
	 * @param cookie
	 *            the cookie to debug.
	 * @return a string that represents the internals of the cookie.
	 */
	private String cookieToDebugString(final Cookie cookie)
	{
		return "[Cookie " + " name = " + cookie.getName() + ", value = " + cookie.getValue() +
{'fraction_non_alphanumeric': 0.11333167825223436, 'fraction_numerical': 0.0024826216484607746, 'mean_word_length': 3.5648725212464587, 'pattern_counts': {'":': 1, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4269052', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2353, 'n_words': 718}