1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.BufferedReader; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.io.InputStreamReader; |
---|
10 | import java.net.MalformedURLException; |
---|
11 | import java.net.URISyntaxException; |
---|
12 | import java.net.URL; |
---|
13 | import java.util.HashMap; |
---|
14 | |
---|
15 | import javax.servlet.http.HttpServletRequest; |
---|
16 | import javax.servlet.http.HttpSession; |
---|
17 | import javax.xml.transform.TransformerException; |
---|
18 | |
---|
19 | import net.sf.json.JSON; |
---|
20 | import net.sf.json.JSONArray; |
---|
21 | import net.sf.json.JSONObject; |
---|
22 | import net.sf.json.JSONSerializer; |
---|
23 | |
---|
24 | |
---|
25 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
26 | |
---|
27 | import com.opensymphony.xwork2.ActionSupport; |
---|
28 | |
---|
29 | import eu.clarin.cmdi.mdservice.model.Diagnostics; |
---|
30 | |
---|
31 | /** |
---|
32 | * main Struts 2 controller |
---|
33 | * responds to requests (collections, model, recordset) |
---|
34 | * by dispatching the requests to appropriate internal methods and filling the inputStream with the result |
---|
35 | * |
---|
36 | * @author vronk |
---|
37 | * |
---|
38 | */ |
---|
39 | public class GenericProxyAction extends ActionSupport |
---|
40 | implements ServletRequestAware |
---|
41 | { |
---|
42 | |
---|
43 | private static final long serialVersionUID = 1L; |
---|
44 | |
---|
45 | private static JSONArray repositories = null; |
---|
46 | |
---|
47 | private Diagnostics diagnostics; |
---|
48 | |
---|
49 | /** |
---|
50 | * Properties to be filled by Struts with according request-parameters |
---|
51 | */ |
---|
52 | private String actionkey; |
---|
53 | private String q; |
---|
54 | private String squery; |
---|
55 | private String cache = Cache.USE; |
---|
56 | private String collection; |
---|
57 | private String columns; |
---|
58 | private String startItem; |
---|
59 | private String maximumItems; |
---|
60 | private String sort; |
---|
61 | private String lang="en"; |
---|
62 | private int maxdepth; |
---|
63 | private String format; |
---|
64 | private String options; |
---|
65 | private String actionContentType; |
---|
66 | private String userMsg; |
---|
67 | private String repository; |
---|
68 | //private Map<String,Object> session; |
---|
69 | private HttpServletRequest request; |
---|
70 | private long duration = 0; |
---|
71 | |
---|
72 | public GenericProxyAction(){ |
---|
73 | super(); |
---|
74 | initialize(); |
---|
75 | } |
---|
76 | |
---|
77 | public void setRepositoryByIndex(int id){ |
---|
78 | try { |
---|
79 | setRepositories(GenericProxyAction.createRepositories()); |
---|
80 | // static repositories |
---|
81 | net.sf.json.JSONArray json = getRepositories(); |
---|
82 | if (json.size() > 0){ |
---|
83 | repository = json.getJSONObject(id).getString("name"); |
---|
84 | } |
---|
85 | } catch (IOException e) { |
---|
86 | // TODO Auto-generated catch block |
---|
87 | e.printStackTrace(); |
---|
88 | } catch (InterruptedException e) { |
---|
89 | // TODO Auto-generated catch block |
---|
90 | e.printStackTrace(); |
---|
91 | } catch (TransformerException e) { |
---|
92 | // TODO Auto-generated catch block |
---|
93 | e.printStackTrace(); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | protected void initialize(){ |
---|
98 | //repository = 1; |
---|
99 | setRepositoryByIndex(0); |
---|
100 | } |
---|
101 | |
---|
102 | public Diagnostics getDiagnostics(){ |
---|
103 | return this.diagnostics; |
---|
104 | } |
---|
105 | |
---|
106 | public void setDiagnostics(Diagnostics diagnostics){ |
---|
107 | this.diagnostics = diagnostics; |
---|
108 | } |
---|
109 | public Diagnostics Diagnostics(){ |
---|
110 | if (diagnostics == null){ |
---|
111 | diagnostics = new Diagnostics(); |
---|
112 | } |
---|
113 | return this.diagnostics; |
---|
114 | } |
---|
115 | public String getQ() { |
---|
116 | return q; |
---|
117 | } |
---|
118 | |
---|
119 | public void setQ(String pq) { |
---|
120 | if (pq == null) pq=""; |
---|
121 | /* |
---|
122 | if (q != null){ |
---|
123 | if (q.trim().length() == 0){ |
---|
124 | q = null; |
---|
125 | } |
---|
126 | } |
---|
127 | */ |
---|
128 | this.q = pq; |
---|
129 | } |
---|
130 | |
---|
131 | public String getSquery() { |
---|
132 | return squery; |
---|
133 | } |
---|
134 | |
---|
135 | public void setSquery(String psquery) { |
---|
136 | if (psquery==null) psquery=""; |
---|
137 | this.squery = psquery; |
---|
138 | } |
---|
139 | |
---|
140 | public String getCache() { |
---|
141 | return cache; |
---|
142 | } |
---|
143 | |
---|
144 | public void setCache(String cache) { |
---|
145 | this.cache = cache; |
---|
146 | } |
---|
147 | |
---|
148 | public String getCollection() { |
---|
149 | return collection; |
---|
150 | } |
---|
151 | |
---|
152 | public void setCollection(String collection) { |
---|
153 | this.collection = collection; |
---|
154 | } |
---|
155 | |
---|
156 | //TODO defaults |
---|
157 | public String getColumns() { |
---|
158 | String cols = columns; |
---|
159 | /*if (columns == null){ |
---|
160 | cols = "Id,Name,Title"; |
---|
161 | } |
---|
162 | */ |
---|
163 | return cols; |
---|
164 | } |
---|
165 | |
---|
166 | public void setColumns(String columns) { |
---|
167 | this.columns = columns; |
---|
168 | } |
---|
169 | |
---|
170 | public int getMaxdepth() { |
---|
171 | return maxdepth; |
---|
172 | } |
---|
173 | |
---|
174 | public void setMaxdepth(int maxdepth) { |
---|
175 | this.maxdepth = maxdepth; |
---|
176 | } |
---|
177 | |
---|
178 | public String getFormat() { |
---|
179 | return format; |
---|
180 | } |
---|
181 | |
---|
182 | public void setFormat(String format) { |
---|
183 | this.format = format; |
---|
184 | } |
---|
185 | |
---|
186 | public String getOptions() { |
---|
187 | return options; |
---|
188 | } |
---|
189 | |
---|
190 | public void setOptions(String options) { |
---|
191 | this.options = options; |
---|
192 | } |
---|
193 | |
---|
194 | public String getActionContentType() { |
---|
195 | if (format.toLowerCase().startsWith("html")) { |
---|
196 | this.actionContentType = "text/html"; |
---|
197 | } else { |
---|
198 | this.actionContentType = "text/xml"; |
---|
199 | } |
---|
200 | |
---|
201 | return actionContentType; |
---|
202 | } |
---|
203 | |
---|
204 | public void setActionContentType(String actionContentType) { |
---|
205 | this.actionContentType = actionContentType; |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | public void setRepository(String repository) { |
---|
210 | this.repository = repository; |
---|
211 | } |
---|
212 | |
---|
213 | public String getRepository() { |
---|
214 | //default repository = 1 (set on init) |
---|
215 | return repository; |
---|
216 | } |
---|
217 | public String getActionkey() { |
---|
218 | return actionkey; |
---|
219 | } |
---|
220 | |
---|
221 | public void setActionkey(String actionKey) { |
---|
222 | actionkey = actionKey; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | public String getStartItem() { |
---|
227 | return startItem; |
---|
228 | } |
---|
229 | |
---|
230 | public void setStartItem(String startItem) { |
---|
231 | this.startItem = startItem; |
---|
232 | } |
---|
233 | |
---|
234 | public String getMaximumItems() { |
---|
235 | return maximumItems; |
---|
236 | } |
---|
237 | |
---|
238 | public void setMaximumItems(String maximumItems) { |
---|
239 | this.maximumItems = maximumItems; |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | public String getSort() { |
---|
244 | return sort; |
---|
245 | } |
---|
246 | |
---|
247 | public void setSort(String sort) { |
---|
248 | this.sort = sort; |
---|
249 | } |
---|
250 | |
---|
251 | public String getLang() { |
---|
252 | return lang; |
---|
253 | } |
---|
254 | |
---|
255 | public void setLang(String lang) { |
---|
256 | this.lang = lang; |
---|
257 | } |
---|
258 | |
---|
259 | public String getFullFormat() { |
---|
260 | return actionkey + "2" + format; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * primitive identification of the target-proxy |
---|
265 | * base for finding the right base_url in the props |
---|
266 | * subclass has to override with its specific proxykey |
---|
267 | * @return |
---|
268 | */ |
---|
269 | public String getProxyKey() { |
---|
270 | return ""; |
---|
271 | } |
---|
272 | |
---|
273 | public String getUserMsg() { |
---|
274 | return userMsg; |
---|
275 | } |
---|
276 | |
---|
277 | public void setUserMsg(String userMsg) { |
---|
278 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
279 | } |
---|
280 | |
---|
281 | @Override |
---|
282 | public void setServletRequest(HttpServletRequest arg0) { |
---|
283 | request = arg0; |
---|
284 | } |
---|
285 | |
---|
286 | public HttpServletRequest getServletRequest() { |
---|
287 | return request; |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | private InputStream resultStream; |
---|
292 | private InputStream sourceStream; |
---|
293 | |
---|
294 | /** |
---|
295 | * The stream holding the resulting data to be sent back to the user as response |
---|
296 | * @return |
---|
297 | */ |
---|
298 | public InputStream getResultStream() { |
---|
299 | return resultStream; |
---|
300 | } |
---|
301 | public void setResultStream(InputStream _resultStream){ |
---|
302 | resultStream = _resultStream; |
---|
303 | } |
---|
304 | public void setSourceStream(InputStream _sourceStream){ |
---|
305 | sourceStream = _sourceStream; |
---|
306 | } |
---|
307 | |
---|
308 | /* |
---|
309 | public InputStream getSourceStream() throws IOException { |
---|
310 | return getTargetRequest().openStream(); |
---|
311 | } |
---|
312 | */ |
---|
313 | |
---|
314 | public JSONArray getRepositories(){ |
---|
315 | return repositories; |
---|
316 | } |
---|
317 | |
---|
318 | public void setRepositories(JSONArray rep){ |
---|
319 | GenericProxyAction.repositories = rep; |
---|
320 | } |
---|
321 | |
---|
322 | protected URL base_url ; |
---|
323 | |
---|
324 | public URL getBaseURL() throws MalformedURLException { |
---|
325 | if (base_url == null) { |
---|
326 | base_url = new URL(getBaseURI());//Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
327 | } |
---|
328 | return base_url; |
---|
329 | } |
---|
330 | |
---|
331 | public String getBaseURI() { |
---|
332 | String uri =Admin.getConfig().getProperty(getProxyKey() + ".uri"); |
---|
333 | return uri; |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | public URL getTargetRequest() throws IOException { |
---|
338 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
339 | URL targetURL = getBaseURL(); |
---|
340 | |
---|
341 | return targetURL; |
---|
342 | } |
---|
343 | |
---|
344 | /* |
---|
345 | public String getRequestKey() { |
---|
346 | String key=""; |
---|
347 | if (getActionkey()!=null) { |
---|
348 | key += getActionkey() + "//-" ; |
---|
349 | } |
---|
350 | if (getQ()!=null) { |
---|
351 | key += getQ() + "//-" ; |
---|
352 | } |
---|
353 | if (getCollection()!=null) { |
---|
354 | key += getCollection(); |
---|
355 | } |
---|
356 | |
---|
357 | return key; |
---|
358 | } |
---|
359 | */ |
---|
360 | |
---|
361 | public String getRequestKey() { |
---|
362 | String key=""; |
---|
363 | if (getActionkey()!=null) { |
---|
364 | key += getActionkey() + "//-" ; |
---|
365 | }else { |
---|
366 | key +="//-" ; |
---|
367 | } |
---|
368 | if (getQ()!=null) { |
---|
369 | key += getQ() + "//-" ; |
---|
370 | } else { |
---|
371 | key +="//-" ; |
---|
372 | } |
---|
373 | if (getCollection()!=null) { |
---|
374 | key += getCollection() + "//-"; |
---|
375 | } else { |
---|
376 | key +="//-" ; |
---|
377 | } |
---|
378 | if (getSquery()!=null) { |
---|
379 | key += getSquery() + "//-" ; |
---|
380 | } else { |
---|
381 | key +="//-" ; |
---|
382 | } |
---|
383 | if (getStartItem()!=null) { |
---|
384 | key += getStartItem() + "//-"; |
---|
385 | } |
---|
386 | else{ |
---|
387 | key +="//-" ; |
---|
388 | } |
---|
389 | if (getMaximumItems()!=null) { |
---|
390 | key += getMaximumItems() + "//-"; |
---|
391 | } |
---|
392 | else{ |
---|
393 | key +="//-" ; |
---|
394 | } |
---|
395 | |
---|
396 | key += getRepository() + "//-"; |
---|
397 | key += getMaxdepth() + "//-"; |
---|
398 | |
---|
399 | if (getLang()!=null) { |
---|
400 | key += getLang() + "//-"; |
---|
401 | }else{ |
---|
402 | key +="//-" ; |
---|
403 | } |
---|
404 | |
---|
405 | |
---|
406 | return key; |
---|
407 | } |
---|
408 | public InputStream getSourceStream() throws IOException { |
---|
409 | return getTargetRequest().openStream(); |
---|
410 | // Admin.notifyUser(getProxyKey() + ".getSourceStream() - unable to open stream: " + getTargetRequest(); |
---|
411 | } |
---|
412 | |
---|
413 | public static String convertStreamToString(InputStream is) { |
---|
414 | |
---|
415 | BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
---|
416 | StringBuilder sb = new StringBuilder(); |
---|
417 | String line = null; |
---|
418 | |
---|
419 | try { |
---|
420 | while ((line = reader.readLine()) != null) { |
---|
421 | sb.append(line + "\n"); |
---|
422 | } |
---|
423 | } catch (IOException e) { |
---|
424 | // TODO Auto-generated catch block |
---|
425 | e.printStackTrace(); |
---|
426 | } finally { |
---|
427 | try { |
---|
428 | is.close(); |
---|
429 | } catch (IOException e) { |
---|
430 | // TODO Auto-generated catch block |
---|
431 | e.printStackTrace(); |
---|
432 | } |
---|
433 | try { |
---|
434 | is.reset(); |
---|
435 | } catch (IOException e) { |
---|
436 | // TODO Auto-generated catch block |
---|
437 | e.printStackTrace(); |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | return sb.toString(); |
---|
443 | } |
---|
444 | |
---|
445 | |
---|
446 | public static JSONArray createRepositories() throws IOException, InterruptedException, TransformerException{ |
---|
447 | String path = Admin.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.PROFILENAME_SERVER; |
---|
448 | File file=new File(path); |
---|
449 | InputStream in = new BufferedInputStream( new FileInputStream(path));; |
---|
450 | |
---|
451 | MDTransformer transformer = new MDTransformer(); |
---|
452 | transformer.setSrcFile(file.toURL()); |
---|
453 | transformer.setParams(MDTransformer.createParamsMap("xml2json")); |
---|
454 | InputStream jsonstream = transformer.transformXML(in); |
---|
455 | |
---|
456 | JSONObject json = JSONObject.fromObject(convertStreamToString(jsonstream)); |
---|
457 | JSONArray array = json.getJSONObject("WorkspaceProfile").getJSONArray("Repositories"); |
---|
458 | |
---|
459 | // repository as static variable |
---|
460 | return array; |
---|
461 | |
---|
462 | // repository as session attribute |
---|
463 | /* |
---|
464 | HttpSession session = getServletRequest().getSession(); |
---|
465 | //JSONArray param = (JSONArray) session.getAttribute("repositories"); |
---|
466 | JSONArray param = new JSONArray(); |
---|
467 | param.add(array); |
---|
468 | session.setAttribute("repositories", param); |
---|
469 | */ |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | public String getRepositoryPath(){ |
---|
474 | String repository_path = null; |
---|
475 | |
---|
476 | |
---|
477 | if (repositories == null ) { |
---|
478 | try { |
---|
479 | setRepositories(GenericProxyAction.createRepositories()); |
---|
480 | } catch (IOException e) { |
---|
481 | // TODO Auto-generated catch block |
---|
482 | e.printStackTrace(); |
---|
483 | } catch (InterruptedException e) { |
---|
484 | // TODO Auto-generated catch block |
---|
485 | e.printStackTrace(); |
---|
486 | } catch (TransformerException e) { |
---|
487 | // TODO Auto-generated catch block |
---|
488 | e.printStackTrace(); |
---|
489 | } |
---|
490 | } |
---|
491 | // static repositories |
---|
492 | net.sf.json.JSONArray json = getRepositories(); |
---|
493 | for(int i=0;i<json.size();i++){ |
---|
494 | if (json.getJSONObject(i).getString("name").equals(getRepository())){ |
---|
495 | repository_path = json.getJSONObject(i).getString("uri"); |
---|
496 | } |
---|
497 | } |
---|
498 | //Admin.notifyUser("REPOSITORY_PATH:" + repository_path); |
---|
499 | |
---|
500 | //session repositories |
---|
501 | /* |
---|
502 | HttpSession session = getServletRequest().getSession(); |
---|
503 | json = (net.sf.json.JSONArray) session.getAttribute("repositories"); |
---|
504 | for(int i=0;i<json.size();i++){ |
---|
505 | if (json.getJSONObject(i).getInt("id") == getRepository()){ |
---|
506 | repository_path = json.getJSONObject(i).getString("uri"); |
---|
507 | } |
---|
508 | } |
---|
509 | Admin.notifyUser("REPOSITORY_PATH-SESSIONATTR:" + repository_path); |
---|
510 | */ |
---|
511 | return repository_path; |
---|
512 | |
---|
513 | } |
---|
514 | |
---|
515 | public String addDurationKey(){ |
---|
516 | String req_key = getRequestKey(); |
---|
517 | |
---|
518 | Double duration_d; |
---|
519 | |
---|
520 | duration_d = (double)duration; |
---|
521 | duration_d = duration_d/1000.0; |
---|
522 | req_key += duration_d + "//-"; |
---|
523 | return req_key; |
---|
524 | } |
---|
525 | |
---|
526 | public HashMap<String,String> createTransformerParams(){ |
---|
527 | |
---|
528 | HashMap<String,String> hm = new HashMap<String,String>(); |
---|
529 | |
---|
530 | if (getFullFormat() != null){ |
---|
531 | hm.put("format", getFullFormat()); |
---|
532 | } |
---|
533 | if (getColumns() != null){ |
---|
534 | hm.put("cols", getColumns()); |
---|
535 | } |
---|
536 | if (getStartItem() != null){ |
---|
537 | hm.put("startItem", getStartItem()); |
---|
538 | } |
---|
539 | if (getMaximumItems() != null){ |
---|
540 | hm.put("maximumItems", getMaximumItems()); |
---|
541 | } |
---|
542 | if (getLang() != null){ |
---|
543 | hm.put("lang", getLang()); |
---|
544 | } |
---|
545 | if (getQ() != null){ |
---|
546 | hm.put("q", getQ()); |
---|
547 | } |
---|
548 | //if (getRepository() != null){ |
---|
549 | hm.put("repository_name", String.valueOf(getRepository())); |
---|
550 | //} |
---|
551 | |
---|
552 | return hm; |
---|
553 | |
---|
554 | } |
---|
555 | |
---|
556 | public void prepare() throws Exception { |
---|
557 | |
---|
558 | String req_key = getRequestKey(); |
---|
559 | Admin.notifyUser("request_key: " + req_key); |
---|
560 | Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); |
---|
561 | |
---|
562 | // Caching |
---|
563 | String xcid; |
---|
564 | |
---|
565 | // Admin.notifyUser("GPA.prepareSourceStream"); |
---|
566 | |
---|
567 | if (getCache().equals(Cache.SKIP)) { |
---|
568 | |
---|
569 | sourceStream = getSourceStream(); |
---|
570 | |
---|
571 | } else { |
---|
572 | if (getCache().equals(Cache.USE)) { |
---|
573 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
574 | } |
---|
575 | if (sourceStream == null) { // either not in cache or cache_flag=refresh |
---|
576 | //sourceStream = getTargetRequest().openStream(); |
---|
577 | long startMillis = System.currentTimeMillis(); |
---|
578 | sourceStream = getSourceStream(); |
---|
579 | duration = System.currentTimeMillis() - startMillis; |
---|
580 | req_key = addDurationKey(); |
---|
581 | |
---|
582 | long now = System.currentTimeMillis(); |
---|
583 | xcid = Cache.getCache().putInCache(req_key,sourceStream); |
---|
584 | //Admin.notifyUser("putting in cache: " + req_key); |
---|
585 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
586 | } /* else { |
---|
587 | Admin.notifyUser("reading from cache: " + req_key); |
---|
588 | } */ |
---|
589 | } |
---|
590 | |
---|
591 | if (format.equals("xml")) { |
---|
592 | resultStream = sourceStream; |
---|
593 | }else { |
---|
594 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
595 | MDTransformer transformer = new MDTransformer(); |
---|
596 | transformer.setSrcFile(getTargetRequest()); |
---|
597 | // getColumns |
---|
598 | // Admin.notifyUser("GPA.getRepository, before xsl:" + getRepository()); |
---|
599 | transformer.setParams(createTransformerParams()); |
---|
600 | resultStream = transformer.transformXML(sourceStream);//, getFullFormat(), getColumns(), getStartRecord(), getMaximumRecords(),getLang(),getQ(),String.valueOf(getRepository())); |
---|
601 | } |
---|
602 | |
---|
603 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | * default Action method |
---|
608 | */ |
---|
609 | public String execute() throws Exception { |
---|
610 | // HttpServletRequest request = ServletActionContext.getRequest(); |
---|
611 | |
---|
612 | |
---|
613 | //Admin.notifyUser("session-attrs:"); |
---|
614 | //Admin.notifyUser(getServletRequest().getRemoteUser() ); |
---|
615 | //Admin.notifyUser(String.valueOf(getSession())); |
---|
616 | |
---|
617 | long now = System.currentTimeMillis(); |
---|
618 | |
---|
619 | prepare(); |
---|
620 | long duration = (System.currentTimeMillis() - now); |
---|
621 | //duration |
---|
622 | |
---|
623 | if (resultStream == null) { |
---|
624 | return ERROR; |
---|
625 | } else { |
---|
626 | return SUCCESS; |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | } |
---|