package com.lgx8.common.servlet;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.lgx8.common.KeyStore;
import com.lgx8.common.util.StrUtil;
/**
* @author chenbutao
* @version 1.0
*/
@Controller
public class KeyStoreController extends MultiActionController {
private static final String CONTENT_TYPE = "text/plain; charset=utf-8";
public static final String DEFAULT_KEY_NAME = BaseController.DEFAULT_KEY_NAME;
public static final String KEY_NAME = "key_name";
public KeyStoreController() {
super();
}
@RequestMapping(value="/setKey.do")
public void setKey(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType(CONTENT_TYPE);
String keyName = request.getParameter(KEY_NAME);
if (keyName == null || keyName.equals("")) {
keyName = DEFAULT_KEY_NAME;
}
String key = request.getParameter(keyName);
if (key == null || key.equals("")) {
response.getWriter().print("error[key is null]\n");
}
KeyStore keyStore = this.getKeyStore(request);
if (!keyStore.getKey().equals(keyName)) {
response.getWriter().print(
"error[keyStore.key[" + keyStore.getKey() + "]!=["
+ keyName + "] ]\n");
}
String checked = request.getParameter("checked");
String[] keyArray = key.split("\\|");
for (int i = 0; i < keyArray.length; i++){
if (keyArray[i] == null || keyArray[i].length() <= 0){
continue;
}else{
key = this.unescape(keyArray[i]);
if (key == null || key.equals("")) {
response.getWriter().print("error[key is null]\n");
}else{
if ("true".equals(checked)) {
keyStore.add(key);
} else {
keyStore.remove(key);
}
}
}
}
response.getWriter().print("ok\n");
}
@RequestMapping(value="/checkKey.do")
public void checkKey(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType(CONTENT_TYPE);
String keyName = request.getParameter(KEY_NAME);
if (keyName == null || keyName.equals("")) {
keyName = DEFAULT_KEY_NAME;
}
String key = request.getParameter(keyName);
key = this.unescape(key);
if (key == null || key.equals("")) {
response.getWriter().print("error[key is null]\n");
}
KeyStore keyStore = this.getKeyStore(request);
if (!keyStore.getKey().equals(keyName)) {
response.getWriter().print(
"error[keyStore.key[" + keyStore.getKey() + "]!=["
+ keyName + "] ]\n");
}
if (keyStore.contains(key)) {
keyStore.remove(key);
} else {
keyStore.add(key);
}
response.getWriter().print("ok\n");
}
@RequestMapping(value="/radioKey.do")
public void radioKey(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType(CONTENT_TYPE);
String keyName = request.getParameter(KEY_NAME);
if (keyName == null || keyName.equals("")) {
keyName = DEFAULT_KEY_NAME;
}
String key = request.getParameter(keyName);
key = this.unescape(key);
if (key == null || key.equals("")) {
response.getWriter().print("error[key is null]\n");
}
KeyStore keyStore = this.getKeyStore(request);
if (!keyStore.getKey().equals(keyName)) {
response.getWriter().print(
"error[keyStore.key[" + keyStore.getKey() + "]!=["
+ keyName + "] ]\n");
}
keyStore.clear();
keyStore.add(key);
response.getWriter().print("ok\n");
}
protected ActionSessionData getActionSessionData(HttpServletRequest request) {
HttpSession session = request.getSession();
ActionSessionData data = (ActionSessionData) session.getAttribute(BaseController.class.getName());
if (data == null) {
data = new ActionSessionData();
data.keyStore = this.createKeyStore();
session.setAttribute(BaseController.class.getName(), data);
}
return data;
}
protected KeyStore createKeyStore() {
return new KeyStore(DEFAULT_KEY_NAME);
}
/**
* @return ams.action.KeyStore
* @roseuid 4644358E01B5
*/
public KeyStore getKeyStore(HttpServletRequest request) {
return this.getActionSessionData(request).keyStore;
}
private String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) StrUtil.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) StrUtil.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
}