source: MDService2/branches/MDService_simple/src/eu/clarin/cmdi/mdservice/action/RepoProxyAction.java @ 1508

Last change on this file since 1508 was 1508, checked in by vronk, 13 years ago

intermediate consolidated version of the interface (struts.xml),
that should at least be able to search in md + basic static responses (index);
also added a separate catchall-response for unsupported requests.

File size: 7.8 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.StringReader;
7import java.io.StringWriter;
8import java.net.MalformedURLException;
9import java.net.URISyntaxException;
10import java.net.URL;
11import java.util.HashMap;
12import java.io.ByteArrayInputStream;
13
14import javax.xml.parsers.DocumentBuilder;
15import javax.xml.parsers.DocumentBuilderFactory;
16import javax.xml.transform.OutputKeys;
17import javax.xml.transform.Transformer;
18import javax.xml.transform.TransformerException;
19import javax.xml.transform.TransformerFactory;
20import javax.xml.transform.dom.DOMSource;
21import javax.xml.transform.stream.StreamResult;
22
23import org.w3c.dom.Document;
24import org.w3c.dom.Node;
25import org.w3c.dom.NodeList;
26import org.xml.sax.InputSource;
27
28import eu.clarin.cmdi.mdservice.model.Query;
29import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
30
31/**
32 * Main Struts 2 controller
33 * responds to requests (collections, model, recordset)
34 * by dispatching the requests to appropriate internal methods and filling the inputStream with the result
35 *
36 * It is possible to switch target-repository dynamically  (on every request). I.e. the client explicitly selects one of the repositories.
37 * 
38 * @author vronk
39 *
40 */
41public class RepoProxyAction extends GenericProxyAction {
42
43        private static final long serialVersionUID = 1L;
44
45        private String proxy_key = "repository";
46       
47        @Override
48        public String getProxyKey() {
49                return proxy_key;
50        }
51
52        /**
53         * Override of the basic method provided by the super-class
54         * Reading the repository-uri from the WorkspaceProfile based on the repository-parameter
55         */     
56        @Override       
57        public String getBaseURI() {           
58                String uri = WorkspaceProfile.getRepositoryPath(getRepository());               
59                return uri;
60        }
61       
62        /**
63         * A mapping between the actionkeys in the request and the operation-parameter expected by the MD!Repository
64         * FIXME: This is taken from MDRepoProxyAction - so it will not work for SRU and Pazpar2
65         */
66        private final static HashMap<String,String> urls = new HashMap<String,String>();
67         static
68         {       urls.put("collection", "?operation=getCollections&collection=");
69                 urls.put("model", "?operation=queryModel&q=");
70                 urls.put("values", "?operation=scanIndex&q=");
71                 urls.put("search", "?operation=searchRetrieve&query=");
72                 urls.put("record", "?operation=searchRetrieve&query=");
73         } 
74
75       
76        //TODO remove - just for tests
77        @Override
78        public String getCache() {
79                switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository())))
80                {
81                    case PAZPAR: 
82                        return Cache.SKIP;
83                    case SRU:
84                        return Cache.SKIP;
85                    case MD:
86                        return super.getCache();
87                    default:
88                        return super.getCache();
89                }
90        }
91       
92        //TODO remove after xsl update
93        @Override
94        public String getFullFormat() {         
95                switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository())))
96                {
97                    case PAZPAR: 
98                        return "pazpar22" + this.getFormat();
99                    case SRU:
100                        return "sru2" + this.getFormat();
101                    case MD:
102                        return super.getFullFormat();
103                    default:
104                        return super.getFullFormat();
105                }
106        }
107       
108public URL getTargetRequestSRU() throws IOException, NoStylesheetException{
109       
110        SRUProxyAction sruaction = new SRUProxyAction();
111        // TODO MD to SRU params
112        // missing collections, columns...
113        sruaction.setOperation("searchRetrieve");
114        //TODO what version
115        sruaction.setVersion("1.2");
116        sruaction.setStartRecord(Integer.parseInt(this.getStartItem()));
117        sruaction.setMaximumRecords(Integer.parseInt(this.getMaximumItems()));
118        sruaction.setRepository(getRepository());
119        sruaction.setQuery(this.fullQueryString());
120       
121        return sruaction.getTargetRequest();
122       
123}
124public InputStream getSourcePz2() throws Exception {
125               
126                InputStream is = null;
127                Pz2ProxyAction pzaction = new Pz2ProxyAction();
128                // TODO param settings
129                //init
130                pzaction.setRepository(getRepository());
131                pzaction.setCommand("init");
132                String sessionID = Helpers.getDocumentData(pzaction.getSourceStream(), "//init/session");
133                // get result
134                pzaction.setCommand("search");
135                pzaction.setSessionID(sessionID);
136                pzaction.setQuery(this.fullQueryString());
137                String ok = Helpers.getDocumentData(pzaction.getSourceStream(),"//search/status");
138               
139                String activeclients = "1";
140                pzaction.setCommand("show");
141                pzaction.setSort("relevance");
142                pzaction.setBlock("1");
143                //TODO timeout  settings
144                long startMillis = System.currentTimeMillis();
145                long timeout = 0;
146                if (WorkspaceProfile.getOption("pazpartimeout") == null){
147                        timeout = 1000;
148                } else {
149                        timeout = Integer.parseInt(WorkspaceProfile.getOption("pazpartimeout"));
150                }
151                while (Integer.parseInt(activeclients) > 0){
152                        //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients");
153                        is = pzaction.getSourceStream();
154                        activeclients = Helpers.getDocumentData(is, "//show/activeclients");
155                        Thread.sleep(1000); 
156                        Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG);
157                        if ((System.currentTimeMillis() - startMillis)/1000 > timeout) {
158                                break;
159                        }
160                }
161                is = pzaction.getSourceStream();
162                return is;
163        }
164               
165        @Override
166        public InputStream getSourceStream() throws IOException, NoStylesheetException {
167               
168               
169                switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository())))
170                {
171                    case PAZPAR: 
172                        try {
173                                        return getSourcePz2();
174                                } catch (Exception e) {
175                                        // TODO Auto-generated catch block
176                                        e.printStackTrace();
177                                }
178                    case SRU:
179                        return super.getSourceStream();//getSourceSRU();
180                    case MD:
181                        return super.getSourceStream();
182                    default:
183                        return super.getSourceStream();
184                }
185               
186        }
187 
188         public String fullQueryString(){
189                String full_query_string= this.getQ();
190                       
191                if (!getSquery().equals("") && !this.getQ().equals("")){
192                                full_query_string = "(" + this.getSquery() + " ) and (" + this.getQ() + ")";
193                        } else if (!this.getSquery().equals("")) {
194                                full_query_string = this.getSquery();
195                        } else if (!this.getQ().equals("")){
196                                full_query_string = this.getQ();
197                        } else {
198                                full_query_string = "";
199                        }
200               
201                return full_query_string;
202         }
203         
204
205        /**
206         * uses base_url + url_pattern (parametrized by actionkey) to form a url
207         * @return the URL to the component-file
208         * @throws Exception
209         */
210         @Override
211        public URL getTargetRequest() throws IOException {
212        // URL targetURL =new URL( base_url, compname + ".xml");
213               
214                Admin.notifyUser("RPA.getQ:" + getSquery() + " and (" +  getQ() + ")");
215                Admin.notifyUser("RPA.getActionkey:" + getActionkey());         
216               
217                Query query = new Query(getSquery(), getQ(),getActionkey());
218
219        // check if the query could get parsed
220                if (query.isStatus(Query.PARSEERROR)) {
221                        Admin.notifyUser("RPA.query.PARSEERROR:" + query.getMsg());
222                        // pass this bad news to the client
223                        setUserMsg(query.getMsg());
224                        return null;
225                } else {
226                        query.setMaxdepth(getMaxdepth());               
227                        query.setCollection(getCollection());
228                        query.setColumns(getColumns());
229                        query.setMaximumItems(getMaximumItems());
230                        query.setStartItem(getStartItem());
231                        query.setOptions(getOptions());
232                        query.setSort(getSort());
233               
234                        URL targetURL = null;
235                        targetURL =new URL( getBaseURL(), urls.get(getActionkey()) + query.toURLParam() );
236                        Admin.notifyUser("RPA.targetURL.query.toURLParam:" + query.toURLParam());
237                        Admin.notifyUser("RPA.targetURL:" + targetURL);
238                return targetURL;
239                }
240        }
241
242
243
244/*      @Override
245        public URL getTargetRequest() throws IOException {
246 
247                switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository())))
248                {
249                    case PAZPAR:
250                        return base_url;
251                    case SRU:
252                                try {
253                                        return getTargetRequestSRU();
254                                } catch (NoStylesheetException e) {
255                                        // TODO Auto-generated catch block
256                                        e.printStackTrace();
257                                }
258                    case MD:
259                        return super.getTargetRequest();
260                    default:
261                        return super.getTargetRequest();
262                }
263        }
264        */
265}       
Note: See TracBrowser for help on using the repository browser.