package frame.object.http; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import frame.call.writer.EnvelopWriter; import frame.role.OnlineUser; import frame.role.UserRight; import frame.util.Util; public abstract class HttpObject { protected static Logger logger; protected HttpServletResponse response; protected HttpServletRequest request; protected OnlineUser onlineUser; protected EnvelopWriter writer; protected ResultPool resultPool; protected HttpDataPool dataPool; protected Path path ; protected Map methodMap; protected Map allMethodMap; protected boolean fireReplay; static { logger = Logger.getLogger(HttpObject.class); } public HttpObject() { methodMap = new HashMap(); allMethodMap = new HashMap(); fireReplay = true; collectMethod(); publishMethod(); } public void receive(String pathString) { //path为 shortTarget resultPool = new ResultPool(); try { path = new Path(pathString); if (!path.isValid()) { writer.ReplyError("bad path:" + pathString); } dataPool = new HttpDataPool(); dataPool.load(request); writer = new EnvelopWriter(request, response); try { if (onlineUser != null) { try { //1. check user right UserRight userRight = onlineUser.getUserRight(); if (!userRight.isValid(path)) { resultPool.setInvalidUserRight(path); return; } //2. execute doReceive(path); } catch (Exception e) { onError(e); } } else { writer.replayTimeout(); } if (fireReplay) { writer.replay(resultPool); writer.flush(); } } catch (Exception e) { String error = e.getClass().getName() + ": " + e.getMessage(); writer.ReplyError(error); logger.error("ajax error: " + e); } } catch (Exception e) { } } protected void doReceive(Path path) throws Exception { String lower = path.getOperator().toLowerCase(); Method method = methodMap.get(lower); if (method == null) { method = methodMap.get("call"); } if (method != null) { try { beforeExecute(); method.invoke(this); } catch (InvocationTargetException e) { Throwable throwable = e.getTargetException(); if (throwable == null) { throw e; } else { throw (Exception) throwable; } } } else { resultPool.error("method not exists: " + path.getPathString()); } } protected void beforeExecute() throws Exception { } protected void onError(Exception e) throws Exception { logger.error(e); e.printStackTrace(); } public HttpDataPool getDataPool() throws Exception { return dataPool; } protected void collectMethod() { Method[] methods = this.getClass().getDeclaredMethods(); for (Method one : methods) { allMethodMap.put(one.getName().toLowerCase(), one); } } protected void publishMethod() { } protected void addMethod(String name) { if (name == null) { return; } name = name.toLowerCase(); Method method = allMethodMap.get(name); if (method != null) { method.setAccessible(true); methodMap.put(name, method); } } protected String locateSQLVariant(String name) throws Exception { String value = request.getParameter(name); if (value != null && value.startsWith("@{")) { name = value.subString(2, value.length() - 1); value = getDefaultParameter(name); return value; } else if (value != null) { return value; } value = getDefaultParameter(name); return value; } protected String getDefaultParameter(String name) { if ("userid".equalsIgnoreCase(name)) { return onlineUser.getId(); } else if ("rolecode".equalsIgnoreCase(name)) { return onlineUser.getRoleCode(); } else if ("username".equalsIgnoreCase(name)) { return onlineUser.getName(); } else if ("orgid".equalsIgnoreCase(name)) { return onlineUser.getOrgId(); } else if ("newid".equalsIgnoreCase(name)) { return Util.newShortGUID(); } else if ("newDate".equalsIgnoreCase(name)) { return Util.DataTimeToString(new Date()); } else if ("filter".equalsIgnoreCase(name)) { return "1 = 1"; } return null; } public HttpServletRequest getRequest() { return request; } public HttpServletResponse getResponse() { return response; } public OnlineUser getOnlineUser() { return onlineUser; } public ResultPool getResultPool() { return resultPool; } public Path getPath() { return path; } public EnvelopWriter getWriter() { return writer; } }