1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.net.MalformedURLException; |
---|
6 | import java.net.URISyntaxException; |
---|
7 | import java.net.URL; |
---|
8 | |
---|
9 | import javax.servlet.http.HttpServletRequest; |
---|
10 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
11 | |
---|
12 | import com.opensymphony.xwork2.ActionSupport; |
---|
13 | |
---|
14 | /** |
---|
15 | * main Struts 2 controller |
---|
16 | * responds to requests (collections, model, recordset) |
---|
17 | * by dispatching the requests to appropriate internal methods and filling the inputStream with the result |
---|
18 | * |
---|
19 | * @author vronk |
---|
20 | * |
---|
21 | */ |
---|
22 | public class GenericProxyAction extends ActionSupport |
---|
23 | implements ServletRequestAware |
---|
24 | { |
---|
25 | |
---|
26 | private static final long serialVersionUID = 1L; |
---|
27 | |
---|
28 | /** |
---|
29 | * Properties to be filled by Struts with according request-parameters |
---|
30 | */ |
---|
31 | private String actionkey; |
---|
32 | private String q; |
---|
33 | private String squery; |
---|
34 | private String cache = Cache.USE; |
---|
35 | private String collection; |
---|
36 | private String columns; |
---|
37 | private String startRecord; |
---|
38 | private String maximumRecords; |
---|
39 | private String lang="en"; |
---|
40 | private int maxdepth; |
---|
41 | private String format; |
---|
42 | private String userMsg; |
---|
43 | private String repository; |
---|
44 | //private Map<String,Object> session; |
---|
45 | private HttpServletRequest request; |
---|
46 | |
---|
47 | public String getQ() { |
---|
48 | return q; |
---|
49 | } |
---|
50 | |
---|
51 | public void setQ(String pq) { |
---|
52 | if (pq == null) pq=""; |
---|
53 | /* |
---|
54 | if (q != null){ |
---|
55 | if (q.trim().length() == 0){ |
---|
56 | q = null; |
---|
57 | } |
---|
58 | } |
---|
59 | */ |
---|
60 | this.q = pq; |
---|
61 | } |
---|
62 | |
---|
63 | public String getSquery() { |
---|
64 | return squery; |
---|
65 | } |
---|
66 | |
---|
67 | public void setSquery(String psquery) { |
---|
68 | if (psquery==null) psquery=""; |
---|
69 | this.squery = psquery; |
---|
70 | } |
---|
71 | |
---|
72 | public String getCache() { |
---|
73 | return cache; |
---|
74 | } |
---|
75 | |
---|
76 | public void setCache(String cache) { |
---|
77 | this.cache = cache; |
---|
78 | } |
---|
79 | |
---|
80 | public String getCollection() { |
---|
81 | return collection; |
---|
82 | } |
---|
83 | |
---|
84 | public void setCollection(String collection) { |
---|
85 | this.collection = collection; |
---|
86 | } |
---|
87 | |
---|
88 | //TODO defaults |
---|
89 | public String getColumns() { |
---|
90 | String cols = columns; |
---|
91 | /*if (columns == null){ |
---|
92 | cols = "Id,Name,Title"; |
---|
93 | } |
---|
94 | */ |
---|
95 | return cols; |
---|
96 | } |
---|
97 | |
---|
98 | public void setColumns(String columns) { |
---|
99 | this.columns = columns; |
---|
100 | } |
---|
101 | |
---|
102 | public int getMaxdepth() { |
---|
103 | return maxdepth; |
---|
104 | } |
---|
105 | |
---|
106 | public void setMaxdepth(int maxdepth) { |
---|
107 | this.maxdepth = maxdepth; |
---|
108 | } |
---|
109 | |
---|
110 | public String getFormat() { |
---|
111 | return format; |
---|
112 | } |
---|
113 | |
---|
114 | public void setFormat(String format) { |
---|
115 | this.format = format; |
---|
116 | } |
---|
117 | |
---|
118 | public void setRepository(String repository) { |
---|
119 | this.repository = repository; |
---|
120 | } |
---|
121 | |
---|
122 | public String getRepository() { |
---|
123 | return repository; |
---|
124 | } |
---|
125 | public String getActionkey() { |
---|
126 | return actionkey; |
---|
127 | } |
---|
128 | |
---|
129 | public void setActionkey(String actionKey) { |
---|
130 | actionkey = actionKey; |
---|
131 | } |
---|
132 | |
---|
133 | public String getStartRecord() { |
---|
134 | return startRecord; |
---|
135 | } |
---|
136 | |
---|
137 | public void setStartRecord(String startRecord) { |
---|
138 | this.startRecord = startRecord; |
---|
139 | } |
---|
140 | |
---|
141 | public String getMaximumRecords() { |
---|
142 | return maximumRecords; |
---|
143 | } |
---|
144 | |
---|
145 | public void setMaximumRecords(String maximumRecords) { |
---|
146 | this.maximumRecords = maximumRecords; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | public String getLang() { |
---|
151 | return lang; |
---|
152 | } |
---|
153 | |
---|
154 | public void setLang(String lang) { |
---|
155 | this.lang = lang; |
---|
156 | } |
---|
157 | |
---|
158 | public String getFullFormat() { |
---|
159 | return actionkey + "2" + format; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * primitive identification of the target-proxy |
---|
164 | * base for finding the right base_url in the props |
---|
165 | * subclass has to override with its specific proxykey |
---|
166 | * @return |
---|
167 | */ |
---|
168 | public String getProxyKey() { |
---|
169 | return ""; |
---|
170 | } |
---|
171 | |
---|
172 | public String getUserMsg() { |
---|
173 | return userMsg; |
---|
174 | } |
---|
175 | |
---|
176 | public void setUserMsg(String userMsg) { |
---|
177 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
178 | } |
---|
179 | |
---|
180 | @Override |
---|
181 | public void setServletRequest(HttpServletRequest arg0) { |
---|
182 | request = arg0; |
---|
183 | } |
---|
184 | |
---|
185 | public HttpServletRequest getServletRequest() { |
---|
186 | return request; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | private InputStream resultStream; |
---|
191 | private InputStream sourceStream; |
---|
192 | |
---|
193 | /** |
---|
194 | * The stream holding the resulting data to be sent back to the user as response |
---|
195 | * @return |
---|
196 | */ |
---|
197 | public InputStream getResultStream() { |
---|
198 | return resultStream; |
---|
199 | } |
---|
200 | public void setResultStream(InputStream _resultStream){ |
---|
201 | resultStream = _resultStream; |
---|
202 | } |
---|
203 | public void setSourceStream(InputStream _sourceStream){ |
---|
204 | sourceStream = _sourceStream; |
---|
205 | } |
---|
206 | |
---|
207 | /* |
---|
208 | public InputStream getSourceStream() throws IOException { |
---|
209 | return getTargetRequest().openStream(); |
---|
210 | } |
---|
211 | */ |
---|
212 | |
---|
213 | protected URL base_url ; |
---|
214 | |
---|
215 | public URL getBaseURL() throws MalformedURLException { |
---|
216 | if (base_url == null) { |
---|
217 | base_url = new URL(getBaseURI());//Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
218 | } |
---|
219 | return base_url; |
---|
220 | } |
---|
221 | |
---|
222 | public String getBaseURI() { |
---|
223 | String uri =Admin.getConfig().getProperty(getProxyKey() + ".uri"); |
---|
224 | return uri; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | public URL getTargetRequest() throws IOException { |
---|
229 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
230 | URL targetURL = getBaseURL(); |
---|
231 | |
---|
232 | return targetURL; |
---|
233 | } |
---|
234 | |
---|
235 | /* |
---|
236 | public String getRequestKey() { |
---|
237 | String key=""; |
---|
238 | if (getActionkey()!=null) { |
---|
239 | key += getActionkey() + "//-" ; |
---|
240 | } |
---|
241 | if (getQ()!=null) { |
---|
242 | key += getQ() + "//-" ; |
---|
243 | } |
---|
244 | if (getCollection()!=null) { |
---|
245 | key += getCollection(); |
---|
246 | } |
---|
247 | |
---|
248 | return key; |
---|
249 | } |
---|
250 | */ |
---|
251 | |
---|
252 | public String getRequestKey() { |
---|
253 | String key=""; |
---|
254 | if (getActionkey()!=null) { |
---|
255 | key += getActionkey() + "//-" ; |
---|
256 | }else { |
---|
257 | key +="//-" ; |
---|
258 | } |
---|
259 | if (getQ()!=null) { |
---|
260 | key += getQ() + "//-" ; |
---|
261 | } else { |
---|
262 | key +="//-" ; |
---|
263 | } |
---|
264 | if (getCollection()!=null) { |
---|
265 | key += getCollection() + "//-"; |
---|
266 | } else { |
---|
267 | key +="//-" ; |
---|
268 | } |
---|
269 | if (getSquery()!=null) { |
---|
270 | key += getSquery() + "//-" ; |
---|
271 | } else { |
---|
272 | key +="//-" ; |
---|
273 | } |
---|
274 | if (getStartRecord()!=null) { |
---|
275 | key += getStartRecord() + "//-"; |
---|
276 | } |
---|
277 | else{ |
---|
278 | key +="//-" ; |
---|
279 | } |
---|
280 | if (getMaximumRecords()!=null) { |
---|
281 | key += getMaximumRecords() + "//-"; |
---|
282 | } |
---|
283 | else{ |
---|
284 | key +="//-" ; |
---|
285 | } |
---|
286 | if (getRepository()!=null) { |
---|
287 | key += getRepository() + "//-"; |
---|
288 | }else{ |
---|
289 | key +="//-" ; |
---|
290 | } |
---|
291 | |
---|
292 | key += getMaxdepth() + "//-"; |
---|
293 | |
---|
294 | if (getLang()!=null) { |
---|
295 | key += getLang(); |
---|
296 | } |
---|
297 | |
---|
298 | return key; |
---|
299 | } |
---|
300 | public InputStream getSourceStream() throws IOException { |
---|
301 | return getTargetRequest().openStream(); |
---|
302 | // Admin.notifyUser(getProxyKey() + ".getSourceStream() - unable to open stream: " + getTargetRequest(); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | public void prepare() throws Exception { |
---|
307 | |
---|
308 | String req_key = getRequestKey(); |
---|
309 | Admin.notifyUser("request_key: " + req_key); |
---|
310 | Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); |
---|
311 | |
---|
312 | // Caching |
---|
313 | String xcid; |
---|
314 | |
---|
315 | // Admin.notifyUser("GPA.prepareSourceStream"); |
---|
316 | |
---|
317 | if (getCache().equals(Cache.SKIP)) { |
---|
318 | |
---|
319 | sourceStream = getSourceStream(); |
---|
320 | |
---|
321 | } else { |
---|
322 | if (getCache().equals(Cache.USE)) { |
---|
323 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
324 | } |
---|
325 | if (sourceStream == null) { // either not in cache or cache_flag=refresh |
---|
326 | //sourceStream = getTargetRequest().openStream(); |
---|
327 | sourceStream = getSourceStream(); |
---|
328 | xcid = Cache.getCache().putInCache(req_key,sourceStream); |
---|
329 | Admin.notifyUser("putting in cache: " + req_key); |
---|
330 | sourceStream = Cache.getCache().getFromCache(req_key); |
---|
331 | } else { |
---|
332 | Admin.notifyUser("reading from cache: " + req_key); |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | if (format.equals("xml")) { |
---|
337 | resultStream = sourceStream; |
---|
338 | }else { |
---|
339 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
340 | MDTransformer.getMDTransformer().setSrcFile(getTargetRequest()); |
---|
341 | // getColumns |
---|
342 | resultStream = MDTransformer.getMDTransformer().transformXML(sourceStream, getFullFormat(), getColumns(), getStartRecord(), getMaximumRecords(),getLang(),getQ(),getRepository()); |
---|
343 | } |
---|
344 | |
---|
345 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * default Action method |
---|
350 | */ |
---|
351 | public String execute() throws Exception { |
---|
352 | // HttpServletRequest request = ServletActionContext.getRequest(); |
---|
353 | |
---|
354 | |
---|
355 | Admin.notifyUser("session-attrs:"); |
---|
356 | Admin.notifyUser(getServletRequest().getRemoteUser() ); |
---|
357 | //Admin.notifyUser(String.valueOf(getSession())); |
---|
358 | |
---|
359 | prepare(); |
---|
360 | if (resultStream == null) { |
---|
361 | return ERROR; |
---|
362 | } else { |
---|
363 | return SUCCESS; |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | } |
---|