<commit_before>package com.aggrepoint.winlet;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.owasp.esapi.errors.ValidationException;

import com.aggrepoint.winlet.form.Form;
import com.aggrepoint.winlet.form.FormImpl;
import com.aggrepoint.winlet.spring.RequestAttributeRecorder;
import com.aggrepoint.winlet.spring.WinletRequestWrapper;
import com.aggrepoint.winlet.spring.def.ReturnDef;
import com.aggrepoint.winlet.spring.def.WinletDef;
import com.aggrepoint.winlet.utils.BufferedResponse;

/**
 * 
 * @author <NAME> (<EMAIL>)
 */
public class ReqInfoImpl implements ReqConst, ReqInfo {
	private static final String REQ_PARAMETERS_FROM_ACTION_KEY = ".req.param.from.action";

	private static long REQUEST_ID = 0;

	private long requestId;

	private HttpServletRequest request;
	// 遇到当执行了一定逻辑后，request.getSession(true)会不定期返回null
	private HttpSession session;
	String requestPath;
	private String path;
	private String rootWindowId;
	private String windowId;
	private String pageId;
	private String pageUrl;
	private String actionId;
	private String validateFieldName;
	private String validateFieldValue;
	private String validateFieldId;
	private boolean pageRefresh;
	private String translateUpdate;
	private WindowInstance wi;
	private Form form;
	private PageStorage ps;
	private SharedPageStorage sps;
	private ReturnDef rd;

	// 待移植
	public boolean m_bUseAjax = true;

	/**
	 * 用于分解Action或Resource
	 */
	Pattern P_DECODE = Pattern.compile("([^!]*)!([^!]+)");

	public ReqInfoImpl(HttpServletRequest request, String path) {
		this.request = request;
		this.session = request.getSession(true);
		this.path = request.getContextPath() + path;

		// 将IncludeTag需要获得的当前Winlet的RequestPath取出保存
		// 发现如果在WinletDispatcherServlet中用HttpServletRequestWrapper的派生类将request封装，请求forward到JSP页面后
		// ，用ReqInfoImpl中保存的request对象的getRequestURI()方法只能获得当前JSP的URI而不是期望的Winlet
		// URI。如果不封装，或者封装类不从HttpServletRequestWrapper派生，则ReqInfoImpl中request.getRequestURI()可以正常工作。为了避免这个问题，这里直接将requestURI取出保存。
		requestPath = request.getRequestURI();
		int idx = requestPath.indexOf("/", 1);
		requestPath = requestPath.substring(idx);

		requestId = REQUEST_ID++;

		pageId = getParameter(PARAM_PAGE_PATH, null);
		if (pageId == null)
			pageId = request.getRequestURI();

		pageUrl = getParameter(PARAM_PAGE_URL, null);
		if (pageUrl == null)
			pageUrl = request.getRequestURL().toString();

		windowId = request.getHeader(WinletConst.REQUEST_HEADER_WINDOW_ID);
		if (windowId == null || windowId.equals(""))
			windowId = getParameter(PARAM_WIN_ID, null);

		actionId = getParameter(PARAM_WIN_ACTION, null);
		if (actionId != null) {
			Matcher m;
			synchronized (P_DECODE) {
				m = P_DECODE.matcher(actionId);
			}

			if (m.find()) {
				try {
					windowId = m.group(1);
				} catch (Exception e) {
				}
				actionId = m.group(2);
			}
		} else {
			pageRefresh = "yes".equalsIgnoreCase(getParameter(
					PARAM_PAGE_REFRESH, ""));
		}

		validateFieldName = getParameter(PARAM_WIN_VALIDATE_FIELD, null);
		if (validateFieldName != null) {
			validateFieldValue = getParameter(PARAM_WIN_VALIDATE_FIELD_VALUE,
					"");
			validateFieldId = getParameter(PARAM_WIN_VALIDATE_FIELD_ID, "");
		}

		translateUpdate = getParameter(PARAM_TRANSLATE_UPDATE, null);

		form = new FormImpl(this);

		rootWindowId = WindowInstance.getRootWindowId(windowId);

		ContextUtils.setReqInfo(this);
	}

	@Override
	public String getParameter(String name, String def) {
		String str;

		str = request.getParameter(name);
		if (str == null)
			return def;
		return str.trim();
	}

	@Override
	public int getParameter(String name, int def) {
		return Integer.parseInt(getParameter(name, Integer.toString(def)));
	}

	@Override
	public long getParameter(String name, long def) {
		return Long.parseLong(getParameter(name, Long.toString(def)));
	}

	/**
	 * Save the request attributes set by action to session in order to pass
	 * them to window request
	 */
	public void saveActionRequestParameters() {
		HttpSession ses = getSession();

		if (actionId == null || wi == null || request == null || ses == null
				|| !(request instanceof RequestAttributeRecorder))
			return;

		ses.setAttribute(wi.getWinlet().toString()
				+ REQ_PARAMETERS_FROM_ACTION_KEY,
				((RequestAttributeRecorder) request).getRecorded());
	}

	public void setWindowIntance(WindowInstance wi) {
		HttpSession ses = getSession();

		this.wi = wi;

		// { retrieve attributes passed to window by action
		if (actionId == null && request != null && ses != null) {
			@SuppressWarnings("unchecked")
			HashMap<String, Object> attrs = (HashMap<String, Object>) ses
					.getAttribute(wi.getWinlet().toString()
							+ REQ_PARAMETERS_FROM_ACTION_KEY);
			if (attrs != null) {
				for (String key : attrs.keySet())
					request.setAttribute(key, attrs.get(key));
				ses.removeAttribute(wi.getWinlet().toString()<commit_msg>synchronize HashMap<commit_after>package com.aggrepoint.winlet;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.owasp.esapi.errors.ValidationException;

import com.aggrepoint.winlet.form.Form;
import com.aggrepoint.winlet.form.FormImpl;
import com.aggrepoint.winlet.spring.RequestAttributeRecorder;
import com.aggrepoint.winlet.spring.WinletRequestWrapper;
import com.aggrepoint.winlet.spring.def.ReturnDef;
import com.aggrepoint.winlet.spring.def.WinletDef;
import com.aggrepoint.winlet.utils.BufferedResponse;

/**
 * 
 * @author <NAME> (<EMAIL>)
 */
public class ReqInfoImpl implements ReqConst, ReqInfo {
	private static final String REQ_PARAMETERS_FROM_ACTION_KEY = ".req.param.from.action";

	private static long REQUEST_ID = 0;

	private long requestId;

	private HttpServletRequest request;
	// 遇到当执行了一定逻辑后，request.getSession(true)会不定期返回null
	private HttpSession session;
	String requestPath;
	private String path;
	private String rootWindowId;
	private String windowId;
	private String pageId;
	private String pageUrl;
	private String actionId;
	private String validateFieldName;
	private String validateFieldValue;
	private String validateFieldId;
	private boolean pageRefresh;
	private String translateUpdate;
	private WindowInstance wi;
	private Form form;
	private PageStorage ps;
	private SharedPageStorage sps;
	private ReturnDef rd;

	// 待移植
	public boolean m_bUseAjax = true;

	/**
	 * 用于分解Action或Resource
	 */
	Pattern P_DECODE = Pattern.compile("([^!]*)!([^!]+)");

	public ReqInfoImpl(HttpServletRequest request, String path) {
		this.request = request;
		this.session = request.getSession(true);
		this.path = request.getContextPath() + path;

		// 将IncludeTag需要获得的当前Winlet的RequestPath取出保存
		// 发现如果在WinletDispatcherServlet中用HttpServletRequestWrapper的派生类将request封装，请求forward到JSP页面后
		// ，用ReqInfoImpl中保存的request对象的getRequestURI()方法只能获得当前JSP的URI而不是期望的Winlet
		// URI。如果不封装，或者封装类不从HttpServletRequestWrapper派生，则ReqInfoImpl中request.getRequestURI()可以正常工作。为了避免这个问题，这里直接将requestURI取出保存。
		requestPath = request.getRequestURI();
		int idx = requestPath.indexOf("/", 1);
		requestPath = requestPath.substring(idx);

		requestId = REQUEST_ID++;

		pageId = getParameter(PARAM_PAGE_PATH, null);
		if (pageId == null)
			pageId = request.getRequestURI();

		pageUrl = getParameter(PARAM_PAGE_URL, null);
		if (pageUrl == null)
			pageUrl = request.getRequestURL().toString();

		windowId = request.getHeader(WinletConst.REQUEST_HEADER_WINDOW_ID);
		if (windowId == null || windowId.equals(""))
			windowId = getParameter(PARAM_WIN_ID, null);

		actionId = getParameter(PARAM_WIN_ACTION, null);
		if (actionId != null) {
			Matcher m;
			synchronized (P_DECODE) {
				m = P_DECODE.matcher(actionId);
			}

			if (m.find()) {
				try {
					windowId = m.group(1);
				} catch (Exception e) {
				}
				actionId = m.group(2);
			}
		} else {
			pageRefresh = "yes".equalsIgnoreCase(getParameter(
					PARAM_PAGE_REFRESH, ""));
		}

		validateFieldName = getParameter(PARAM_WIN_VALIDATE_FIELD, null);
		if (validateFieldName != null) {
			validateFieldValue = getParameter(PARAM_WIN_VALIDATE_FIELD_VALUE,
					"");
			validateFieldId = getParameter(PARAM_WIN_VALIDATE_FIELD_ID, "");
		}

		translateUpdate = getParameter(PARAM_TRANSLATE_UPDATE, null);

		form = new FormImpl(this);

		rootWindowId = WindowInstance.getRootWindowId(windowId);

		ContextUtils.setReqInfo(this);
	}

	@Override
	public String getParameter(String name, String def) {
		String str;

		str = request.getParameter(name);
		if (str == null)
			return def;
		return str.trim();
	}

	@Override
	public int getParameter(String name, int def) {
		return Integer.parseInt(getParameter(name, Integer.toString(def)));
	}

	@Override
	public long getParameter(String name, long def) {
		return Long.parseLong(getParameter(name, Long.toString(def)));
	}

	/**
	 * Save the request attributes set by action to session in order to pass
	 * them to window request
	 */
	public void saveActionRequestParameters() {
		HttpSession ses = getSession();

		if (actionId == null || wi == null || request == null || ses == null
				|| !(request instanceof RequestAttributeRecorder))
			return;

		ses.setAttribute(wi.getWinlet().toString()
				+ REQ_PARAMETERS_FROM_ACTION_KEY,
				((RequestAttributeRecorder) request).getRecorded());
	}

	public void setWindowIntance(WindowInstance wi) {
		HttpSession ses = getSession();

		this.wi = wi;

		// { retrieve attributes passed to window by action
		if (actionId == null && request != null && ses != null) {
			@SuppressWarnings("unchecked")
			HashMap<String, Object> attrs = (HashMap<String, Object>) ses
					.getAttribute(wi.getWinlet().toString()
							+ REQ_PARAMETERS_FROM_ACTION_KEY);
			if (attrs != null) {
				synchronized (attrs) {
					for (String key : attrs.keySet())
						request.setAttribute(key, attrs.get(key));
				}
				ses.removeAttribute(wi.getWinlet().toString()
{'fraction_non_alphanumeric': 0.11140846466448608, 'fraction_numerical': 0.0007929428089999009, 'mean_word_length': 4.852668213457076, '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': '4042057', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3343, 'n_words': 672}