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 | |
---|
30 | /** |
---|
31 | * Main Struts 2 controller |
---|
32 | * responds to requests (collections, model, recordset) |
---|
33 | * by dispatching the requests to appropriate internal methods and filling the inputStream with the result |
---|
34 | * |
---|
35 | * @author vronk |
---|
36 | * |
---|
37 | */ |
---|
38 | public class MDRepoProxyAction extends GenericProxyAction { |
---|
39 | |
---|
40 | private static final long serialVersionUID = 1L; |
---|
41 | |
---|
42 | private String proxy_key = "mdrepository"; |
---|
43 | |
---|
44 | |
---|
45 | @Override |
---|
46 | public String getBaseURI() { |
---|
47 | Admin.notifyUser("MDREPO-property:" + Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
48 | Admin.notifyUser("MDREPO-profile:" + getRepositoryPath()); |
---|
49 | String uri = getRepositoryPath();//Admin.getConfig().getProperty(getProxyKey() + ".uri"); |
---|
50 | return uri; |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public void setFormat(String format) { |
---|
55 | super.setFormat(format); |
---|
56 | } |
---|
57 | @Override |
---|
58 | public String getProxyKey() { |
---|
59 | return proxy_key; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | private final static HashMap<String,String> urls = new HashMap<String,String>(); |
---|
64 | static |
---|
65 | { urls.put("collection", "?operation=getCollections&collection="); |
---|
66 | urls.put("model", "?q="); |
---|
67 | urls.put("recordset", "?operation=searchRetrieve&query="); |
---|
68 | urls.put("record", "?operation=searchRetrieve&query="); |
---|
69 | } |
---|
70 | /* |
---|
71 | @Override |
---|
72 | public URL getBaseURL() throws MalformedURLException { |
---|
73 | if (base_url == null) { |
---|
74 | base_url = new URL(Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
75 | try { |
---|
76 | Admin.notifyUser("TEST-URLold:" + base_url.toURI()); |
---|
77 | } catch (URISyntaxException e) { |
---|
78 | // TODO Auto-generated catch block |
---|
79 | e.printStackTrace(); |
---|
80 | } |
---|
81 | base_url = new URL(getRepository()); |
---|
82 | try { |
---|
83 | Admin.notifyUser("TEST-URLnew:" + base_url.toURI()); |
---|
84 | } catch (URISyntaxException e) { |
---|
85 | // TODO Auto-generated catch block |
---|
86 | e.printStackTrace(); |
---|
87 | } |
---|
88 | } |
---|
89 | return base_url; |
---|
90 | } |
---|
91 | */ |
---|
92 | |
---|
93 | /** |
---|
94 | * uses base_url + url_pattern (parametrized by actionkey) to form a url |
---|
95 | * @return the URL to the component-file |
---|
96 | * @throws Exception |
---|
97 | */ |
---|
98 | public URL getTargetRequest() throws IOException { |
---|
99 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
100 | |
---|
101 | Admin.notifyUser("MDRPA.getQ:" + getSquery() + " and (" + getQ() + ")"); |
---|
102 | Admin.notifyUser("MDRPA.getActionkey:" + getActionkey()); |
---|
103 | Admin.notifyUser("MDRPA.getMaxdepth:" + getMaxdepth()); |
---|
104 | |
---|
105 | Query query = new Query(getSquery(), getQ(),getActionkey()); |
---|
106 | if (query.isStatus(Query.PARSEERROR)) { |
---|
107 | Admin.notifyUser("MDRPA.query.PARSEERROR:" + query.getMsg()); |
---|
108 | // pass this bad news to the client |
---|
109 | setUserMsg(query.getMsg()); |
---|
110 | return null; |
---|
111 | } else { |
---|
112 | query.setMaxdepth(getMaxdepth()); |
---|
113 | query.setCollection(getCollection()); |
---|
114 | query.setColumns(getColumns()); |
---|
115 | query.setMaximumRecords(getMaximumRecords()); |
---|
116 | query.setStartRecord(getStartRecord()); |
---|
117 | //Admin.notifyUser("MDRPA.records:" + query.getStartRecord()); |
---|
118 | //Admin.notifyUser("MDRPA.records:" + query.getMaximumRecords()); |
---|
119 | URL targetURL = null; |
---|
120 | |
---|
121 | targetURL =new URL( getBaseURL(), urls.get(getActionkey()) + query.toURLParam() ); |
---|
122 | Admin.notifyUser("MDRPA.targetURL:" + targetURL); |
---|
123 | return targetURL; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /* |
---|
129 | * FOLLOWING ARE OBSOLETE METHODS |
---|
130 | * for processing the requests and serving the results |
---|
131 | * they stay here for reference (as bad examples) temporarily |
---|
132 | * and can/shall be removed in the near future. |
---|
133 | */ |
---|
134 | |
---|
135 | /** |
---|
136 | * serves the collections, passes the Request to MDRepositoryProxy |
---|
137 | * @return |
---|
138 | * @throws Exception |
---|
139 | */ |
---|
140 | /* |
---|
141 | public String collections() throws Exception { |
---|
142 | |
---|
143 | Query query = new Query(getQ(),Query.COLLECTIONS); |
---|
144 | |
---|
145 | // inputStream = getMDRepo().getCollections(query); |
---|
146 | |
---|
147 | return SUCCESS; |
---|
148 | } |
---|
149 | */ |
---|
150 | /** |
---|
151 | * serves the model/terms, via Query.execute() and .getResult() |
---|
152 | * @return |
---|
153 | * @throws Exception |
---|
154 | */ |
---|
155 | /* |
---|
156 | public String model() throws Exception { |
---|
157 | |
---|
158 | Query query = new Query(getQ(), Query.MODEL, getCollection()); |
---|
159 | Admin.notifyUser(query.getTargetRequest().toString()); |
---|
160 | query.setMaxdepth(getMaxdepth()); |
---|
161 | query.execute(); |
---|
162 | |
---|
163 | //inputStream = query.getResult().getResultStream(format); |
---|
164 | return SUCCESS; |
---|
165 | } |
---|
166 | */ |
---|
167 | /** |
---|
168 | * the method for actual querying of MDRepository |
---|
169 | * and getting back the MD-recordset |
---|
170 | * using Query-object (not asking MDRepository yet) |
---|
171 | * |
---|
172 | * @return |
---|
173 | * @throws Exception |
---|
174 | * @throws Exception |
---|
175 | */ |
---|
176 | /* |
---|
177 | public String recordset() throws Exception { |
---|
178 | |
---|
179 | Query query = new Query(getQ(), Query.RECORDSET, getCollection()); |
---|
180 | |
---|
181 | Admin.notifyUser("recordset_target_uri:" + query.getTargetRequest().toString()); |
---|
182 | query.execute(); |
---|
183 | |
---|
184 | // String str =query.getResult().getHeader(); |
---|
185 | |
---|
186 | // inputStream = new ByteArrayInputStream(str.getBytes()); |
---|
187 | |
---|
188 | // inputStream = query.getResult().getResultStream(format); |
---|
189 | return SUCCESS; |
---|
190 | |
---|
191 | } |
---|
192 | */ |
---|
193 | |
---|
194 | @Override |
---|
195 | public void prepare() throws Exception{ |
---|
196 | super.prepare(); |
---|
197 | |
---|
198 | } |
---|
199 | } |
---|