1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.net.MalformedURLException; |
---|
6 | import java.net.URL; |
---|
7 | import java.net.URLConnection; |
---|
8 | import java.util.HashMap; |
---|
9 | |
---|
10 | import javax.xml.transform.stream.StreamSource; |
---|
11 | import javax.xml.xpath.XPath; |
---|
12 | import javax.xml.xpath.XPathConstants; |
---|
13 | import javax.xml.xpath.XPathExpression; |
---|
14 | import javax.xml.xpath.XPathExpressionException; |
---|
15 | import javax.xml.xpath.XPathFactory; |
---|
16 | |
---|
17 | import org.w3c.dom.Document; |
---|
18 | import org.w3c.dom.Node; |
---|
19 | import org.w3c.dom.NodeList; |
---|
20 | |
---|
21 | import eu.clarin.cmdi.mdservice.model.Termset; |
---|
22 | |
---|
23 | /** |
---|
24 | * Acts as a Struts 2 controller |
---|
25 | * serves the requests regarding DataCategories |
---|
26 | * it passes them further (Proxy) to the Data Category Registry. |
---|
27 | * |
---|
28 | * @author vronk |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | public class DCRProxyAction extends GenericProxyAction { |
---|
33 | |
---|
34 | private String proxy_key = "dcregistry"; |
---|
35 | |
---|
36 | private String dcrid; |
---|
37 | |
---|
38 | public static String ALL = "all"; |
---|
39 | public static String DATCAT = "datcat"; |
---|
40 | public static String DATCATS = "datcats"; |
---|
41 | |
---|
42 | private static final long serialVersionUID = 1L; |
---|
43 | |
---|
44 | |
---|
45 | public String getDcrid() { |
---|
46 | return dcrid; |
---|
47 | } |
---|
48 | |
---|
49 | public void setDcrid(String dcrid) { |
---|
50 | this.dcrid = dcrid; |
---|
51 | } |
---|
52 | |
---|
53 | public Termset getDCR() { |
---|
54 | String dcr_id=""; |
---|
55 | if (getQ()!=null && !getQ().equals("")) dcr_id = getQ(); |
---|
56 | // override with dcrid-param |
---|
57 | if (getDcrid()!=null && !getDcrid().equals("")) dcr_id = getDcrid(); |
---|
58 | |
---|
59 | Termset res = (Termset) Termset.getTermset(dcr_id); |
---|
60 | if (res==null) Admin.notifyUser("getDCR(): no termset found for:" + dcr_id); |
---|
61 | //else |
---|
62 | //Admin.notifyUser("getDCR():" + res.getAttr("id") + ":" + res.getAttr("url") ); |
---|
63 | |
---|
64 | return res; |
---|
65 | } |
---|
66 | |
---|
67 | public String getDCRAttr(String key) { |
---|
68 | Termset dcr = getDCR(); |
---|
69 | String val=""; |
---|
70 | if (dcr!=null) val= dcr.getAttr(key); |
---|
71 | return val; |
---|
72 | } |
---|
73 | |
---|
74 | @Override |
---|
75 | public String getProxyKey() { |
---|
76 | return proxy_key; |
---|
77 | } |
---|
78 | |
---|
79 | public String getFullFormat() { |
---|
80 | Termset dcr = getDCR(); |
---|
81 | String srcformat="", srctype=""; |
---|
82 | if (dcr!=null){ |
---|
83 | srcformat = dcr.getAttr("format"); |
---|
84 | srctype = dcr.getAttr("type"); |
---|
85 | } else { |
---|
86 | srcformat = ALL; |
---|
87 | srctype = "datcat"; |
---|
88 | } |
---|
89 | return srctype + "-" + srcformat + "2" + getFormat(); |
---|
90 | } |
---|
91 | |
---|
92 | public URL getBaseURL() throws MalformedURLException { |
---|
93 | |
---|
94 | if (base_url == null) { |
---|
95 | base_url = new URL(getBaseURI()); |
---|
96 | } |
---|
97 | |
---|
98 | return base_url; |
---|
99 | } |
---|
100 | |
---|
101 | public String getBaseURI() { |
---|
102 | return getDCRAttr("url"); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * uses base_url and (optionally) id to form a url |
---|
107 | * no id => ?list all datacategories? |
---|
108 | * @return the URL to data category xml format |
---|
109 | * @throws Exception |
---|
110 | */ |
---|
111 | public URL getTargetRequest() throws IOException { |
---|
112 | |
---|
113 | URL targetURL = null; |
---|
114 | |
---|
115 | //if (getActionkey().equals(DATCATS)) { |
---|
116 | if ((getQ()!=null && !getQ().equals(""))) { |
---|
117 | //targetURL =new URL(getBaseURI() + "?dcif-mode=all&workingLanguage=" + getLang()) ; |
---|
118 | targetURL =new URL(getBaseURI() + "?dcif-mode=all&workingLanguage=" + getLang()) ; |
---|
119 | } |
---|
120 | /* |
---|
121 | if (getQ()==null || getQ().equals("")) { |
---|
122 | // get all available datcat-sets from terms_setup.xml |
---|
123 | |
---|
124 | targetURL =getBaseURL(); |
---|
125 | } else { |
---|
126 | targetURL =new URL( getBaseURL(), "/" + getQ()); |
---|
127 | } |
---|
128 | */ |
---|
129 | // Admin.notifyUser("DCRProxy.targetURL:" + targetURL); |
---|
130 | |
---|
131 | return targetURL; |
---|
132 | } |
---|
133 | |
---|
134 | public InputStream getSourceStream() throws IOException { |
---|
135 | |
---|
136 | InputStream resultStream = null; |
---|
137 | // if no arguments - get the local-config file |
---|
138 | // FIXME: although this should be competence of TermsProxyAction |
---|
139 | if ((getQ().equals(ALL) || getQ()==null || getQ().equals("")) && (getActionkey().equals(DATCATS))) { |
---|
140 | Admin.notifyUser("serving terms.file:" + Admin.getConfig().getProperty("terms.file")); |
---|
141 | resultStream = this.getClass().getClassLoader().getResourceAsStream(Admin.getConfig().getProperty("terms.file")); |
---|
142 | } else { |
---|
143 | |
---|
144 | /* need to set Request-Property: Accept to xml |
---|
145 | * for to get dcif/xml from DCR-server by means of content negotiation */ |
---|
146 | URLConnection urlConnection = getTargetRequest().openConnection(); |
---|
147 | |
---|
148 | if (getDCRAttr("format").equals("dcif")) { |
---|
149 | urlConnection.setRequestProperty("Accept", "application/dcif+xml"); |
---|
150 | Admin.notifyUser("setting header Accept:" + urlConnection.getRequestProperty("Accept")); |
---|
151 | } |
---|
152 | //urlConnection.setRequestProperty("Accept-Language", getLang()); |
---|
153 | |
---|
154 | // |
---|
155 | resultStream = urlConnection.getInputStream(); |
---|
156 | } |
---|
157 | return resultStream; |
---|
158 | |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|