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

Last change on this file since 704 was 704, checked in by gaba, 14 years ago
File size: 5.7 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 cache = Cache.USE;
33        private String collection;
34        private String columns;
35        private int maxdepth;
36        private String format;
37        private String userMsg;
38        //private Map<String,Object> session;
39         private HttpServletRequest request; 
40
41        public String getQ() {
42                return q;
43        }
44
45        public void setQ(String q) {
46                this.q = q;
47        }
48
49        public String getCache() {
50                return cache;
51        }
52
53        public void setCache(String cache) {
54                this.cache = cache;
55        }
56
57        public String getCollection() {
58                return collection;
59        }
60
61        public void setCollection(String collection) {
62                this.collection = collection;
63        }
64       
65        //TODO defaults
66        public String getColumns() {
67                String cols = columns;
68                /*if (columns == null){
69                        cols = "Id,Name,Title";
70                }
71                */
72                return cols;
73        }
74
75        public void setColumns(String columns) {
76                this.columns = columns;
77        }
78       
79        public int getMaxdepth() {
80                return maxdepth;
81        }
82
83        public void setMaxdepth(int maxdepth) {
84                this.maxdepth = maxdepth;
85        }
86
87        public String getFormat() {
88                return format;
89        }
90
91        public void setFormat(String format) {
92                this.format = format;
93        }
94       
95        public String getActionkey() {
96                return actionkey;
97        }
98
99        public void setActionkey(String actionKey) {
100                actionkey = actionKey;
101        }
102
103        public String getFullFormat() {         
104                return actionkey + "2" + format;
105        }
106
107        /**
108         * primitive identification of the target-proxy
109         * base for finding the right base_url in the props
110         * subclass has to override with its specific proxykey
111         * @return
112         */
113        public String getProxyKey() {
114                return "";
115        }
116       
117        public String getUserMsg() {
118                return userMsg;
119        }
120
121        public void setUserMsg(String userMsg) {
122                this.userMsg = this.userMsg + "\n" + userMsg;
123        }
124       
125        @Override
126        public void setServletRequest(HttpServletRequest arg0) {
127                request = arg0;
128        }
129
130        public HttpServletRequest getServletRequest() {
131                return request;
132        }
133
134
135        private InputStream resultStream;
136        private InputStream sourceStream;
137
138        /**
139         * The stream holding the resulting data to be sent back to the user as response
140         * @return
141         */
142        public InputStream getResultStream() {
143                return resultStream;
144    }
145        public void setResultStream(InputStream _resultStream){
146                resultStream = _resultStream;
147        }
148        public void setSourceStream(InputStream _sourceStream){
149                sourceStream = _sourceStream;
150        }
151       
152        /*
153        public InputStream getSourceStream() throws IOException {
154                return getTargetRequest().openStream();
155    }
156*/
157       
158        private URL base_url ;
159       
160        public URL getBaseURL() throws MalformedURLException {         
161                if (base_url == null) {
162                        base_url = new URL(Admin.getConfig().getProperty(getProxyKey() + ".uri"));
163                }
164                return base_url;
165        }
166       
167       
168        public URL getTargetRequest() throws IOException {
169        // URL targetURL =new URL( base_url, compname + ".xml");
170                URL targetURL = getBaseURL();     
171                     
172        return targetURL;
173        }
174       
175        public String getRequestKey() {
176                String key="";
177                if (getActionkey()!=null) {
178                        key += getActionkey() + "//-" ;
179                }
180                if (getQ()!=null) {
181                        key += getQ() + "//-" ;
182                }
183                if (getCollection()!=null) {
184                        key += getCollection();
185                }
186                 
187                return key;
188        }
189
190        public InputStream getSourceStream() throws IOException {               
191                return getTargetRequest().openStream();
192                //      Admin.notifyUser(getProxyKey() + ".getSourceStream() - unable to open stream: " + getTargetRequest(); 
193        }
194       
195       
196        public void prepare() throws Exception {               
197               
198                Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat());
199               
200                // Caching
201                String xcid;
202                Admin.notifyUser("GPA.prepareSourceStream");
203                if (getCache().equals(Cache.SKIP)) {
204                       
205                        sourceStream = getSourceStream();
206                       
207                } else { 
208                        if (getCache().equals(Cache.USE)) {
209                                        sourceStream = Cache.getCache().getFromCache(getRequestKey());
210                        }
211
212                        if (sourceStream == null) { // either not in cache or cache_flag=refresh
213                                //sourceStream = getTargetRequest().openStream();
214                                sourceStream = getSourceStream();
215                                xcid = Cache.getCache().putInCache(getRequestKey(),sourceStream);                       
216                                Admin.notifyUser("putting in cache: " + getRequestKey());                       
217                                sourceStream = Cache.getCache().getFromCache(getRequestKey());
218                        }  else {
219                                Admin.notifyUser("reading from cache: " + getRequestKey());
220                        }
221                }
222                       
223                if (format.equals("xml")) {                     
224                        resultStream = sourceStream;   
225                }else {
226                        // set URL as srcFile (for MDTransformer to pass to xsl-scripts)
227                        MDTransformer.getMDTransformer().setSrcFile(getTargetRequest());
228                        // getColumns
229                        resultStream = MDTransformer.getMDTransformer().transformXML(sourceStream, getFullFormat(), getColumns());
230                }
231                       
232                Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null));
233        }
234
235        /**
236         * default Action method
237         */
238        public String execute() throws Exception {
239//              HttpServletRequest request = ServletActionContext.getRequest();
240
241               
242                Admin.notifyUser("session-attrs:");
243      Admin.notifyUser(getServletRequest().getRemoteUser() );
244        //Admin.notifyUser(String.valueOf(getSession()));
245     
246                prepare();
247                if (resultStream == null) {
248                        return ERROR;   
249                } else {
250                        return SUCCESS;
251                }               
252        }
253
254
255}
Note: See TracBrowser for help on using the repository browser.