source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/CompRegistryProxyAction.java @ 898

Last change on this file since 898 was 898, checked in by gaba, 14 years ago
File size: 5.9 KB
Line 
1 package eu.clarin.cmdi.mdservice.action;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.URL;
6
7/**
8 * Acts as a Struts 2 controller
9 * serves the requests regarding Components
10 * it passes them further (Proxy) to the ComponentRegistry.
11 * 
12 * Nested Components are resolved during xsl-transformation.
13 * @author vronk
14 *
15 */
16public class CompRegistryProxyAction extends GenericProxyAction {
17
18        private static final long serialVersionUID = 1L;
19       
20        private String proxy_key = "compregistry";
21        private String compid;
22       
23        public String getCompid() {
24                return compid;
25        }
26
27        public void setCompid(String compid) {
28                this.compid = compid;
29        }
30
31        @Override 
32        public String getFullFormat() {
33                if (compid==null || compid.equals("")) {
34                        if (getActionkey().equals("elements")){
35                                return "terms2" + getFormat(); 
36                        } else {
37                                return "complist2" + getFormat();
38                        }
39                } else {
40                        return "comp2" + getFormat();
41                }
42        }
43       
44        @Override
45        public String getProxyKey() {
46                return proxy_key;
47        }
48       
49        @Override
50        /*
51        public String getRequestKey() {
52                String key="";
53                if (getActionkey()!=null) {
54                        key += getActionkey() + "//-" ;
55                }
56                if (getCompid()!=null) {
57                        key += getCompid() + "//-" ;
58                }
59                 
60                return key;
61        }
62*/
63         public String getRequestKey() {
64                String key="";
65                if (getActionkey()!=null) {
66                        key += getActionkey() + "//-" ;
67                }else {
68                        key +="//-" ;
69                }
70                if (getCompid()!=null) {
71                        key += getCompid() + "//-" ;
72                } else {
73                        key +="//-" ;
74                }
75                //if (getCollection()!=null) {
76                key +="//-" ;
77               
78                //if (getSquery()!=null) {
79                key +="//-" ;
80               
81                if (getStartRecord()!=null) {
82                        key += getStartRecord() + "//-";
83                }
84                        else{
85                                key +="//-" ;
86                }
87                if (getMaximumRecords()!=null) {
88                        key += getMaximumRecords() + "//-";
89                }
90                else{
91                        key +="//-" ;
92                }
93               
94                key += getRepository()  + "//-";
95                key += getMaxdepth()  + "//-";
96               
97                if (getLang()!=null) {
98                        key += getLang() + "//-";
99                }else{
100                        key +="//-" ;
101                }
102               
103                 
104                return key;
105        }
106
107               
108        @SuppressWarnings("deprecation")       
109        @Override
110        public void prepare() throws Exception {               
111               
112                Admin.notifyUser("CRPA.prepare");
113                if (!getActionkey().equals("elements")) {
114                        super.prepare();
115                } else  {
116                        Admin.notifyUser("CRPA.prepareSourceStream - elements");
117                        String xcid;
118                        InputStream _sourceStream;
119                        InputStream __sourceStream;
120                        InputStream _resultStream;
121                       
122                        _sourceStream = Cache.getCache().getFromCache(getRequestKey());
123                        if (_sourceStream == null) {
124                                        Admin.notifyUser("CRPA.prepareSourceStream- profiles");
125                                        setActionkey("profiles");
126                                        setFormat("xmlresolved");
127                                        __sourceStream = Cache.getCache().getFromCache(getRequestKey());
128                                        if (__sourceStream == null) {
129                                                __sourceStream = getSourceStream();
130                                                xcid = Cache.getCache().putInCache(getRequestKey(),__sourceStream);                     
131                                                Admin.notifyUser("CRPA-putting in cache: " + getRequestKey()); 
132                                                __sourceStream = Cache.getCache().getFromCache(getRequestKey());
133                                        } else{
134                                                Admin.notifyUser("CRPA-reading from cache: " + getRequestKey());       
135                                        }
136                                        Admin.notifyUser("CCCRPA." + getTargetRequest());
137                                        Admin.notifyUser("CCCRPA." + getFullFormat());
138                                        MDTransformer.getMDTransformer().setSrcFile(getTargetRequest());
139                                        MDTransformer.getMDTransformer().setParams(createTransformerParams());
140                                        _sourceStream = MDTransformer.getMDTransformer().transformXML(__sourceStream);//, getFullFormat(),null,null,null,null,null,null);
141                                        Admin.notifyUser("CRPA.- elements");
142                                        setActionkey("elements");
143                                        setFormat("htmllist");
144                                        xcid = Cache.getCache().putInCache(getRequestKey(),_sourceStream);                     
145                                        Admin.notifyUser("CRPA-putting in cache: " + getRequestKey());
146                                        _sourceStream = Cache.getCache().getFromCache(getRequestKey());
147                        }  else {
148                                        Admin.notifyUser("CRPA-reading from cache: " + getRequestKey());
149                        }
150                                /*
151                        File file=new File(Cache.getCache().formPath(getRequestKey()));
152                        Admin.notifyUser("File-absolutePath " + file.getAbsolutePath());
153                        URL url = null;
154                           try{
155                                   url = file.toURL();
156                                   Admin.notifyUser("URL-tostring " + url.toString());
157                                   }  catch (MalformedURLException me){
158                          }
159                         */
160                       
161                        if (getFormat().equals("xml")) {                       
162                                _resultStream = _sourceStream; 
163                        }else {
164                                // set URL as srcFile (for MDTransformer to pass to xsl-scripts)
165                                MDTransformer.getMDTransformer().setSrcFile(getTargetRequest());
166                                MDTransformer.getMDTransformer().setParams(MDTransformer.createParamsMap(getFullFormat()));
167                                _resultStream = MDTransformer.getMDTransformer().transformXML(_sourceStream);//, getFullFormat());
168                                       
169                        }
170       
171                        Admin.notifyUser(getProxyKey() + " success:" + (_resultStream!=null));
172                       
173                        setSourceStream(_sourceStream);
174                        setResultStream(_resultStream);
175
176                        /*
177                        if (getFormat().equals("txt")) {
178                                File f=new File(Cache.getCache().formPath("terms_autocomplete.txt"));
179                            OutputStream out=new FileOutputStream(f);
180                            byte buf[]=new byte[1024];
181                            int len;
182                            while((len=getResultStream().read(buf))>0)
183                                out.write(buf,0,len);
184                            out.close();
185Admin.notifyUser("terms_autocomplete.txt");
186                        }
187                        */
188                       
189                }
190
191        }
192
193
194        /**
195         * uses base_url and (optionally) compid to form a url
196         * no compid => list all components/profiles
197         * @return the URL to component-registry
198         * @throws Exception
199         */
200        public URL getTargetRequest() throws IOException {
201        // URL targetURL =new URL( base_url, compname + ".xml");
202                URL targetURL = null;
203                if (compid==null || compid.equals("")) {
204                        targetURL =new URL( getBaseURL(), getActionkey() );
205                } else {
206                        targetURL =new URL( getBaseURL(), getActionkey() + "/clarin.eu:cr1:" + compid);
207                }
208//              Admin.notifyUser("CompReg.targetURL:" + targetURL);             
209     //   URLConnection urlConnection = targetURL.openConnection();
210//        urlConnection.setRequestProperty("Accept", "application/xml");                 
211        return targetURL;
212        }
213
214       
215}
Note: See TracBrowser for help on using the repository browser.