source: MDService2/branches/MDService_simple2/src/eu/clarin/cmdi/mdservice/action/GenericAction.java @ 1511

Last change on this file since 1511 was 1511, checked in by vronk, 13 years ago

brutal refactoring,
added new packages: proxies, internal,
very much restructured Action hierarchy.
Introduced division Action/Proxy?.
This is a first compiling version.
it's total mess.

File size: 9.3 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.util.HashMap;
6
7import javax.servlet.http.HttpServletRequest;
8
9import org.apache.log4j.Logger;
10import org.apache.struts2.interceptor.ServletRequestAware;
11
12import com.opensymphony.xwork2.ActionSupport;
13
14import eu.clarin.cmdi.mdservice.internal.Cache;
15import eu.clarin.cmdi.mdservice.internal.MDTransformer;
16import eu.clarin.cmdi.mdservice.internal.NoStylesheetException;
17import eu.clarin.cmdi.mdservice.model.Diagnostics;
18import eu.clarin.cmdi.mdservice.proxy.GenericProxy;
19
20
21
22/**
23 * main Struts2 controller (ie implemenets the struts' execute()-method), all other Actions are derived from.   
24 * Defines the methods used for retrieving the data
25 * and provides basic implementation where possible.
26 * Also responsible for caching and transformation of the result.
27 * 
28 * @author vronk
29 */
30public class GenericAction extends ActionSupport
31implements ServletRequestAware
32{
33        public static Logger log = Logger.getLogger("GenericAction");
34        private static final long serialVersionUID = 1L;
35       
36       
37        private Diagnostics diagnostics;
38        /**
39         * Properties to be filled by Struts with according request-parameters
40         */     
41        private String actionkey = "generic";
42        private String cache = Cache.USE;
43        private String lang="en";
44        private String format="xml"; // default no transformation
45        private String actionContentType;
46        private String userMsg;
47        private HttpServletRequest request; 
48        private long duration = 0;
49        private String repository;
50        private InputStream resultStream;
51        private InputStream sourceStream;
52       
53//      protected URL base_url ;
54       
55        private GenericProxy target_proxy;
56               
57               
58         public GenericAction(){
59                 super();
60        //       initialize();
61         }
62       
63        public GenericProxy getTargetProxy() {
64                return target_proxy;
65        }
66
67        public void setTargetProxy(GenericProxy proxy) {
68                target_proxy = proxy;
69        }
70
71         public Diagnostics getDiagnostics(){
72                 return this.diagnostics;
73         }
74         
75         public void setDiagnostics(Diagnostics diagnostics){
76                 this.diagnostics = diagnostics;
77         }
78         public Diagnostics Diagnostics(){
79                 if (diagnostics == null){
80                         diagnostics  = new Diagnostics();
81                 }
82                 return this.diagnostics;
83         }
84
85         public String getCache() {
86                return cache;
87        }
88
89        public void setCache(String cache) {
90                this.cache = cache;
91        }
92
93        public String getFormat() {
94                return format;
95        }
96
97        public void setFormat(String format) {
98                this.format = format;
99        }
100
101        public void setRepository(String repository) {
102                this.repository = repository;
103        }
104       
105        public String getRepository() {
106                //default repository = 1 (set on init)
107                return repository;
108        }
109       
110        /**
111         * is used in struts.xml to dynamically set the mime-type of the result, depending on the format-parameter
112         * @return
113         */
114        public String getActionContentType() {
115                if (format.toLowerCase().startsWith("html")) {
116                        this.actionContentType = "text/html";           
117                        //this.actionContentType = "application/xhtml+xml";
118                } else {
119                        this.actionContentType = "text/xml";
120                }
121               
122                return actionContentType;
123        }
124
125        public void setActionContentType(String actionContentType) {
126                this.actionContentType = actionContentType;
127        }
128       
129        public String getActionkey() {
130                return actionkey;
131        }
132
133        public void setActionkey(String actionKey) {
134                actionkey = actionKey;
135        }
136
137        public String getLang() {
138                return lang;
139        }
140
141        public void setLang(String lang) {
142                this.lang = lang;
143        }
144
145        public String getFullFormat() {         
146                return actionkey + "2" + format;
147        }
148
149
150        /**
151         * TODO: this should be replaced by the diagnostics-mechanism
152         * @return
153         */
154        public String getUserMsg() {
155                return userMsg;
156        }
157
158        public void setUserMsg(String userMsg) {
159                this.userMsg = this.userMsg + "\n" + userMsg;
160        }
161       
162        /**
163         * TODO: check what this does, where it is used?
164         * @return
165         */
166        @Override
167        public void setServletRequest(HttpServletRequest arg0) {
168                request = arg0;
169        }
170
171        public HttpServletRequest getServletRequest() {
172                return request;
173        }
174
175        /**
176         * The stream holding the resulting data to be sent back to the user as response
177         * @return
178         */
179        public InputStream getResultStream() {
180                return resultStream;
181    }
182        public void setResultStream(InputStream _resultStream){
183                resultStream = _resultStream;
184        }
185        public void setSourceStream(InputStream _sourceStream){
186                sourceStream = _sourceStream;
187        }
188       
189        /*
190        public InputStream getSourceStream() throws IOException {
191                return getTargetRequest().openStream();
192    }
193*/
194       
195        /**
196         * Constructs an unambiguous key for the request (encoding all the parameters).
197         * This is used as identifier for caching
198         * @return key unambiguously encoding the request
199         */
200       
201         public String getRequestKey() {
202                        String key="";
203                        if (getActionkey()!=null) {
204                                key += getActionkey() + "//-" ;
205                        }else {
206                                key +="//-" ;
207                        }
208                        /*if (getQ()!=null) {
209                                key += getQ() + "//-" ;
210                        } else {
211                                key +="//-" ;
212                        }
213                        if (getCollection()!=null) {
214                                key += getCollection() + "//-";
215                        } else {
216                                key +="//-" ;
217                        }
218                        if (getSquery()!=null) {
219                                key += getSquery() + "//-" ;
220                        } else {
221                                key +="//-" ;
222                        }
223                        if (getStartItem()!=null) {
224                                key += getStartItem() + "//-";
225                        }
226                                else{
227                                        key +="//-" ;
228                        }
229                        if (getMaximumItems()!=null) {
230                                key += getMaximumItems() + "//-";
231                        }
232                        else{
233                                key +="//-" ;
234                        }
235                       
236                        key += getRepository()  + "//-";
237                        key += getMaxdepth()  + "//-";
238                       
239                        if (getLang()!=null) {
240                                key += getLang() + "//-";
241                        }else{
242                                key +="//-" ;
243                        }                       
244                         */
245                return key;
246        }
247         
248/**
249 * Get the sourceStream from the target proxy   
250 * @return
251 * @throws IOException
252 * @throws NoStylesheetException
253 */
254        public InputStream getSourceStream() throws IOException, NoStylesheetException {               
255                return getTargetProxy().getSourceStream(); 
256        }
257       
258        public String addDurationKey(){
259                String req_key = getRequestKey();
260       
261                Double duration_d;
262
263            duration_d = (double)duration; 
264            duration_d = duration_d/1000.0;
265                req_key += duration_d + "//-";
266                return req_key;
267        }
268       
269        public HashMap<String,String> createTransformerParams(){
270               
271                HashMap<String,String> hm = new HashMap<String,String>();
272               
273            if (getFullFormat() != null){
274                        hm.put("format", getFullFormat());
275            }
276            /*
277                if (getColumns() != null){
278                        hm.put("cols", getColumns());
279                } else {
280                        hm.put("cols", "");
281                }
282                if (getStartItem() != null){
283                        hm.put("startItem", getStartItem());
284                }
285                if (getMaximumItems() != null){
286                        hm.put("maximumItems", getMaximumItems());     
287                }
288                if (getLang() != null){
289                        hm.put("lang", getLang());
290                }
291                if (getQ() != null){
292                        hm.put("q", getQ());
293                }
294                //if (getRepository() != null){
295                hm.put("repository_name", String.valueOf(getRepository()));
296                hm.put("repository_type", WorkspaceProfile.getRepositoryType(this.getRepository()));
297                //}
298        */
299                return hm;
300               
301        }
302       
303        /**
304         * This is the work-horse function.
305         * It does two things:
306         * a) check if the source data is already in cache (based on cache-key that is constructed from the request parameters)
307         * b) if format-parameter provided, invokes the transformation.
308         * at the end the data to be returned as result is contained in the resultStream
309         * If format is xml, then the data from sourceStream is passed further as resultStream.
310         *   
311         * @throws Exception  This is handled by struts (is mapped to some result in struts.xml based on the Exception-Class)
312         */
313       
314        public void prepare() throws Exception {               
315               
316                String req_key = getRequestKey();
317                log.debug("request_key: " +  req_key); 
318                log.debug(getActionkey() + ".targetURL: " + getTargetProxy().getTargetRequest() + " .format:" + getFullFormat());
319               
320                // Caching
321                String xcid;
322       
323                if (getCache().equals(Cache.SKIP)) {
324                       
325                        sourceStream = getSourceStream();
326                       
327                } else { 
328                        if (getCache().equals(Cache.USE)) {
329                                        sourceStream = Cache.getCache().getFromCache(req_key);
330                        }
331                        if (sourceStream == null) { // either not in cache or cache_flag=refresh
332                                //sourceStream = getTargetRequest().openStream();
333                                long startMillis = System.currentTimeMillis();
334                                sourceStream = getSourceStream();
335                                duration = System.currentTimeMillis() - startMillis;
336                                req_key = addDurationKey();
337                               
338                                long now = System.currentTimeMillis();
339                                xcid = Cache.getCache().putInCache(req_key,sourceStream);                       
340                                //Admin.notifyUser("putting in cache: " + req_key);                     
341                                sourceStream = Cache.getCache().getFromCache(req_key);
342                        } /* else {
343                                Admin.notifyUser("reading from cache: " + req_key);
344                        } */
345                }
346                       
347                if (format.equals("xml")) {                     
348                        resultStream = sourceStream;   
349                }else {
350                        MDTransformer transformer = new MDTransformer();
351                        // set URL as srcFile (for MDTransformer to pass to xsl-scripts)
352                        // TODO: WHY??
353                        transformer.setSrcFile(getTargetProxy().getTargetRequest());
354                        transformer.setParams(createTransformerParams());
355                        // here the transformation is invoked
356                        resultStream = transformer.transformXML(sourceStream);
357                }
358               
359                log.debug(getActionkey() + " success:" + (resultStream!=null));
360        }
361
362        /**
363         * default Action method that gets called by Struts. Everything interesting happens in prepare()
364         */
365        public String execute() throws Exception {
366//              HttpServletRequest request = ServletActionContext.getRequest();
367                //Admin.notifyUser("session-attrs:");
368        //Admin.notifyUser(getServletRequest().getRemoteUser() );
369        //Admin.notifyUser(String.valueOf(getSession()));
370       
371        long now = System.currentTimeMillis();
372
373                prepare();
374                long duration = (System.currentTimeMillis() - now);
375                //duration
376
377                if (resultStream == null) {
378                        return ERROR;   
379                } else {
380                        return SUCCESS;
381                }               
382        }
383
384
385}
Note: See TracBrowser for help on using the repository browser.