source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/GenericProxyAction.java @ 768

Last change on this file since 768 was 768, checked in by vronk, 14 years ago

terms-matrix; model, datcats to terms; model-matrix-all!

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