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

Last change on this file since 713 was 713, checked in by gaba, 14 years ago

WorkspaceProfile? - arrayName = <item>

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