<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ page import="net.sf.json.JSONObject" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="java.util.concurrent.ConcurrentHashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="weaver.security.core.MD5" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.io.FileUtils" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.UUID" %>
<%@ page import="weaver.security.util.HttpClientUtil,weaver.filter.XssUtil" %>
<jsp:useBean id="xssUtil" class="weaver.filter.XssUtil" scope="page"></jsp:useBean><%
	User user = HrmUserVarify.getUser (request , response) ;
	JSONObject result = new JSONObject();
	if(user == null)  {
		result.put("result", false);
		result.put("msg", "用户为空。");
		out.println(result.toString());
		return;
	} ;
	int UID = xssUtil.getIntValue(""+xssUtil.getRule().get("userID"),1);
	if (user.getUID()!=UID)
	{
		result.put("result", false);
		result.put("msg", "用户无权限。");
		out.println(result.toString());
		return;
	}

	String node = request.getParameter("node");
	if(node == null || "".equals(node)){
		result.put("result", false);
		result.put("msg", "节点信息为空。");
		out.println(result.toString());
		return;
	}
	String nodes = new BaseBean().getPropValue("weaver","initial_hosts");
	if(nodes!=null && !"".equals(nodes)){
		Map<String, String> ipMap = new ConcurrentHashMap<String, String>();
		String[] nodeArr = nodes.split(",");
		String ip = "";
		for(String node1 : nodeArr) {
			String md5 = new MD5().getMD5ofStr(node1);
			if(md5.equals(node)){
				ip = node1;
			}
		}
		if(ip == null || "".equals(ip)){
			result.put("result", false);
			result.put("msg", "未找到要刷新的节点信息。");
			out.println(result.toString());
			return;
		}
		File tokenFile = new File(xssUtil.getRootPath() + File.separatorChar + "WEB-INF" + File.separatorChar+"securityRule"+File.separatorChar+"weaver_security_cache_token");
		if(!tokenFile.exists()){
			tokenFile.createNewFile();
			FileUtils.writeStringToFile(tokenFile, UUID.randomUUID().toString());
		}
		String token = FileUtils.readFileToString(tokenFile);
		if(token == null || "".equals(token.trim())){
			token = UUID.randomUUID().toString();
			FileUtils.writeStringToFile(tokenFile, token);
		}
		try{
			String src = request.getParameter("src");
			String res = HttpClientUtil.get("http://"+ip+"/security/monitor/updateCacheInner.jsp?rnd="+token+"&src="+src);
			if((res == null || "".equals(res))){
				if(!"class".equals(src)){
					result.put("result", false);
					result.put("msg", "刷新缓存失败，请求响应为空。");
				}else{
					result.put("result", true);
					result.put("refreshTime", XssUtil.getCurrentTimeString());
					result.put("msg", "刷新缓存成功。");
				}
				out.println(result.toString());
				return;
			}else{
				result = JSONObject.fromObject(res);
				out.println(result);
				return;
			}
		}catch(Exception e){
			new BaseBean().writeLog(e);
			result.put("result",false);
			result.put("msg","远程调用异常，刷新缓存失败。");
			out.println(result);
		}

	}else{
		result.put("result", false);
		result.put("msg", "未找到集群节点信息。");
		out.println(result.toString());
		return;
	}
%>