1 | package org.greenstone.tabletop;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.net.MalformedURLException;
|
---|
11 |
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.logging.Level;
|
---|
18 | import java.util.logging.Logger;
|
---|
19 | import java.util.regex.Matcher;
|
---|
20 | import java.util.regex.Pattern;
|
---|
21 |
|
---|
22 | import org.apache.commons.io.FileUtils;
|
---|
23 |
|
---|
24 | import javax.servlet.Servlet;
|
---|
25 | import javax.servlet.ServletConfig;
|
---|
26 | import javax.servlet.ServletContext;
|
---|
27 | import javax.servlet.ServletException;
|
---|
28 | import javax.servlet.http.HttpServlet;
|
---|
29 | import javax.servlet.http.HttpServletRequest;
|
---|
30 | import javax.servlet.http.HttpServletResponse;
|
---|
31 |
|
---|
32 | import org.json.JSONObject;
|
---|
33 |
|
---|
34 | //import org.eclipse.jetty.server.Server;
|
---|
35 |
|
---|
36 | import org.greenstone.tabletop.action.BaseAction;
|
---|
37 | import org.greenstone.tabletop.action.CheckExistsAction;
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Servlet implementation class to act as a bridge between
|
---|
42 | * Greenstone3 and the XML format the Touch Tabletop software expects
|
---|
43 | */
|
---|
44 | public class ConnectProxyServlet extends HttpServlet
|
---|
45 | {
|
---|
46 | private static final long serialVersionUID = 1L;
|
---|
47 | protected static Logger logger = Logger.getLogger(ConnectProxyServlet.class.getName());
|
---|
48 |
|
---|
49 | protected static String[] TableNames_ = null;
|
---|
50 |
|
---|
51 | protected static CheckExistsAction check_exists_ = null;
|
---|
52 | protected static ArrayList<BaseAction> action_list_ = null;
|
---|
53 |
|
---|
54 | protected String GSDL3SRCHOME_ = null;
|
---|
55 | protected String GSDL3_LIBRARY_URL_ = null;
|
---|
56 |
|
---|
57 | protected HashMap<String,String> table_to_url_ = null;
|
---|
58 |
|
---|
59 | public ConnectProxyServlet() {
|
---|
60 |
|
---|
61 | if (TableNames_ == null) {
|
---|
62 | TableNames_ = new String[] {
|
---|
63 | "Tbl_Acknowledgements",
|
---|
64 | "Tbl_CoolStuff",
|
---|
65 | "Tbl_EventType",
|
---|
66 | "Tbl_MajorEvents",
|
---|
67 | "Tbl_Maps",
|
---|
68 | "Tbl_People",
|
---|
69 | "Tbl_Per_Events",
|
---|
70 | "Tbl_Pictures",
|
---|
71 | "Tbl_Sponsors",
|
---|
72 | "Tbl_Timeline",
|
---|
73 | "Tbl_Videos",
|
---|
74 | "Team"
|
---|
75 | };
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected static Map<String,List<String>> convertParamMapArrayToMapList(Map<String,String []> map_array)
|
---|
80 | {
|
---|
81 | Map<String,List<String>> map_list = new HashMap<String,List<String>>();
|
---|
82 |
|
---|
83 | for (String key: map_array.keySet()) {
|
---|
84 | List<String> val_list = Arrays.asList(map_array.get(key));
|
---|
85 |
|
---|
86 | map_list.put(key, val_list);
|
---|
87 | }
|
---|
88 |
|
---|
89 | return map_list;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | // keep around, could be useful if needing to convert Map data in the other direction
|
---|
94 | protected static Map<String,String[]> convertParamMapListToMapArray(Map<String,List<String>> map_list)
|
---|
95 | {
|
---|
96 | Map<String,String[]> map_array = new HashMap<String,String[]>();
|
---|
97 |
|
---|
98 | for (String key: map_list.keySet()) {
|
---|
99 | String [] val_array = (String[]) map_list.get(key).toArray();
|
---|
100 | map_array.put(key, val_array );
|
---|
101 | }
|
---|
102 |
|
---|
103 | return map_array;
|
---|
104 | }
|
---|
105 | */
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @see Servlet#init(ServletConfig)
|
---|
110 | */
|
---|
111 | public void init(ServletConfig config) throws ServletException {
|
---|
112 | super.init(config);
|
---|
113 |
|
---|
114 | GSDL3SRCHOME_ = config.getInitParameter("GSDL3SRCHOME");
|
---|
115 | GSDL3_LIBRARY_URL_ = config.getInitParameter("GSDL3_LIBRARY_URL");
|
---|
116 |
|
---|
117 | table_to_url_ = new HashMap<String,String>();
|
---|
118 | for (String table_name: TableNames_) {
|
---|
119 | String table_url = config.getInitParameter(table_name);
|
---|
120 | if ((table_url != null) && (!table_url.equals(""))) {
|
---|
121 | table_to_url_.put(table_name,table_url);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | ServletContext context = getServletContext();
|
---|
127 |
|
---|
128 | if (check_exists_ == null) {
|
---|
129 | check_exists_ = new CheckExistsAction(context,config);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | if (action_list_ == null) {
|
---|
134 | action_list_ = new ArrayList<BaseAction>();
|
---|
135 | action_list_.add(check_exists_);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | protected void displayUsage(HttpServletResponse http_servlet_response)
|
---|
141 | {
|
---|
142 | try {
|
---|
143 | PrintWriter pw = http_servlet_response.getWriter();
|
---|
144 |
|
---|
145 | pw.append("Usage:\n");
|
---|
146 |
|
---|
147 | for (BaseAction action: action_list_) {
|
---|
148 |
|
---|
149 | pw.append(" action=" + action.getHandle() + "\n");
|
---|
150 | String[] mess = action.getDescription();
|
---|
151 | for (String sm: mess) {
|
---|
152 | pw.append(" " + sm + "\n");
|
---|
153 | }
|
---|
154 | pw.append("\n");
|
---|
155 | }
|
---|
156 | pw.close();
|
---|
157 | }
|
---|
158 | catch (Exception e) {
|
---|
159 | e.printStackTrace();
|
---|
160 | }
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | public String escapeHTML(String s)
|
---|
165 | {
|
---|
166 | StringBuilder out = new StringBuilder(Math.max(16, s.length()));
|
---|
167 | for (int i = 0; i < s.length(); i++) {
|
---|
168 | char c = s.charAt(i);
|
---|
169 | if (c == '"' || c == '<' || c == '>' || c == '&') {
|
---|
170 | out.append("&#");
|
---|
171 | out.append((int) c);
|
---|
172 | out.append(';');
|
---|
173 | } else {
|
---|
174 | out.append(c);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | return out.toString();
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected String convertGS3UrlToFile(String gs3_url)
|
---|
181 | {
|
---|
182 | String gs3_file = gs3_url
|
---|
183 | .replace("/greenstone3/library",GSDL3SRCHOME_+"/web")
|
---|
184 | .replaceAll("/","\\\\");
|
---|
185 |
|
---|
186 | return gs3_file;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | protected String convertGS3Urls(String xml_text)
|
---|
191 | {
|
---|
192 | Pattern gsdlurl_pattern = Pattern.compile("(<GS3URL>)(.*?)(</GS3URL>)");
|
---|
193 | Matcher gsdlurl_matcher = gsdlurl_pattern.matcher(xml_text);
|
---|
194 |
|
---|
195 | StringBuilder mapped_output_builder = new StringBuilder();
|
---|
196 |
|
---|
197 | int last_index = 0;
|
---|
198 |
|
---|
199 | while (gsdlurl_matcher.find()) {
|
---|
200 | mapped_output_builder.append(xml_text, last_index, gsdlurl_matcher.start());
|
---|
201 | mapped_output_builder.append(convertGS3UrlToFile(gsdlurl_matcher.group(2)));
|
---|
202 |
|
---|
203 | last_index = gsdlurl_matcher.end();
|
---|
204 | }
|
---|
205 | if (last_index < xml_text.length()) {
|
---|
206 | mapped_output_builder.append(xml_text, last_index, xml_text.length());
|
---|
207 | }
|
---|
208 |
|
---|
209 | String mapped_output = mapped_output_builder.toString();
|
---|
210 |
|
---|
211 | return mapped_output;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | protected void doTableOutput(HttpServletRequest request, HttpServletResponse response, String table_name )
|
---|
216 | throws MalformedURLException, IOException
|
---|
217 | {
|
---|
218 | String table_url = table_to_url_.get(table_name);
|
---|
219 | URL dl_url = new URL(GSDL3_LIBRARY_URL_ + table_url);
|
---|
220 |
|
---|
221 | InputStreamReader isr = new InputStreamReader(dl_url.openStream(),StandardCharsets.UTF_8);
|
---|
222 | BufferedReader in = new BufferedReader(isr);
|
---|
223 |
|
---|
224 | response.setHeader("Content-type", "text/xml");
|
---|
225 | response.setCharacterEncoding("UTF-8");
|
---|
226 |
|
---|
227 | PrintWriter pw = response.getWriter();
|
---|
228 | pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
---|
229 |
|
---|
230 | String input_line;
|
---|
231 | while ((input_line = in.readLine()) != null) {
|
---|
232 | //logger.info("input_line = '" + input_line + "'");
|
---|
233 | String converted_input_line1 = convertGS3Urls(input_line);
|
---|
234 | String converted_input_line2 = converted_input_line1.replaceAll("<query id=\"classifiers\">","<query>");
|
---|
235 | String converted_input_line3 = converted_input_line2.replaceAll("<c/>","<c></c>");
|
---|
236 | String converted_input_line_final = converted_input_line3.replaceAll("<c />","<c></c>");
|
---|
237 |
|
---|
238 | if (!converted_input_line_final.matches("^\\s*$")) {
|
---|
239 | //logger.info("converted_line = " + converted_input_line_final);
|
---|
240 | pw.append(converted_input_line_final + "\n");
|
---|
241 | }
|
---|
242 |
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | protected void doTblPeopleDEPRECATED(HttpServletRequest request, HttpServletResponse response)
|
---|
248 | throws MalformedURLException, IOException
|
---|
249 | {
|
---|
250 | // http://localhost:8383/greenstone3/library/collection/computing-50-tabletop/browse/CL1?sa=tabletop&excerptid=classifiers");
|
---|
251 |
|
---|
252 | String Tbl_People_url = table_to_url_.get("Tbl_People");
|
---|
253 | URL dl_url = new URL(GSDL3_LIBRARY_URL_ + Tbl_People_url);
|
---|
254 |
|
---|
255 | InputStreamReader isr = new InputStreamReader(dl_url.openStream(),StandardCharsets.UTF_8);
|
---|
256 | BufferedReader in = new BufferedReader(isr);
|
---|
257 |
|
---|
258 | response.setHeader("Content-type", "text/xml");
|
---|
259 | response.setCharacterEncoding("UTF-8");
|
---|
260 |
|
---|
261 | PrintWriter pw = response.getWriter();
|
---|
262 | pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
---|
263 |
|
---|
264 | String input_line;
|
---|
265 | while ((input_line = in.readLine()) != null) {
|
---|
266 | //logger.info("input_line = '" + input_line + "'");
|
---|
267 | String converted_input_line1 = convertGS3Urls(input_line);
|
---|
268 | String converted_input_line2 = converted_input_line1.replaceAll("<query id=\"classifiers\">","<query>");
|
---|
269 | String converted_input_line3 = converted_input_line2.replaceAll("<c/>","<c></c>");
|
---|
270 | String converted_input_line_final = converted_input_line3.replaceAll("<c />","<c></c>");
|
---|
271 |
|
---|
272 | if (!converted_input_line_final.matches("^\\s*$")) {
|
---|
273 | //logger.info("converted_line = " + converted_input_line_final);
|
---|
274 | pw.append(converted_input_line_final + "\n");
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
281 | throws ServletException, IOException
|
---|
282 | {
|
---|
283 | //logger.info("Handling path quest: request = " + request.toString());
|
---|
284 |
|
---|
285 | Map<String, String[]> param_map_array = request.getParameterMap();
|
---|
286 |
|
---|
287 | String[] query_args = param_map_array.get("query");
|
---|
288 |
|
---|
289 | if ((query_args != null) && (query_args.length>0)) {
|
---|
290 |
|
---|
291 | String query_arg = query_args[query_args.length-1];
|
---|
292 |
|
---|
293 | logger.info("Processing query argument: '" + query_arg + "'");
|
---|
294 | String export_filename = null;
|
---|
295 |
|
---|
296 | for (String table_name: TableNames_) {
|
---|
297 | if (query_arg.indexOf(table_name)>=0) {
|
---|
298 | logger.info("ConnectProxy matching on table-name: " + table_name);
|
---|
299 | export_filename = table_name + ".xml";
|
---|
300 | break;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | //final String __dirname = ".";
|
---|
305 | //final String __dirname = "C:/Temp";
|
---|
306 | final String __dirname = GSDL3SRCHOME_ + "/web/sites/computing50/ConnectProxyServlet";
|
---|
307 |
|
---|
308 | if (export_filename != null) {
|
---|
309 |
|
---|
310 | if (export_filename.equals("Tbl_People.xml")) {
|
---|
311 | doTableOutput(request,response,"Tbl_People");
|
---|
312 | }
|
---|
313 | else if (export_filename.equals("Tbl_Pictures.xml")) {
|
---|
314 | doTableOutput(request,response,"Tbl_Pictures");
|
---|
315 | }
|
---|
316 | else if (export_filename.equals("Tbl_CoolStuff.xml")) {
|
---|
317 | doTableOutput(request,response,"Tbl_CoolStuff");
|
---|
318 | }
|
---|
319 | else if (export_filename.equals("Tbl_Videos.xml")) {
|
---|
320 | doTableOutput(request,response,"Tbl_Videos");
|
---|
321 | }
|
---|
322 | else if (export_filename.equals("Tbl_Maps.xml")) {
|
---|
323 | doTableOutput(request,response,"Tbl_Maps");
|
---|
324 | }
|
---|
325 | else if (export_filename.equals("Tbl_Timeline.xml")) {
|
---|
326 | doTableOutput(request,response,"Tbl_Timeline");
|
---|
327 | }
|
---|
328 | else if (export_filename.equals("Tbl_MajorEvents.xml")) {
|
---|
329 | doTableOutput(request,response,"Tbl_MajorEvents");
|
---|
330 | }
|
---|
331 | else {
|
---|
332 |
|
---|
333 | String waikato_export_filename = "Waikato_" + export_filename;
|
---|
334 | File waikato_src_file = new File(__dirname + "/" + waikato_export_filename);
|
---|
335 |
|
---|
336 | File srcFile = null;
|
---|
337 | if (waikato_src_file.exists()) {
|
---|
338 | logger.info("Detected Waikato version of: " + export_filename);
|
---|
339 | srcFile = waikato_src_file;
|
---|
340 | }
|
---|
341 | else {
|
---|
342 | srcFile = new File(__dirname + "/" + export_filename);
|
---|
343 | }
|
---|
344 |
|
---|
345 | response.setHeader("Content-type", "text/xml");
|
---|
346 | response.setCharacterEncoding("UTF-8");
|
---|
347 |
|
---|
348 | FileUtils.copyFile(srcFile, response.getOutputStream());
|
---|
349 | }
|
---|
350 | }
|
---|
351 | else {
|
---|
352 | logger.log(Level.SEVERE, "Requessted URL did not match one of the predefined table names");
|
---|
353 | logger.log(Level.SEVERE, Arrays.toString(TableNames_));
|
---|
354 | response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | else {
|
---|
358 | try {
|
---|
359 | PrintWriter pw = response.getWriter();
|
---|
360 |
|
---|
361 | response.setHeader("Content-type", "text/xml");
|
---|
362 | pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ping />");
|
---|
363 | logger.info("Responding with 'ping'");
|
---|
364 |
|
---|
365 | pw.close();
|
---|
366 | }
|
---|
367 | catch (Exception e) {
|
---|
368 | e.printStackTrace();
|
---|
369 | response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | protected void doGet_ACTION_BASED(HttpServletRequest request, HttpServletResponse response)
|
---|
375 | throws ServletException, IOException
|
---|
376 | {
|
---|
377 | Map<String, String[]> param_map_array = request.getParameterMap();
|
---|
378 | Map<String, List<String>> param_map_list = convertParamMapArrayToMapList(param_map_array);
|
---|
379 |
|
---|
380 | boolean action_match = false;
|
---|
381 | String action_handle = BaseAction.getParameter(param_map_list,"action");
|
---|
382 |
|
---|
383 | for (BaseAction action: action_list_) {
|
---|
384 | if (action.getHandle().equals(action_handle)) {
|
---|
385 | action_match = true;
|
---|
386 |
|
---|
387 | action.doAction(param_map_list,response);
|
---|
388 | break;
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (!action_match) {
|
---|
393 | // No action given => generate usage statement
|
---|
394 | response.setContentType("text/plain");
|
---|
395 | displayUsage(response);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
402 | */
|
---|
403 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
404 | throws ServletException, IOException {
|
---|
405 | doGet(request, response);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | }
|
---|