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

Last change on this file since 879 was 879, checked in by gaba, 14 years ago
File size: 5.2 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        public String getRequestKey() {
51                String key="";
52                if (getActionkey()!=null) {
53                        key += getActionkey() + "//-" ;
54                }
55                if (getCompid()!=null) {
56                        key += getCompid() + "//-" ;
57                }
58                 
59                return key;
60        }
61
62               
63        @SuppressWarnings("deprecation")       
64        @Override
65        public void prepare() throws Exception {               
66               
67                Admin.notifyUser("CRPA.prepare");
68                if (!getActionkey().equals("elements")) {
69                        super.prepare();
70                } else  {
71                        Admin.notifyUser("CRPA.prepareSourceStream - elements");
72                        String xcid;
73                        InputStream _sourceStream;
74                        InputStream __sourceStream;
75                        InputStream _resultStream;
76                       
77                        _sourceStream = Cache.getCache().getFromCache(getRequestKey());
78                        if (_sourceStream == null) {
79                                        Admin.notifyUser("CRPA.prepareSourceStream- profiles");
80                                        setActionkey("profiles");
81                                        setFormat("xmlresolved");
82                                        __sourceStream = Cache.getCache().getFromCache(getRequestKey());
83                                        if (__sourceStream == null) {
84                                                __sourceStream = getSourceStream();
85                                                xcid = Cache.getCache().putInCache(getRequestKey(),__sourceStream);                     
86                                                Admin.notifyUser("CRPA-putting in cache: " + getRequestKey()); 
87                                                __sourceStream = Cache.getCache().getFromCache(getRequestKey());
88                                        } else{
89                                                Admin.notifyUser("CRPA-reading from cache: " + getRequestKey());       
90                                        }
91                                        Admin.notifyUser("CCCRPA." + getTargetRequest());
92                                        Admin.notifyUser("CCCRPA." + getFullFormat());
93                                        MDTransformer.getMDTransformer().setSrcFile(getTargetRequest());
94                                        MDTransformer.getMDTransformer().setParams(createTransformerParams());
95                                        _sourceStream = MDTransformer.getMDTransformer().transformXML(__sourceStream);//, getFullFormat(),null,null,null,null,null,null);
96                                        Admin.notifyUser("CRPA.- elements");
97                                        setActionkey("elements");
98                                        setFormat("htmllist");
99                                        xcid = Cache.getCache().putInCache(getRequestKey(),_sourceStream);                     
100                                        Admin.notifyUser("CRPA-putting in cache: " + getRequestKey());
101                                        _sourceStream = Cache.getCache().getFromCache(getRequestKey());
102                        }  else {
103                                        Admin.notifyUser("CRPA-reading from cache: " + getRequestKey());
104                        }
105                                /*
106                        File file=new File(Cache.getCache().formPath(getRequestKey()));
107                        Admin.notifyUser("File-absolutePath " + file.getAbsolutePath());
108                        URL url = null;
109                           try{
110                                   url = file.toURL();
111                                   Admin.notifyUser("URL-tostring " + url.toString());
112                                   }  catch (MalformedURLException me){
113                          }
114                         */
115                       
116                        if (getFormat().equals("xml")) {                       
117                                _resultStream = _sourceStream; 
118                        }else {
119                                // set URL as srcFile (for MDTransformer to pass to xsl-scripts)
120                                MDTransformer.getMDTransformer().setSrcFile(getTargetRequest());
121                                MDTransformer.getMDTransformer().setParams(MDTransformer.createParamsMap(getFullFormat()));
122                                _resultStream = MDTransformer.getMDTransformer().transformXML(_sourceStream);//, getFullFormat());
123                                       
124                        }
125       
126                        Admin.notifyUser(getProxyKey() + " success:" + (_resultStream!=null));
127                       
128                        setSourceStream(_sourceStream);
129                        setResultStream(_resultStream);
130
131                        /*
132                        if (getFormat().equals("txt")) {
133                                File f=new File(Cache.getCache().formPath("terms_autocomplete.txt"));
134                            OutputStream out=new FileOutputStream(f);
135                            byte buf[]=new byte[1024];
136                            int len;
137                            while((len=getResultStream().read(buf))>0)
138                                out.write(buf,0,len);
139                            out.close();
140Admin.notifyUser("terms_autocomplete.txt");
141                        }
142                        */
143                       
144                }
145
146        }
147
148
149        /**
150         * uses base_url and (optionally) compid to form a url
151         * no compid => list all components/profiles
152         * @return the URL to component-registry
153         * @throws Exception
154         */
155        public URL getTargetRequest() throws IOException {
156        // URL targetURL =new URL( base_url, compname + ".xml");
157                URL targetURL = null;
158                if (compid==null || compid.equals("")) {
159                        targetURL =new URL( getBaseURL(), getActionkey() );
160                } else {
161                        targetURL =new URL( getBaseURL(), getActionkey() + "/clarin.eu:cr1:" + compid);
162                }
163//              Admin.notifyUser("CompReg.targetURL:" + targetURL);             
164     //   URLConnection urlConnection = targetURL.openConnection();
165//        urlConnection.setRequestProperty("Accept", "application/xml");                 
166        return targetURL;
167        }
168
169       
170}
Note: See TracBrowser for help on using the repository browser.