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 | //import net.sf.saxon.Controller; |
---|
24 | //import net.sf.saxon.event.MessageEmitter; |
---|
25 | |
---|
26 | |
---|
27 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
28 | import com.opensymphony.xwork2.ActionSupport; |
---|
29 | import eu.clarin.cmdi.mdservice.model.Diagnostics; |
---|
30 | import eu.clarin.cmdi.mdservice.model.WorkspaceProfile; |
---|
31 | |
---|
32 | /** |
---|
33 | * main Struts2 controller (ie implemenets the struts' execute()-method), all other Actions are derived from. |
---|
34 | * Defines the methods used for retrieving the data |
---|
35 | * and provides basic implementation where possible. |
---|
36 | * |
---|
37 | * @author vronk |
---|
38 | */ |
---|
39 | public class GenericProxyAction extends ActionSupport |
---|
40 | implements ServletRequestAware |
---|
41 | { |
---|
42 | |
---|
43 | private static final long serialVersionUID = 1L; |
---|
44 | |
---|
45 | |
---|
46 | private Diagnostics diagnostics; |
---|
47 | /** |
---|
48 | * Properties to be filled by Struts with according request-parameters |
---|
49 | */ |
---|
50 | private String actionkey; |
---|
51 | private String q; |
---|
52 | private String squery; |
---|
53 | private String cache = Cache.USE; |
---|
54 | private String collection; |
---|
55 | private String columns; |
---|
56 | private String startItem; |
---|
57 | private String maximumItems; |
---|
58 | private String sort; |
---|
59 | private String lang="en"; |
---|
60 | private int maxdepth; |
---|
61 | private String format; |
---|
62 | private String options; |
---|
63 | private String actionContentType; |
---|
64 | private String userMsg; |
---|
65 | private String repository; |
---|
66 | //private Map<String,Object> session; |
---|
67 | private HttpServletRequest request; |
---|
68 | private long duration = 0; |
---|
69 | private InputStream resultStream; |
---|
70 | private InputStream sourceStream; |
---|
71 | protected URL base_url ; |
---|
72 | |
---|
73 | public GenericProxyAction(){ |
---|
74 | super(); |
---|
75 | initialize(); |
---|
76 | } |
---|
77 | |
---|
78 | public void setRepositoryByIndex(int id){ |
---|
79 | |
---|
80 | net.sf.json.JSONArray json = WorkspaceProfile.getRepositories(); |
---|
81 | if (json.size() > 0){ |
---|
82 | repository = json.getJSONObject(id).getString("name"); |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | protected void initialize(){ |
---|
88 | //repository = 1; |
---|
89 | setRepositoryByIndex(0); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | public Diagnostics getDiagnostics(){ |
---|
94 | return this.diagnostics; |
---|
95 | } |
---|
96 | |
---|
97 | public void setDiagnostics(Diagnostics diagnostics){ |
---|
98 | this.diagnostics = diagnostics; |
---|
99 | } |
---|
100 | public Diagnostics Diagnostics(){ |
---|
101 | if (diagnostics == null){ |
---|
102 | diagnostics = new Diagnostics(); |
---|
103 | } |
---|
104 | return this.diagnostics; |
---|
105 | } |
---|
106 | public String getQ() { |
---|
107 | return q; |
---|
108 | } |
---|
109 | |
---|
110 | public void setQ(String pq) { |
---|
111 | if (pq == null) pq=""; |
---|
112 | /* |
---|
113 | if (q != null){ |
---|
114 | if (q.trim().length() == 0){ |
---|
115 | q = null; |
---|
116 | } |
---|
117 | } |
---|
118 | */ |
---|
119 | this.q = pq; |
---|
120 | } |
---|
121 | |
---|
122 | public String getSquery() { |
---|
123 | return squery; |
---|
124 | } |
---|
125 | |
---|
126 | public void setSquery(String psquery) { |
---|
127 | if (psquery==null) psquery=""; |
---|
128 | this.squery = psquery; |
---|
129 | } |
---|
130 | |
---|
131 | public String getCache() { |
---|
132 | return cache; |
---|
133 | } |
---|
134 | |
---|
135 | public void setCache(String cache) { |
---|
136 | this.cache = cache; |
---|
137 | } |
---|
138 | |
---|
139 | public String getCollection() { |
---|
140 | return collection; |
---|
141 | } |
---|
142 | |
---|
143 | public void setCollection(String collection) { |
---|
144 | this.collection = collection; |
---|
145 | } |
---|
146 | |
---|
147 | //TODO defaults |
---|
148 | public String getColumns() { |
---|
149 | String cols = columns; |
---|
150 | /*if (columns == null){ |
---|
151 | cols = "Id,Name,Title"; |
---|
152 | } |
---|
153 | */ |
---|
154 | return cols; |
---|
155 | } |
---|
156 | |
---|
157 | public void setColumns(String columns) { |
---|
158 | this.columns = columns; |
---|
159 | } |
---|
160 | |
---|
161 | public int getMaxdepth() { |
---|
162 | return maxdepth; |
---|
163 | } |
---|
164 | |
---|
165 | public void setMaxdepth(int maxdepth) { |
---|
166 | this.maxdepth = maxdepth; |
---|
167 | } |
---|
168 | |
---|
169 | public String getFormat() { |
---|
170 | return format; |
---|
171 | } |
---|
172 | |
---|
173 | public void setFormat(String format) { |
---|
174 | this.format = format; |
---|
175 | } |
---|
176 | |
---|
177 | public String getOptions() { |
---|
178 | return options; |
---|
179 | } |
---|
180 | |
---|
181 | public void setOptions(String options) { |
---|
182 | this.options = options; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * is used in struts.xml to dynamically set the mime-type of the result, depending on the format-parameter |
---|
187 | * @return |
---|
188 | */ |
---|
189 | public String getActionContentType() { |
---|
190 | if (format.toLowerCase().startsWith("html")) { |
---|
191 | this.actionContentType = "text/html"; |
---|
192 | //this.actionContentType = "application/xhtml+xml"; |
---|
193 | } else { |
---|
194 | this.actionContentType = "text/xml"; |
---|
195 | } |
---|
196 | |
---|
197 | return actionContentType; |
---|
198 | } |
---|
199 | |
---|
200 | public void setActionContentType(String actionContentType) { |
---|
201 | this.actionContentType = actionContentType; |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | public void setRepository(String repository) { |
---|
206 | this.repository = repository; |
---|
207 | } |
---|
208 | |
---|
209 | public String getRepository() { |
---|
210 | //default repository = 1 (set on init) |
---|
211 | return repository; |
---|
212 | } |
---|
213 | public String getActionkey() { |
---|
214 | return actionkey; |
---|
215 | } |
---|
216 | |
---|
217 | public void setActionkey(String actionKey) { |
---|
218 | actionkey = actionKey; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | public String getStartItem() { |
---|
223 | return startItem; |
---|
224 | } |
---|
225 | |
---|
226 | public void setStartItem(String startItem) { |
---|
227 | this.startItem = startItem; |
---|
228 | } |
---|
229 | |
---|
230 | public String getMaximumItems() { |
---|
231 | return maximumItems; |
---|
232 | } |
---|
233 | |
---|
234 | public void setMaximumItems(String maximumItems) { |
---|
235 | this.maximumItems = maximumItems; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | public String getSort() { |
---|
240 | return sort; |
---|
241 | } |
---|
242 | |
---|
243 | public void setSort(String sort) { |
---|
244 | this.sort = sort; |
---|
245 | } |
---|
246 | |
---|
247 | public String getLang() { |
---|
248 | return lang; |
---|
249 | } |
---|
250 | |
---|
251 | public void setLang(String lang) { |
---|
252 | this.lang = lang; |
---|
253 | } |
---|
254 | |
---|
255 | public String getFullFormat() { |
---|
256 | return actionkey + "2" + format; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * internal identification of the target-proxy |
---|
261 | * base for finding the right base_url in the properties |
---|
262 | * subclass has to override with its specific proxykey |
---|
263 | * @return the key identifying this type of proxy |
---|
264 | */ |
---|
265 | public String getProxyKey() { |
---|
266 | return ""; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * TODO: check what this does, where it is used? |
---|
271 | * @return |
---|
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 | /** |
---|
282 | * TODO: check what this does, where it is used? |
---|
283 | * @return |
---|
284 | */ |
---|
285 | @Override |
---|
286 | public void setServletRequest(HttpServletRequest arg0) { |
---|
287 | request = arg0; |
---|
288 | } |
---|
289 | |
---|
290 | public HttpServletRequest getServletRequest() { |
---|
291 | return request; |
---|
292 | } |
---|
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 | |
---|
315 | /** |
---|
316 | * Reads URI from the configuration (mdservice.properties) based on the proxy-key |
---|
317 | * @return the base-URI of the target repository or registry |
---|
318 | */ |
---|
319 | public String getBaseURI() { |
---|
320 | String uri =Admin.getConfig().getProperty(getProxyKey() + ".uri"); |
---|
321 | return uri; |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * Provides the base-URL specific to given target repository or registry |
---|
326 | * @return |
---|
327 | * @throws MalformedURLException |
---|
328 | */ |
---|
329 | public URL getBaseURL() throws MalformedURLException { |
---|
330 | if (base_url == null) { |
---|
331 | base_url = new URL(getBaseURI());//Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
332 | } |
---|
333 | return base_url; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * Constructs the actual URL to be send to the target repository/registry |
---|
338 | * It has to be overridden by the subclasses |
---|
339 | * as every target service has a different request-pattern. |
---|
340 | * @return URL of the target request |
---|
341 | * @throws IOException |
---|
342 | */ |
---|
343 | public URL getTargetRequest() throws IOException { |
---|
344 | URL targetURL = getBaseURL(); |
---|
345 | return targetURL; |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * Constructs an unambiguous key for the request (encoding all the parameters). |
---|
350 | * This is used as identifier for caching |
---|
351 | * @return key unambiguously encoding the request |
---|
352 | */ |
---|
353 | |
---|
354 | public String getRequestKey() { |
---|
355 | String key=""; |
---|
356 | if (getActionkey()!=null) { |
---|
357 | key += getActionkey() + "//-" ; |
---|
358 | }else { |
---|
359 | key +="//-" ; |
---|
360 | } |
---|
361 | if (getQ()!=null) { |
---|
362 | key += getQ() + "//-" ; |
---|
363 | } else { |
---|
364 | key +="//-" ; |
---|
365 | } |
---|
366 | if (getCollection()!=null) { |
---|
367 | key += getCollection() + "//-"; |
---|
368 | } else { |
---|
369 | key +="//-" ; |
---|
370 | } |
---|
371 | if (getSquery()!=null) { |
---|
372 | key += getSquery() + "//-" ; |
---|
373 | } else { |
---|
374 | key +="//-" ; |
---|
375 | } |
---|
376 | if (getStartItem()!=null) { |
---|
377 | key += getStartItem() + "//-"; |
---|
378 | } |
---|
379 | else{ |
---|
380 | key +="//-" ; |
---|
381 | } |
---|
382 | if (getMaximumItems()!=null) { |
---|
383 | key += getMaximumItems() + "//-"; |
---|
384 | } |
---|
385 | else{ |
---|
386 | key +="//-" ; |
---|
387 | } |
---|
388 | |
---|
389 | key += getRepository() + "//-"; |
---|
390 | key += getMaxdepth() + "//-"; |
---|
391 | |
---|
392 | if (getLang()!=null) { |
---|
393 | key += getLang() + "//-"; |
---|
394 | }else{ |
---|
395 | key +="//-" ; |
---|
396 | } |
---|
397 | |
---|
398 | return key; |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | public InputStream getSourceStream() throws IOException, NoStylesheetException { |
---|
403 | return getTargetRequest().openStream(); |
---|
404 | |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | |
---|
409 | public String addDurationKey(){ |
---|
410 | String req_key = getRequestKey(); |
---|
411 | |
---|
412 | Double duration_d; |
---|
413 | |
---|
414 | duration_d = (double)duration; |
---|
415 | duration_d = duration_d/1000.0; |
---|
416 | req_key += duration_d + "//-"; |
---|
417 | return req_key; |
---|
418 | } |
---|
419 | |
---|
420 | public HashMap<String,String> createTransformerParams(){ |
---|
421 | |
---|
422 | HashMap<String,String> hm = new HashMap<String,String>(); |
---|
423 | |
---|
424 | if (getFullFormat() != null){ |
---|
425 | hm.put("format", getFullFormat()); |
---|
426 | } |
---|
427 | if (getColumns() != null){ |
---|
428 | hm.put("cols", getColumns()); |
---|
429 | } else { |
---|
430 | hm.put("cols", ""); |
---|
431 | } |
---|
432 | if (getStartItem() != null){ |
---|
433 | hm.put("startItem", getStartItem()); |
---|
434 | } |
---|
435 | if (getMaximumItems() != null){ |
---|
436 | hm.put("maximumItems", getMaximumItems()); |
---|
437 | } |
---|
438 | if (getLang() != null){ |
---|
439 | hm.put("lang", getLang()); |
---|
440 | } |
---|
441 | if (getQ() != null){ |
---|
442 | hm.put("q", getQ()); |
---|
443 | } |
---|
444 | //if (getRepository() != null){ |
---|
445 | hm.put("repository_name", String.valueOf(getRepository())); |
---|
446 | hm.put("repository_type", WorkspaceProfile.getRepositoryType(this.getRepository())); |
---|
447 | //} |
---|
448 | |
---|
449 | return hm; |
---|
450 | |
---|
451 | } |
---|
452 | |
---|
453 | /** |
---|
454 | * This is the work-horse function. |
---|
455 | * It does two things: |
---|
456 | * a) check if the source data is already in cache (based on cache-key that is constructed from the request parameters) |
---|
457 | * b) if format-parameter provided, invokes the transformation. |
---|
458 | * at the end the data to be returned as result is contained in the resultStream |
---|
459 | * If format is xml, then the data from sourceStream is passed further as resultStream. |
---|
460 | * |
---|
461 | * @throws Exception This is handled by struts (is mapped to some result in struts.xml based on the Exception-Class) |
---|
462 | */ |
---|
463 | |
---|
464 | public void prepare() throws Exception { |
---|
465 | |
---|
466 | String req_key = getRequestKey(); |
---|
467 | Admin.notifyUser("request_key: " + req_key); |
---|
468 | Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); |
---|
469 | |
---|
470 | // Caching |
---|
471 | String xcid; |
---|
472 | |
---|
473 | // Admin.notifyUser("GPA.prepareSourceStream"); |
---|
474 | |
---|
475 | if (getCache().equals(Cache.SKIP)) { |
---|
476 | |
---|
477 | sourceStream = getSourceStream(); |
---|
478 | |
---|
479 | } else { |
---|
480 | if (getCache().equals(Cache.USE)) { |
---|
481 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
482 | } |
---|
483 | if (sourceStream == null) { // either not in cache or cache_flag=refresh |
---|
484 | //sourceStream = getTargetRequest().openStream(); |
---|
485 | long startMillis = System.currentTimeMillis(); |
---|
486 | sourceStream = getSourceStream(); |
---|
487 | duration = System.currentTimeMillis() - startMillis; |
---|
488 | req_key = addDurationKey(); |
---|
489 | |
---|
490 | long now = System.currentTimeMillis(); |
---|
491 | xcid = Cache.getCache().putInCache(req_key,sourceStream); |
---|
492 | //Admin.notifyUser("putting in cache: " + req_key); |
---|
493 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
494 | } /* else { |
---|
495 | Admin.notifyUser("reading from cache: " + req_key); |
---|
496 | } */ |
---|
497 | } |
---|
498 | |
---|
499 | if (format.equals("xml")) { |
---|
500 | resultStream = sourceStream; |
---|
501 | }else { |
---|
502 | MDTransformer transformer = new MDTransformer(); |
---|
503 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
504 | // TODO: WHY?? |
---|
505 | transformer.setSrcFile(getTargetRequest()); |
---|
506 | transformer.setParams(createTransformerParams()); |
---|
507 | // here the transformation is invoked |
---|
508 | resultStream = transformer.transformXML(sourceStream); |
---|
509 | } |
---|
510 | |
---|
511 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * default Action method that gets called by Struts. Everything interesting happens in prepare() |
---|
516 | */ |
---|
517 | public String execute() throws Exception { |
---|
518 | // HttpServletRequest request = ServletActionContext.getRequest(); |
---|
519 | |
---|
520 | |
---|
521 | //Admin.notifyUser("session-attrs:"); |
---|
522 | //Admin.notifyUser(getServletRequest().getRemoteUser() ); |
---|
523 | //Admin.notifyUser(String.valueOf(getSession())); |
---|
524 | |
---|
525 | long now = System.currentTimeMillis(); |
---|
526 | |
---|
527 | prepare(); |
---|
528 | long duration = (System.currentTimeMillis() - now); |
---|
529 | //duration |
---|
530 | |
---|
531 | if (resultStream == null) { |
---|
532 | return ERROR; |
---|
533 | } else { |
---|
534 | return SUCCESS; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | |
---|
539 | } |
---|