1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.io.StringReader; |
---|
7 | import java.io.StringWriter; |
---|
8 | import java.net.MalformedURLException; |
---|
9 | import java.net.URISyntaxException; |
---|
10 | import java.net.URL; |
---|
11 | import java.util.HashMap; |
---|
12 | import java.io.ByteArrayInputStream; |
---|
13 | |
---|
14 | import javax.xml.parsers.DocumentBuilder; |
---|
15 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
16 | import javax.xml.transform.OutputKeys; |
---|
17 | import javax.xml.transform.Transformer; |
---|
18 | import javax.xml.transform.TransformerException; |
---|
19 | import javax.xml.transform.TransformerFactory; |
---|
20 | import javax.xml.transform.dom.DOMSource; |
---|
21 | import javax.xml.transform.stream.StreamResult; |
---|
22 | |
---|
23 | import org.w3c.dom.Document; |
---|
24 | import org.w3c.dom.Node; |
---|
25 | import org.w3c.dom.NodeList; |
---|
26 | import org.xml.sax.InputSource; |
---|
27 | |
---|
28 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
29 | import 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 | */ |
---|
41 | public class RepoProxyAction extends MDRepoProxyAction { |
---|
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 | //TODO remove - just for tests |
---|
53 | @Override |
---|
54 | public String getCache() { |
---|
55 | switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) |
---|
56 | { |
---|
57 | case PAZPAR: |
---|
58 | return Cache.SKIP; |
---|
59 | case SRU: |
---|
60 | return Cache.SKIP; |
---|
61 | case MD: |
---|
62 | return super.getCache(); |
---|
63 | default: |
---|
64 | return super.getCache(); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | //TODO remove after xsl update |
---|
69 | @Override |
---|
70 | public String getFullFormat() { |
---|
71 | switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) |
---|
72 | { |
---|
73 | case PAZPAR: |
---|
74 | return "pazpar22" + this.getFormat(); |
---|
75 | case SRU: |
---|
76 | return "sru2" + this.getFormat(); |
---|
77 | case MD: |
---|
78 | return super.getFullFormat(); |
---|
79 | default: |
---|
80 | return super.getFullFormat(); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | public URL getTargetRequestSRU() throws IOException, NoStylesheetException{ |
---|
85 | |
---|
86 | SRUProxyAction sruaction = new SRUProxyAction(); |
---|
87 | // TODO MD to SRU params |
---|
88 | // missing collections, columns... |
---|
89 | sruaction.setOperation("searchRetrieve"); |
---|
90 | //TODO what version |
---|
91 | sruaction.setVersion("1.2"); |
---|
92 | sruaction.setStartRecord(Integer.parseInt(this.getStartItem())); |
---|
93 | sruaction.setMaximumRecords(Integer.parseInt(this.getMaximumItems())); |
---|
94 | sruaction.setRepository(getRepository()); |
---|
95 | sruaction.setQuery(this.fullQueryString()); |
---|
96 | |
---|
97 | return sruaction.getTargetRequest(); |
---|
98 | |
---|
99 | } |
---|
100 | public InputStream getSourcePz2() throws Exception { |
---|
101 | |
---|
102 | InputStream is = null; |
---|
103 | Pz2ProxyAction pzaction = new Pz2ProxyAction(); |
---|
104 | // TODO param settings |
---|
105 | //init |
---|
106 | pzaction.setRepository(getRepository()); |
---|
107 | pzaction.setCommand("init"); |
---|
108 | String sessionID = Helpers.getDocumentData(pzaction.getSourceStream(), "//init/session"); |
---|
109 | // get result |
---|
110 | pzaction.setCommand("search"); |
---|
111 | pzaction.setSessionID(sessionID); |
---|
112 | pzaction.setQuery(this.fullQueryString()); |
---|
113 | String ok = Helpers.getDocumentData(pzaction.getSourceStream(),"//search/status"); |
---|
114 | |
---|
115 | String activeclients = "1"; |
---|
116 | pzaction.setCommand("show"); |
---|
117 | pzaction.setSort("relevance"); |
---|
118 | pzaction.setBlock("1"); |
---|
119 | //TODO timeout settings |
---|
120 | long startMillis = System.currentTimeMillis(); |
---|
121 | long timeout = 0; |
---|
122 | if (WorkspaceProfile.getOption("pazpartimeout") == null){ |
---|
123 | timeout = 1000; |
---|
124 | } else { |
---|
125 | timeout = Integer.parseInt(WorkspaceProfile.getOption("pazpartimeout")); |
---|
126 | } |
---|
127 | while (Integer.parseInt(activeclients) > 0){ |
---|
128 | //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients"); |
---|
129 | is = pzaction.getSourceStream(); |
---|
130 | activeclients = Helpers.getDocumentData(is, "//show/activeclients"); |
---|
131 | Thread.sleep(1000); |
---|
132 | Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG); |
---|
133 | if ((System.currentTimeMillis() - startMillis)/1000 > timeout) { |
---|
134 | break; |
---|
135 | } |
---|
136 | } |
---|
137 | is = pzaction.getSourceStream(); |
---|
138 | return is; |
---|
139 | } |
---|
140 | |
---|
141 | @Override |
---|
142 | public InputStream getSourceStream() throws IOException, NoStylesheetException { |
---|
143 | |
---|
144 | |
---|
145 | switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) |
---|
146 | { |
---|
147 | case PAZPAR: |
---|
148 | try { |
---|
149 | return getSourcePz2(); |
---|
150 | } catch (Exception e) { |
---|
151 | // TODO Auto-generated catch block |
---|
152 | e.printStackTrace(); |
---|
153 | } |
---|
154 | case SRU: |
---|
155 | return super.getSourceStream();//getSourceSRU(); |
---|
156 | case MD: |
---|
157 | return super.getSourceStream(); |
---|
158 | default: |
---|
159 | return super.getSourceStream(); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | @Override |
---|
165 | public URL getTargetRequest() throws IOException { |
---|
166 | switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) |
---|
167 | { |
---|
168 | case PAZPAR: |
---|
169 | return base_url; |
---|
170 | case SRU: |
---|
171 | try { |
---|
172 | return getTargetRequestSRU(); |
---|
173 | } catch (NoStylesheetException e) { |
---|
174 | // TODO Auto-generated catch block |
---|
175 | e.printStackTrace(); |
---|
176 | } |
---|
177 | case MD: |
---|
178 | return super.getTargetRequest(); |
---|
179 | default: |
---|
180 | return super.getTargetRequest(); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|