<commit_before>/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.util;

import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.exedio.cope.Cope;
import com.exedio.cope.Item;
import com.exedio.cope.Model;
import com.exedio.cope.NoSuchIDException;
import com.exedio.cope.StringField;
import com.exedio.cope.pattern.MapField;
import com.exedio.cope.util.ConnectToken;
import com.exedio.cope.util.ServletUtil;
import com.exedio.cops.Cop;
import com.exedio.cops.CopsServlet;

public abstract class Editor implements Filter
{
	private final Model model;
	
	/**
	 * Subclasses must define a public no-args constructor
	 * providing the model.
	 */
	protected Editor(final Model model)
	{
		if(model==null)
			throw new NullPointerException("model was null in " + getClass().getName());
		
		this.model = model;
	}
	
	private ConnectToken connectToken = null;
	
	public final void init(final FilterConfig config)
	{
		connectToken = ServletUtil.connect(model, config.getServletContext(), getClass().getName());
	}
	
	public final void destroy()
	{
		connectToken.returnIt();
		connectToken = null;
	}
	
	/**
	 * If you want persistent sessions,
	 * the make implementors of this interface serializable.
	 */
	public interface Login
	{
		String getName();
	}
	
	protected abstract Login login(String user, String password);
	
	public final void doFilter(
			final ServletRequest servletRequest,
			final ServletResponse response,
			final FilterChain chain) throws IOException, ServletException
	{
		if(!(servletRequest instanceof HttpServletRequest))
		{
			chain.doFilter(servletRequest, response);
			return;
		}
		
		final HttpServletRequest request = (HttpServletRequest)servletRequest;
		servletRequest.setCharacterEncoding(CopsServlet.ENCODING);
		
		if(LOGIN_URL_PATH_INFO.equals(request.getPathInfo()))
		{
			handleLogin(request, (HttpServletResponse)response);
			return;
		}

		final HttpSession httpSession = request.getSession(false);
		if(httpSession!=null)
		{
			final Object session = httpSession.getAttribute(SESSION);
			if(session!=null)
				handleBar(request, (HttpServletResponse)response, chain, (Session)session);
			else
				chain.doFilter(request, response);
		}
		else
		{
			chain.doFilter(request, response);
		}
	}
	
	static final String AVOID_COLLISION = "contentEditorBar823658617";
	static final String TOGGLE_BORDERS = "borders";
	static final String SAVE_FEATURE = "feature";
	static final String SAVE_ITEM    = "item";
	static final String SAVE_KIND    = "kind";
	static final String SAVE_LINE    = "line";
	static final String SAVE_AREA    = "area";
	static final String SAVE_KIND_LINE = "kindLine";
	static final String SAVE_KIND_AREA = "kindArea";
	
	private final void handleBar(
			final HttpServletRequest request,
			final HttpServletResponse response,
			final FilterChain chain,
			final Session session) throws IOException, ServletException
	{
		if(Cop.isPost(request) && request.getParameter(AVOID_COLLISION)!=null)
		{
			if(request.getParameter(TOGGLE_BORDERS)!=null)
			{
				session.borders = !session.borders;
			}
			
			final String featureID = request.getParameter(SAVE_FEATURE);
			if(featureID!=null)
			{
				final String itemID    = request.getParameter(SAVE_ITEM);
				final String kind      = request.getParameter(SAVE_KIND);
				final String value;
				if(SAVE_KIND_LINE.equals(kind))
					value = request.getParameter(SAVE_LINE);
				else if(SAVE_KIND_AREA.equals(kind))
					value = request.getParameter(SAVE_AREA);
				else
					throw new RuntimeException(kind);
			
				try
				{
					model.startTransaction(getClass().getName() + "#save");
					
					final StringField feature = (StringField)model.findFeatureByID(featureID);
					assert feature!=null : featureID;
					final Item item = model.findByID(itemID);
					String v = value;
					if("".equals(v))
						v = null;
					feature.set(item, v);
					
					model.commit();
				}
				catch(NoSuchIDException e)
				{
					throw new RuntimeException(e);
				}
				finally
				{
					model.rollbackIfNotCommitted();
				}
			}
		}
		try
		{
			tls.set(new TL(response, session));
			chain.doFilter(request, response);
		}
		finally
		{
			tls.remove();
		}
	}
	
	static final String LOGIN_URL = "contentEditorLogin.html";
	private static final String LOGIN_URL_PATH_INFO = '/' + LOGIN_URL;
	static final String LOGIN = "login";
	static final String LOGIN_USER = "user";
	static final String LOGIN_PASSWORD = "password";
	
	private final void handleLogin(
			final HttpServletRequest request,
			final HttpServletResponse response)
	throws IOException
	{
		response.setContentType("text/html; charset="+CopsServlet.ENCODING);<commit_msg>local rename<commit_after>/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.util;

import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.exedio.cope.Cope;
import com.exedio.cope.Item;
import com.exedio.cope.Model;
import com.exedio.cope.NoSuchIDException;
import com.exedio.cope.StringField;
import com.exedio.cope.pattern.MapField;
import com.exedio.cope.util.ConnectToken;
import com.exedio.cope.util.ServletUtil;
import com.exedio.cops.Cop;
import com.exedio.cops.CopsServlet;

public abstract class Editor implements Filter
{
	private final Model model;
	
	/**
	 * Subclasses must define a public no-args constructor
	 * providing the model.
	 */
	protected Editor(final Model model)
	{
		if(model==null)
			throw new NullPointerException("model was null in " + getClass().getName());
		
		this.model = model;
	}
	
	private ConnectToken connectToken = null;
	
	public final void init(final FilterConfig config)
	{
		connectToken = ServletUtil.connect(model, config.getServletContext(), getClass().getName());
	}
	
	public final void destroy()
	{
		connectToken.returnIt();
		connectToken = null;
	}
	
	/**
	 * If you want persistent sessions,
	 * the make implementors of this interface serializable.
	 */
	public interface Login
	{
		String getName();
	}
	
	protected abstract Login login(String user, String password);
	
	public final void doFilter(
			final ServletRequest servletRequest,
			final ServletResponse response,
			final FilterChain chain) throws IOException, ServletException
	{
		if(!(servletRequest instanceof HttpServletRequest))
		{
			chain.doFilter(servletRequest, response);
			return;
		}
		
		final HttpServletRequest request = (HttpServletRequest)servletRequest;
		servletRequest.setCharacterEncoding(CopsServlet.ENCODING);
		
		if(LOGIN_URL_PATH_INFO.equals(request.getPathInfo()))
		{
			doLogin(request, (HttpServletResponse)response);
			return;
		}

		final HttpSession httpSession = request.getSession(false);
		if(httpSession!=null)
		{
			final Object session = httpSession.getAttribute(SESSION);
			if(session!=null)
				doBar(request, (HttpServletResponse)response, chain, (Session)session);
			else
				chain.doFilter(request, response);
		}
		else
		{
			chain.doFilter(request, response);
		}
	}
	
	static final String AVOID_COLLISION = "contentEditorBar823658617";
	static final String TOGGLE_BORDERS = "borders";
	static final String SAVE_FEATURE = "feature";
	static final String SAVE_ITEM    = "item";
	static final String SAVE_KIND    = "kind";
	static final String SAVE_LINE    = "line";
	static final String SAVE_AREA    = "area";
	static final String SAVE_KIND_LINE = "kindLine";
	static final String SAVE_KIND_AREA = "kindArea";
	
	private final void doBar(
			final HttpServletRequest request,
			final HttpServletResponse response,
			final FilterChain chain,
			final Session session) throws IOException, ServletException
	{
		if(Cop.isPost(request) && request.getParameter(AVOID_COLLISION)!=null)
		{
			if(request.getParameter(TOGGLE_BORDERS)!=null)
			{
				session.borders = !session.borders;
			}
			
			final String featureID = request.getParameter(SAVE_FEATURE);
			if(featureID!=null)
			{
				final String itemID    = request.getParameter(SAVE_ITEM);
				final String kind      = request.getParameter(SAVE_KIND);
				final String value;
				if(SAVE_KIND_LINE.equals(kind))
					value = request.getParameter(SAVE_LINE);
				else if(SAVE_KIND_AREA.equals(kind))
					value = request.getParameter(SAVE_AREA);
				else
					throw new RuntimeException(kind);
			
				try
				{
					model.startTransaction(getClass().getName() + "#save");
					
					final StringField feature = (StringField)model.findFeatureByID(featureID);
					assert feature!=null : featureID;
					final Item item = model.findByID(itemID);
					String v = value;
					if("".equals(v))
						v = null;
					feature.set(item, v);
					
					model.commit();
				}
				catch(NoSuchIDException e)
				{
					throw new RuntimeException(e);
				}
				finally
				{
					model.rollbackIfNotCommitted();
				}
			}
		}
		try
		{
			tls.set(new TL(response, session));
			chain.doFilter(request, response);
		}
		finally
		{
			tls.remove();
		}
	}
	
	static final String LOGIN_URL = "contentEditorLogin.html";
	private static final String LOGIN_URL_PATH_INFO = '/' + LOGIN_URL;
	static final String LOGIN = "login";
	static final String LOGIN_USER = "user";
	static final String LOGIN_PASSWORD = "password";
	
	private final void doLogin(
			final HttpServletRequest request,
			final HttpServletResponse response)
	throws IOException
	{
		response.setContentType("text/html; charset="+CopsServlet.ENCODING);
{'fraction_non_alphanumeric': 0.09392503231365791, 'fraction_numerical': 0.005687203791469194, 'mean_word_length': 4.108274647887324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3013110', 'n_tokens_mistral': 3989, 'n_tokens_neox': 3455, 'n_words': 946}