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

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

introducing the user: only real change in GenericProxyAction?
the rest is just removed unused imports.

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