source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/GenericWorkspaceAction.java @ 659

Last change on this file since 659 was 659, checked in by gaba, 14 years ago
File size: 4.2 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.BufferedInputStream;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.MalformedURLException;
9import java.net.URL;
10
11import javax.servlet.http.HttpServletRequest;
12import org.apache.struts2.interceptor.ServletRequestAware;
13
14import com.opensymphony.xwork2.ActionSupport;
15
16/**
17 *
18 * @author
19 *
20 */
21public class GenericWorkspaceAction extends ActionSupport
22{
23
24        private static final long serialVersionUID = 1L;
25       
26        /**
27         * Properties to be filled by Struts with according request-parameters
28         */     
29        private String actionkey;
30        private String q;
31        private String id;
32        private String format;
33        private String userMsg;
34       
35        private String base_path;
36       
37        public String getQ() {
38                return q;
39        }
40
41        public void setQ(String q) {
42                this.q = q;
43        }
44
45        public String getID() {
46                return id;
47        }
48
49        public void setID(String _id) {
50                this.id = _id;
51        }
52       
53        public String getFormat() {
54                return format;
55        }
56
57        public void setFormat(String format) {
58                this.format = format;
59        }
60       
61        public String getActionkey() {
62                return actionkey;
63        }
64
65        public void setActionkey(String actionKey) {
66                actionkey = actionKey;
67        }
68
69        /**
70         * primitive identification of the target-proxy
71         * base for finding the right base_url in the props
72         * subclass has to override with its specific proxykey
73         * @return
74         */
75        public String getProxyKey() {
76                return "";
77        }
78       
79        public String getUserMsg() {
80                return userMsg;
81        }
82
83        public void setUserMsg(String userMsg) {
84                this.userMsg = this.userMsg + "\n" + userMsg;
85        }
86
87
88        private InputStream resultStream;
89        private InputStream sourceStream;
90
91        /**
92         * The stream holding the resulting data to be sent back to the user as response
93         * @return
94         */
95        public InputStream getResultStream() {
96                return resultStream;
97    }
98        public void setResultStream(InputStream _resultStream){
99                resultStream = _resultStream;
100        }
101        public void setSourceStream(InputStream _sourceStream){
102                sourceStream = _sourceStream;
103        }
104         
105        public URL getBaseURL() {
106                File file=new File(getPath());
107            URL url=null;
108            try{
109                    url=file.toURL(); 
110                    Admin.notifyUser(" JSON TEST: url "  + url);
111            } catch (MalformedURLException e){
112                Admin.notifyUser("JSON exception has been caught" + e);
113            }
114           
115            return url;
116        }
117       
118        public String getBasePath() {           
119                if (base_path == null) {
120                        base_path = Admin.getConfig().getProperty(getProxyKey() + ".path");
121                }
122                return base_path;
123        }
124        //TODO identification of WOrkspaceProfile (user, name, id)
125        public String getWorkspaceProfile() {
126                return "WorkspaceProfile.xml";
127        }
128       
129        public String getFullFormat() {
130                return "xml2json";
131        }
132       
133        public URL getURL() {
134                File file=new File(getPath());
135            URL url=null;
136            try{
137                    url=file.toURL(); 
138                    //Admin.notifyUser("url "  + url);
139            } catch (MalformedURLException e){
140                //Admin.notifyUser("" + e);
141            }
142                return url;
143        }
144       
145        public String getPath() {
146                String targetPath = getBasePath() + getWorkspaceProfile();         
147                     
148        return targetPath;
149        }
150
151        public InputStream getSourceStream() throws IOException {       
152                InputStream stream = new BufferedInputStream( new FileInputStream(getPath()));
153       
154               
155                return   stream;
156        }
157       
158        public InputStream  getPart(InputStream stream){
159                return stream;
160        }
161        @SuppressWarnings("deprecation")
162        public void prepare() throws Exception {               
163               
164                //Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat());
165                Admin.notifyUser("execute:");
166                sourceStream = getSourceStream(); 
167                if (format.equals("xml")) {                     
168                        resultStream = sourceStream;   
169                }else { //JSON
170                        // set srcFile (for MDTransformer to pass to xsl-scripts)
171                        MDTransformer.getMDTransformer().setSrcFile(getURL());
172                        InputStream s =  MDTransformer.getMDTransformer().transformXML(sourceStream, getFullFormat());
173                        resultStream = getPart(s);
174                }
175                       
176                Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null));
177        }
178
179        /**
180         * default Action method
181         */
182        public String execute() throws Exception {
183               
184                prepare();
185                if (resultStream == null) {
186                        return ERROR;   
187                } else {
188                        return SUCCESS;
189                }               
190        }
191       
192        public void save() throws IOException {
193                sourceStream = getSourceStream(); 
194        }
195
196
197}
Note: See TracBrowser for help on using the repository browser.