P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.handler;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.json.JObjectReader;
import foundation.route.Operation;
import foundation.token.IOnlineUser;
import foundation.util.MapList;
import foundation.util.Util;
 
public class DataPool implements IDataPool {
 
    protected static Logger logger;
    public static String ProviderName = "dataPool";
    private IOnlineUser user;
    private HttpServletRequest request;
    private MapList<String, String> params;
    private DataPoolType type;
    private Operation operation;
    private JObjectReader jObjectReader;
    private String token;
    private String body;
 
    static {
        logger = LogManager.getLogger();
    }
    
    public DataPool(HttpServletRequest request) throws Exception {
        params = new MapList<String, String>();
        this.request = request;
        
        loadParams();
        parseType(request);
    }
 
    private void parseType(HttpServletRequest request) {
        String method = request.getMethod();
        method = method.toLowerCase();
        
        String contentCount = request.getContentType();
        
        if (contentCount == null) {
            contentCount = "";
        }
        contentCount = contentCount.toLowerCase();
        
        if (contentCount.indexOf("multipart") >= 0) {
            type = DataPoolType.MultiPart;
        }
        else if ("get".equals(method)) {
            type = DataPoolType.Get;
        }
        else if ("options".equals(method)) {
            type = DataPoolType.Options;
        }
        else {
            type = DataPoolType.Post;
        }
    }
 
    public String getToken() throws IOException {
        //1. read from URL parameters
        token = params.get("token");
        
        if (!Util.isEmpty(token)) {
            return token;
        }
        
        //2. read from body
        getJObjectReader();
        
        if (jObjectReader == null) {
            return null;
        }
        
        token = jObjectReader.getString("token");
        return token;
    }
    
    private void loadParams() throws Exception {
        Enumeration<String> paramNames = request.getParameterNames();
 
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String value = request.getParameter(paramName);
            loadOneParam(paramName, value);
        }
    }
    
    public void loadOneParam(String name, String value) {
        if (Util.isEmpty(name)) {
            return;
        }
        
        params.add(name, value);
    }
 
    private void loadBody() throws IOException {
        if (body != null) {
            return;
        }
        
        InputStream inputStream = request.getInputStream();
        
        //1. read stream
        StringBuilder result = new StringBuilder();
        String line;
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
        
        body = result.toString();
        logger.debug("request: {}", body);
        
        jObjectReader = new JObjectReader(body);
    }
    
    public String getParam(String name) {
        return params.get(name);
    }
 
    public HttpServletRequest getRequest() {
        return request;
    }
 
    public String getRemoteIP() {
        return request.getRemoteAddr();
    }
 
    public Operation getOperation() {
        return operation;
    }
 
    public void setOperation(Operation operation) {
        this.operation = operation;
    }
 
    public String getBody() throws IOException {
        if (body == null) {
            loadBody();
        }
        
        return body;
    }
 
    @Override
    public synchronized JObjectReader getJObjectReader() throws IOException {
        if (jObjectReader == null && (DataPoolType.MultiPart != type)) {
            loadBody();
        }
        
        return jObjectReader;
    }
 
    public MapList<String, String> getParams() {
        return params;
    }
 
    public DataPoolType getType() {
        return type;
    }
 
    public IOnlineUser getUser() {
        return user;
    }
 
    public void setUser(IOnlineUser user) {
        this.user = user;
    }
}