<commit_before>package org.javaosc.framework.web.assist;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javaosc.framework.constant.Constant;
import org.javaosc.framework.constant.Constant.ContentType;
import org.javaosc.framework.util.JsonUtil;
import org.javaosc.framework.web.ActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @description
 * @author <NAME>
 * @date 2014-09-09
 * Copyright 2014 Javaosc Team. All Rights Reserved.
 */
public class ContextHandler {
	
	private static final Logger log = LoggerFactory.getLogger(ContextHandler.class);
	
	/* ========== attribute ========== */
	
	public static void putAttr(Object... param){
		int length = param!=null?param.length : 0;
		if(length==2){
			ActionContext.getContext().getRequest().setAttribute(String.valueOf(param[0]), param[1]);
		}else if(length>2 && length%2==0){
			length = length/2;
			for(int i=0;i<length;i++){
			   ActionContext.getContext().getRequest().setAttribute(String.valueOf(param[2*i]), param[2*i+1]);
			}
		}else{
			log.error(Constant.JAVAOSC_EXCEPTION, "the number of param must be an even number!");
		}
	}
	
	/* ========== json ========== */
	
	public static void putJson(Object... param) {
		String content = "";
		ActionContext.getContext().getResponse().setContentType(ContentType.JSON.getValue());
		try {
			PrintWriter out = ActionContext.getContext().getResponse().getWriter();
			int length = param!=null?param.length : 0;
			if(length==1){
				content = JsonUtil.toJSON(param[0]);
			}else if(length>1 && length%2==0){
				length = length/2;
				HashMap<String, Object> buildResult = new HashMap<String, Object>();
				for(int i=0;i<length;i++){
					buildResult.put(String.valueOf(param[2*i]), param[2*i+1]);
				}
				content = JsonUtil.toJSON(buildResult);
			}else{
				log.error(Constant.JAVAOSC_EXCEPTION, "the number of param must be an even number!");
			}
			out.write(content);
			out.flush();
			out.close();
		} catch (IOException e) {
			log.error(Constant.JAVAOSC_EXCEPTION, e);;
		}finally{
			content = null;
			log.debug(content);
		}
	}
	
	/* ========== session ========== */
	
	public static void setSession(String name, Object value){
		ActionContext.getContext().getRequest().getSession().setAttribute(name, value);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getSession(String name, Class<T> cls){
		return (T)ActionContext.getContext().getRequest().getSession().getAttribute(name);
	}<commit_msg>rewrite json util ref<commit_after>package org.javaosc.framework.web.assist;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javaosc.framework.constant.Constant;
import org.javaosc.framework.constant.Constant.ContentType;
import org.javaosc.framework.util.JsonUtil;
import org.javaosc.framework.web.ActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @description
 * @author <NAME>
 * @date 2014-09-09
 * Copyright 2014 Javaosc Team. All Rights Reserved.
 */
public class ContextHandler {
	
	private static final Logger log = LoggerFactory.getLogger(ContextHandler.class);
	
	/* ========== attribute ========== */
	
	public static void putAttr(Object... param){
		int length = param!=null?param.length : 0;
		if(length==2){
			ActionContext.getContext().getRequest().setAttribute(String.valueOf(param[0]), param[1]);
		}else if(length>2 && length%2==0){
			length = length/2;
			for(int i=0;i<length;i++){
			   ActionContext.getContext().getRequest().setAttribute(String.valueOf(param[2*i]), param[2*i+1]);
			}
		}else{
			log.error(Constant.JAVAOSC_EXCEPTION, "the number of param must be an even number!");
		}
	}
	
	/* ========== json ========== */
	
	public static void putJson(Object... param) {
		String content = "";
		ActionContext.getContext().getResponse().setContentType(ContentType.JSON.getValue());
		try {
			PrintWriter out = ActionContext.getContext().getResponse().getWriter();
			int length = param!=null?param.length : 0;
			if(length==1){
				content = JsonUtil.toJson(param[0]);
			}else if(length>1 && length%2==0){
				length = length/2;
				HashMap<String, Object> buildResult = new HashMap<String, Object>();
				for(int i=0;i<length;i++){
					buildResult.put(String.valueOf(param[2*i]), param[2*i+1]);
				}
				content = JsonUtil.toJson(buildResult);
			}else{
				log.error(Constant.JAVAOSC_EXCEPTION, "the number of param must be an even number!");
			}
			out.write(content);
			out.flush();
			out.close();
		} catch (IOException e) {
			log.error(Constant.JAVAOSC_EXCEPTION, e);;
		}finally{
			content = null;
			log.debug(content);
		}
	}
	
	/* ========== session ========== */
	
	public static void setSession(String name, Object value){
		ActionContext.getContext().getRequest().getSession().setAttribute(name, value);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getSession(String name, Class<T> cls){
		return (T)ActionContext.getContext().getRequest().getSession().getAttribute(name);
	}
{'fraction_non_alphanumeric': 0.16944024205748864, 'fraction_numerical': 0.01399394856278366, 'mean_word_length': 4.6506410256410255, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6276875', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1665, 'n_words': 325}