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 | * Proxy to the Metadata Repository. |
---|
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 | * It is possible to switch target-repository dynamically (on every request). I.e. the client explicitly selects one of the repositories. |
---|
36 | * |
---|
37 | * @author vronk |
---|
38 | * |
---|
39 | */ |
---|
40 | public class MDRepoProxyAction extends GenericProxyAction { |
---|
41 | |
---|
42 | private static final long serialVersionUID = 1L; |
---|
43 | |
---|
44 | private String proxy_key = "mdrepository"; |
---|
45 | |
---|
46 | /** |
---|
47 | * Override of the basic method provided by the super-class |
---|
48 | */ |
---|
49 | @Override |
---|
50 | public String getBaseURI() { |
---|
51 | String uri = getRepositoryPath(); |
---|
52 | return uri; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public void setFormat(String format) { |
---|
57 | super.setFormat(format); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public String getProxyKey() { |
---|
62 | return proxy_key; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * A mapping between the actionkeys in the request and the operation-parameter expected by the MDRepository |
---|
67 | */ |
---|
68 | private final static HashMap<String,String> urls = new HashMap<String,String>(); |
---|
69 | static |
---|
70 | { urls.put("collection", "?operation=getCollections&collection="); |
---|
71 | urls.put("model", "?operation=queryModel&q="); |
---|
72 | urls.put("values", "?operation=scanIndex&q="); |
---|
73 | urls.put("recordset", "?operation=searchRetrieve&query="); |
---|
74 | urls.put("record", "?operation=searchRetrieve&query="); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * uses base_url + url_pattern (parametrized by actionkey) to form a url |
---|
79 | * @return the URL to the component-file |
---|
80 | * @throws Exception |
---|
81 | */ |
---|
82 | public URL getTargetRequest() throws IOException { |
---|
83 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
84 | |
---|
85 | Admin.notifyUser("MDRPA.getQ:" + getSquery() + " and (" + getQ() + ")"); |
---|
86 | Admin.notifyUser("MDRPA.getActionkey:" + getActionkey()); |
---|
87 | |
---|
88 | Query query = new Query(getSquery(), getQ(),getActionkey()); |
---|
89 | |
---|
90 | // check if the query could get parsed |
---|
91 | if (query.isStatus(Query.PARSEERROR)) { |
---|
92 | Admin.notifyUser("MDRPA.query.PARSEERROR:" + query.getMsg()); |
---|
93 | // pass this bad news to the client |
---|
94 | setUserMsg(query.getMsg()); |
---|
95 | return null; |
---|
96 | } else { |
---|
97 | query.setMaxdepth(getMaxdepth()); |
---|
98 | query.setCollection(getCollection()); |
---|
99 | query.setColumns(getColumns()); |
---|
100 | query.setMaximumItems(getMaximumItems()); |
---|
101 | query.setStartItem(getStartItem()); |
---|
102 | query.setOptions(getOptions()); |
---|
103 | query.setSort(getSort()); |
---|
104 | |
---|
105 | URL targetURL = null; |
---|
106 | targetURL =new URL( getBaseURL(), urls.get(getActionkey()) + query.toURLParam() ); |
---|
107 | Admin.notifyUser("MDRPA.targetURL.query.toURLParam:" + query.toURLParam()); |
---|
108 | Admin.notifyUser("MDRPA.targetURL:" + targetURL); |
---|
109 | return targetURL; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | @Override |
---|
115 | public void prepare() throws Exception{ |
---|
116 | super.prepare(); |
---|
117 | |
---|
118 | } |
---|
119 | } |
---|