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 package org.utgenome.gwt.utgb.server;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.util.ArrayList;
31 import java.util.Enumeration;
32 import java.util.List;
33
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.utgenome.UTGBErrorCode;
39 import org.utgenome.UTGBException;
40 import org.xerial.core.XerialException;
41 import org.xerial.db.DBException;
42 import org.xerial.db.sql.DatabaseAccess;
43 import org.xerial.db.sql.SQLExpression;
44 import org.xerial.lens.JSONLens;
45 import org.xerial.util.FileResource;
46 import org.xerial.util.ObjectHandler;
47 import org.xerial.util.ObjectHandlerBase;
48 import org.xerial.util.StringUtil;
49 import org.xerial.util.log.Logger;
50
51
52
53
54
55 public abstract class WebTrackBase extends RequestHandlerBase {
56
57 private static final long serialVersionUID = 1L;
58 private static Logger _logger = Logger.getLogger(WebTrackBase.class);
59
60 public static String createSQLStatement(String sqlTemplate, Object... args) throws UTGBException {
61 try {
62 return SQLExpression.fillTemplate(sqlTemplate, args);
63 }
64 catch (DBException e) {
65 throw new UTGBException(UTGBErrorCode.MaliciousSQLSyntax, e);
66 }
67 }
68
69
70
71
72
73
74
75
76 public DatabaseAccess getDatabaseAccess(String databaseID) throws UTGBException {
77 return UTGBMaster.getDatabaseAccess(databaseID);
78 }
79
80
81
82
83
84
85
86 public String getTrackConfigProperty(String key, String defaultValue) {
87 return UTGBMaster.getUTGBConfig().getProperty(key, defaultValue);
88 }
89
90
91
92
93
94
95
96
97
98
99 public <T> void toJSON(String databaseID, String sql, Class<T> resultClass, HttpServletResponse response) throws UTGBException, IOException {
100 DatabaseAccess db;
101 try {
102 db = getDatabaseAccess(databaseID);
103 db.toJSON(sql, resultClass, response.getWriter());
104 }
105 catch (DBException e) {
106 throw new UTGBException(UTGBErrorCode.DatabaseError, e);
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public <T> void loadJSON(Reader jsonStream, Class<T> resultClass, ObjectHandler<T> resultHandler) throws UTGBException, IOException {
122 try {
123 JSONLens.loadJSON(resultClass, jsonStream, resultHandler);
124 }
125 catch (XerialException e) {
126 throw new UTGBException(UTGBErrorCode.JSONToObjectMapping, e);
127 }
128 }
129
130 private static class JSONAccumulator<T> extends ObjectHandlerBase<T> {
131 private ArrayList<T> result = new ArrayList<T>();
132
133 public void handle(T bean) throws Exception {
134 result.add(bean);
135 }
136
137 public ArrayList<T> getResult() {
138 return result;
139 }
140
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public <T> List<T> loadJSON(Reader jsonStream, Class<T> resultClass) throws UTGBException, IOException {
154 try {
155 JSONAccumulator<T> ja = new JSONAccumulator<T>();
156 JSONLens.loadJSON(resultClass, jsonStream, ja);
157 return ja.getResult();
158 }
159 catch (XerialException e) {
160 throw new UTGBException(UTGBErrorCode.JSONToObjectMapping, e);
161 }
162
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public BufferedReader openAction(HttpServletRequest request, String path) throws ServletException, IOException {
176 return openAction(request, path, false);
177 }
178
179
180
181
182
183
184
185
186
187 public BufferedReader openAction(HttpServletRequest request, String path, boolean bypassRequestParameter) throws ServletException, IOException {
188 if (bypassRequestParameter) {
189 path = path + "?" + getHTTPRequestQueryString(request);
190 }
191
192 return UTGBMaster.openServletReader(request, path);
193 }
194
195 @SuppressWarnings("unchecked")
196 public static String getHTTPRequestQueryString(HttpServletRequest request) {
197 ArrayList<String> argList = new ArrayList<String>();
198
199 for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
200 String param = e.nextElement();
201 argList.add(param + "=" + request.getParameter(param));
202 }
203 return StringUtil.join(argList, "&");
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 public String createSQLFromFile(String sqlFileName, Object... args) throws IOException, UTGBException {
221 BufferedReader reader = FileResource.open(this.getClass(), sqlFileName);
222 if (reader == null) {
223 throw new UTGBException(UTGBErrorCode.FileNotFound, sqlFileName);
224 }
225 StringBuilder sql = new StringBuilder();
226 for (String line; (line = reader.readLine()) != null;) {
227 sql.append(line);
228 sql.append("\n");
229 }
230
231 try {
232 return SQLExpression.fillTemplate(sql.toString(), args);
233 }
234 catch (DBException e) {
235 throw new UTGBException(UTGBErrorCode.InvalidSQLSyntax, sql.toString());
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251 public String createSQL(String sqlTemplate, Object... args) throws UTGBException {
252 try {
253 return SQLExpression.fillTemplate(sqlTemplate, args);
254 }
255 catch (DBException e) {
256 throw new UTGBException(UTGBErrorCode.InvalidSQLSyntax, sqlTemplate);
257 }
258 }
259
260
261
262
263
264
265 public static String getProjectRootPath() {
266 Object value = UTGBMaster.getVariable("projectRoot");
267 return (value != null) ? value.toString() : "";
268 }
269
270
271
272
273
274
275
276 public String getActionSuffix(HttpServletRequest request) {
277 String suffix = (String) request.getAttribute("actionSuffix");
278 return suffix == null ? "" : suffix;
279 }
280
281
282
283
284
285
286
287 public String getActionPrefix(HttpServletRequest request) {
288 String prefix = (String) request.getAttribute("actionPrefix");
289 return prefix == null ? "" : prefix;
290 }
291
292 }