david-PC\david
2018-06-12 cc7f57619fd09f68582b748a3580402717b84c50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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<String, Method> methodMap;
    protected Map<String, Method> allMethodMap;
    protected boolean fireReplay;
 
    static {
        logger = Logger.getLogger(HttpObject.class);
    }
 
    public HttpObject() {
        methodMap = new HashMap<String, Method>();
        allMethodMap = new HashMap<String, Method>();
        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;
    }
 
}