1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.IOException; |
---|
7 | import java.io.InputStream; |
---|
8 | import java.io.InputStreamReader; |
---|
9 | import java.io.StringWriter; |
---|
10 | import java.io.UnsupportedEncodingException; |
---|
11 | import java.net.MalformedURLException; |
---|
12 | import java.net.URL; |
---|
13 | import java.util.HashMap; |
---|
14 | import java.util.Map; |
---|
15 | |
---|
16 | import javax.xml.parsers.ParserConfigurationException; |
---|
17 | import javax.xml.transform.TransformerConfigurationException; |
---|
18 | import javax.xml.transform.TransformerException; |
---|
19 | import javax.xml.transform.TransformerFactory; |
---|
20 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
21 | import javax.xml.transform.dom.DOMSource; |
---|
22 | import javax.xml.transform.stream.StreamResult; |
---|
23 | |
---|
24 | import org.w3c.dom.Document; |
---|
25 | import org.w3c.dom.Node; |
---|
26 | |
---|
27 | import eu.clarin.cmdi.mdservice.internal.CQLParseException; |
---|
28 | import eu.clarin.cmdi.mdservice.internal.Cache; |
---|
29 | import eu.clarin.cmdi.mdservice.internal.NoStylesheetException; |
---|
30 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
31 | import eu.clarin.cmdi.mdservice.model.Diagnostic; |
---|
32 | import eu.clarin.cmdi.mdservice.model.FCSRepositories; |
---|
33 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
34 | import eu.clarin.cmdi.mdservice.model.Repositories; |
---|
35 | import eu.clarin.cmdi.mdservice.model.WorkspaceProfile; |
---|
36 | import eu.clarin.cmdi.mdservice.proxy.FCSProxy; |
---|
37 | import eu.clarin.cmdi.mdservice.proxy.MDRepoProxy; |
---|
38 | import eu.clarin.cmdi.mdservice.proxy.Pz2Proxy; |
---|
39 | import eu.clarin.cmdi.mdservice.proxy.SRUProxy; |
---|
40 | import eu.clarin.cmdi.mdservice.proxy.WorkspaceProxy; |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | public class FCSAction extends SRUAction |
---|
46 | { |
---|
47 | |
---|
48 | private static final long serialVersionUID = 1L; |
---|
49 | |
---|
50 | /** |
---|
51 | * Properties to be filled by Struts with according request-parameters |
---|
52 | */ |
---|
53 | /* |
---|
54 | private String q; |
---|
55 | private String version; |
---|
56 | private String operation; |
---|
57 | private String query; |
---|
58 | private String scanClause; |
---|
59 | private int startRecord; |
---|
60 | private int maximumRecords; |
---|
61 | private String recordPacking; |
---|
62 | private String recordSchema; |
---|
63 | private String recordXPath; |
---|
64 | private int resultSetTTL; |
---|
65 | private String sortKeys; |
---|
66 | private String stylesheet; |
---|
67 | private String x_cmd_repository; |
---|
68 | private String x_cmd_collections; |
---|
69 | private Set paramSet; |
---|
70 | */ |
---|
71 | private String x_content; |
---|
72 | private Map<String,String[]> fcsparams; |
---|
73 | |
---|
74 | private static int OP_SEARCHRETRIEVE = 1; |
---|
75 | private static int OP_EXPLAIN = 2; |
---|
76 | private static int OP_SCAN = 3; |
---|
77 | |
---|
78 | |
---|
79 | //TODO cache after analysis ? |
---|
80 | @Override |
---|
81 | public String getCache() { |
---|
82 | return Cache.SKIP; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | @Override |
---|
87 | public String getRepository() { |
---|
88 | if (getParam("operation").equals("searchRetrieve") && !getParam("x-context").equals("")){ |
---|
89 | if (FCSRepositories.getFCSRepositories().getFCSMapping(params.get("x-context")[0]) == null){ |
---|
90 | return getParam("x-context"); |
---|
91 | } else { |
---|
92 | return FCSRepositories.getFCSRepositories().getFCSMapping(params.get("x-context")[0]); |
---|
93 | } |
---|
94 | } |
---|
95 | return getParam("x-context"); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * primitive identification of the target-proxy |
---|
102 | * base for finding the right base_url in the props |
---|
103 | * subclass has to override with its specific proxykey |
---|
104 | * @return |
---|
105 | */ |
---|
106 | public String getProxyKey() { |
---|
107 | return ""; |
---|
108 | } |
---|
109 | |
---|
110 | @Override |
---|
111 | public String getFullFormat() { |
---|
112 | return getActionkey() + getOperation() + "2" + getFormat(); |
---|
113 | } |
---|
114 | |
---|
115 | public void addFCSParam(String key, String value){ |
---|
116 | String[] sarr = new String[1]; |
---|
117 | sarr[0] = value; |
---|
118 | fcsparams.put(key, sarr); |
---|
119 | } |
---|
120 | public Map<String, String[]> getFCSParams(){ |
---|
121 | if (fcsparams == null){ |
---|
122 | |
---|
123 | fcsparams = new HashMap<String, String[]>(); |
---|
124 | |
---|
125 | if ( params.get("operation") != null){ |
---|
126 | addFCSParam("operation",params.get("operation")[0]); |
---|
127 | } |
---|
128 | if ( params.get("scanClause") != null){ |
---|
129 | addFCSParam("scanClause",params.get("scanClause")[0]); |
---|
130 | } |
---|
131 | if ( params.get("query") != null){ |
---|
132 | addFCSParam("query",params.get("query")[0]); |
---|
133 | } |
---|
134 | if ( params.get("x-context") != null){ |
---|
135 | /*if (WorkspaceProfile.getFCSMapping(params.get("x-context")[0]) != null){ |
---|
136 | addFCSParam("x-context",params.get("x-context")[0]); |
---|
137 | } |
---|
138 | */ |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | return fcsparams; |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | @Override |
---|
149 | public String getFormat() { |
---|
150 | if (getParams().get("x-format") == null){ |
---|
151 | return "xml"; |
---|
152 | } |
---|
153 | return getParam("x-format"); |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | @Override |
---|
158 | protected void setDefaultParams(){ |
---|
159 | // set defaults |
---|
160 | |
---|
161 | if (params.get("x-context") != null){ |
---|
162 | |
---|
163 | // not for scan operation , search in fcs-mapping |
---|
164 | if (!params.get("operation")[0].equals("scan")){ |
---|
165 | if (FCSRepositories.getFCSRepositories().getFCSMapping(params.get("x-context")[0]) == null){ |
---|
166 | addParam("repository",params.get("x-context")[0]); |
---|
167 | } else { |
---|
168 | addParam("repository",FCSRepositories.getFCSRepositories().getFCSMapping(params.get("x-context")[0])); |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | addParam("fullformat",getFullFormat()); |
---|
174 | |
---|
175 | if (params.get("scripts_url") == null){ |
---|
176 | addParam("scripts_url",Utils.getConfig("webscripts.uri")); |
---|
177 | } |
---|
178 | |
---|
179 | if (params.get("base_url") == null){ |
---|
180 | addParam("base_url",Utils.getConfig("base.uri")); |
---|
181 | } |
---|
182 | if (params.get("cache") == null){ |
---|
183 | addParam("cache",Cache.SKIP); |
---|
184 | } |
---|
185 | |
---|
186 | if ((getRepository().equals("")) && (getParam("operation").equals("scan"))){ |
---|
187 | addParam("part","repositories"); |
---|
188 | addParam("extradata","fcs.resource"); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | @Override |
---|
193 | public boolean checkTargetProxy(){ |
---|
194 | return false; |
---|
195 | } |
---|
196 | |
---|
197 | @Override |
---|
198 | public void checkParams(){ |
---|
199 | if (getTargetProxy() != null){ |
---|
200 | getTargetProxy().checkParams(); |
---|
201 | } |
---|
202 | } |
---|
203 | @Override |
---|
204 | public String getUserName(){ |
---|
205 | |
---|
206 | return WorkspaceProfile.SERVER; |
---|
207 | } |
---|
208 | @Override |
---|
209 | public void setTargetProxy(){ |
---|
210 | |
---|
211 | //Query query = new Query(); |
---|
212 | //TODO case scan,explain |
---|
213 | |
---|
214 | if ((getRepository().equals("")) && (getParam("operation").equals("scan"))){ |
---|
215 | setTargetProxy(new WorkspaceProxy()); |
---|
216 | return; |
---|
217 | } |
---|
218 | |
---|
219 | switch (Repositories.RepositoryType.toRepositoryType(Repositories.getRepositories().getRepositoryType(getRepository()))) |
---|
220 | { |
---|
221 | case PAZPAR: |
---|
222 | setTargetProxy(new Pz2Proxy(new Query(getActionkey(), this.getParams()))); |
---|
223 | return; |
---|
224 | case SRU: |
---|
225 | setTargetProxy(new SRUProxy(new Query(Query.SRUEXTERN,getParams()))); |
---|
226 | return; |
---|
227 | case MD: |
---|
228 | setTargetProxy(new MDRepoProxy(new Query(Query.RECORDSET,getParams()))); |
---|
229 | return; |
---|
230 | case FCS: |
---|
231 | setTargetProxy(new FCSProxy(new Query(Query.FCSRECORDSET,getFCSParams()))); |
---|
232 | return; |
---|
233 | case FCSRESOURCE: |
---|
234 | setTargetProxy(new FCSProxy(new Query(Query.FCSRECORDSET,getFCSParams()))); |
---|
235 | return; |
---|
236 | default: |
---|
237 | //TODO error = bad workspaceProfile configuration |
---|
238 | getDiagnostics().Add(Diagnostic.SYSTEM_ERROR, "Error in workspaceprofile configuration", "repository=" + getRepository() ); |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|