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