1
|
package org.bgbm.biovel.drf.checklist;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.URI;
|
5
|
import java.net.URISyntaxException;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Iterator;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
|
11
|
import org.apache.http.HttpHost;
|
12
|
import org.apache.http.HttpResponse;
|
13
|
import org.apache.http.client.ClientProtocolException;
|
14
|
import org.apache.http.client.HttpClient;
|
15
|
import org.apache.http.client.methods.HttpGet;
|
16
|
import org.apache.http.impl.client.DefaultHttpClient;
|
17
|
import org.apache.http.util.EntityUtils;
|
18
|
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
|
19
|
import org.bgbm.biovel.drf.utils.JSONUtils;
|
20
|
import org.slf4j.Logger;
|
21
|
import org.slf4j.LoggerFactory;
|
22
|
|
23
|
public abstract class BaseChecklistClient {
|
24
|
|
25
|
protected Logger logger = LoggerFactory.getLogger(BaseChecklistClient.class);
|
26
|
public final static String QUERY_PLACEHOLDER = "{q}";
|
27
|
|
28
|
protected final static String CHECKLIST_KEY = "checklist";
|
29
|
protected final static String CHECKLIST_URL_KEY = "checklist_url";
|
30
|
protected final static String COPYRIGHT_URL_KEY = "copyright_url";
|
31
|
protected final static String CHECKLIST_LIST = "checklist_list";
|
32
|
|
33
|
private ChecklistInfo checklistInfo;
|
34
|
|
35
|
|
36
|
public BaseChecklistClient() {
|
37
|
checklistInfo = buildChecklistInfo();
|
38
|
}
|
39
|
|
40
|
public BaseChecklistClient(String checklistInfoJson) throws DRFChecklistException {
|
41
|
setChecklistInfo(JSONUtils.convertJsonToObject(checklistInfoJson, BaseChecklistClient.ChecklistInfo.class));
|
42
|
}
|
43
|
|
44
|
public ChecklistInfo getChecklistInfo() {
|
45
|
return checklistInfo;
|
46
|
}
|
47
|
|
48
|
public String getChecklistInfoAsJson() throws DRFChecklistException {
|
49
|
|
50
|
if(getChecklistInfo() != null) {
|
51
|
return JSONUtils.convertObjectToJson(checklistInfo);
|
52
|
}
|
53
|
return null;
|
54
|
}
|
55
|
|
56
|
public void setChecklistInfo(ChecklistInfo checklistInfo) {
|
57
|
this.checklistInfo = checklistInfo;
|
58
|
}
|
59
|
|
60
|
|
61
|
public String processRESTService(URI uri) throws DRFChecklistException {
|
62
|
HttpClient client = new DefaultHttpClient();
|
63
|
HttpGet request = new HttpGet(uri);
|
64
|
|
65
|
try {
|
66
|
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
|
67
|
HttpResponse response = client.execute(request);
|
68
|
|
69
|
String strResponse = EntityUtils.toString(response.getEntity());
|
70
|
System.out.println("<< Response: " + response.getStatusLine());
|
71
|
System.out.println(strResponse);
|
72
|
System.out.println("==============");
|
73
|
|
74
|
return strResponse;
|
75
|
} catch (ClientProtocolException e1) {
|
76
|
// TODO Auto-generated catch block
|
77
|
e1.printStackTrace();
|
78
|
} catch (IOException e) {
|
79
|
// TODO Auto-generated catch block
|
80
|
e.printStackTrace();
|
81
|
throw new DRFChecklistException(e);
|
82
|
}
|
83
|
return null;
|
84
|
}
|
85
|
|
86
|
|
87
|
|
88
|
public void queryChecklist(TnrMsg tnrMsg) throws DRFChecklistException {
|
89
|
resolveNames(tnrMsg);
|
90
|
}
|
91
|
|
92
|
public TnrMsg queryChecklist(List<TnrMsg> tnrMsgs) throws DRFChecklistException {
|
93
|
|
94
|
TnrMsg finalTnrMsg = new TnrMsg();
|
95
|
Iterator<TnrMsg> itrTnrMsg = tnrMsgs.iterator();
|
96
|
while(itrTnrMsg.hasNext()) {
|
97
|
Iterator<TnrMsg.Query> itrQuery = itrTnrMsg.next().getQuery().iterator();
|
98
|
while(itrQuery.hasNext()) {
|
99
|
finalTnrMsg.getQuery().add(itrQuery.next());
|
100
|
}
|
101
|
}
|
102
|
|
103
|
resolveNames(finalTnrMsg);
|
104
|
|
105
|
return finalTnrMsg;
|
106
|
}
|
107
|
|
108
|
|
109
|
public URI buildUriFromQueryStringList(List<String> queryList,
|
110
|
String endpointSuffix,
|
111
|
String queryKey,
|
112
|
Map<String, String> paramMap) {
|
113
|
|
114
|
ChecklistURIBuilder builder = new ChecklistURIBuilder(getHost().getHostName(),
|
115
|
endpointSuffix,
|
116
|
queryKey,
|
117
|
paramMap);
|
118
|
|
119
|
URI uri = null;
|
120
|
Iterator<String> itrQuery = queryList.iterator();
|
121
|
while(itrQuery.hasNext()) {
|
122
|
builder.addQuery(itrQuery.next());
|
123
|
}
|
124
|
|
125
|
try {
|
126
|
uri = builder.build();
|
127
|
} catch (URISyntaxException e) {
|
128
|
// TODO Auto-generated catch block
|
129
|
//e.printStackTrace();
|
130
|
}
|
131
|
return uri;
|
132
|
}
|
133
|
|
134
|
public URI buildUriFromQueryString(String query,
|
135
|
String endpointSuffix,
|
136
|
String queryKey,
|
137
|
Map<String, String> paramMap) {
|
138
|
|
139
|
ChecklistURIBuilder builder = new ChecklistURIBuilder(getHost().getHostName(),
|
140
|
endpointSuffix,
|
141
|
queryKey,
|
142
|
paramMap);
|
143
|
|
144
|
URI uri = null;
|
145
|
|
146
|
builder.addQuery(query);
|
147
|
|
148
|
try {
|
149
|
uri = builder.build();
|
150
|
} catch (URISyntaxException e) {
|
151
|
// TODO Auto-generated catch block
|
152
|
//e.printStackTrace();
|
153
|
}
|
154
|
return uri;
|
155
|
}
|
156
|
|
157
|
public URI buildUriFromQueryString(String endpointUrl,
|
158
|
Map<String, String> paramMap) {
|
159
|
|
160
|
ChecklistURIBuilder builder = new ChecklistURIBuilder(getHost().getHostName(), endpointUrl, paramMap);
|
161
|
|
162
|
URI uri = null;
|
163
|
|
164
|
try {
|
165
|
uri = builder.build();
|
166
|
} catch (URISyntaxException e) {
|
167
|
// TODO Auto-generated catch block
|
168
|
//e.printStackTrace();
|
169
|
}
|
170
|
return uri;
|
171
|
}
|
172
|
|
173
|
public URI buildUriFromQueryList(List<TnrMsg.Query> queryList,
|
174
|
String endpointSuffix,
|
175
|
String queryKey,
|
176
|
Map<String, String> paramMap) {
|
177
|
List<String> queries = new ArrayList<String>();
|
178
|
Iterator<TnrMsg.Query> itrQuery = queryList.iterator();
|
179
|
while(itrQuery.hasNext()) {
|
180
|
queries.add(itrQuery.next().getTnrRequest().getTaxonName().getName().getNameComplete());
|
181
|
}
|
182
|
System.out.println("Query size : " + queries.size());
|
183
|
return buildUriFromQueryStringList(queries,
|
184
|
endpointSuffix,
|
185
|
queryKey,
|
186
|
paramMap);
|
187
|
}
|
188
|
|
189
|
public URI buildUriFromQuery(TnrMsg.Query query,
|
190
|
String endpointSuffix,
|
191
|
String queryKey,
|
192
|
Map<String, String> paramMap) {
|
193
|
return buildUriFromQueryString(query.getTnrRequest().getTaxonName().getName().getNameComplete(),
|
194
|
endpointSuffix,
|
195
|
queryKey,
|
196
|
paramMap);
|
197
|
}
|
198
|
|
199
|
public URI buildUriFromQuery(TnrMsg.Query query,
|
200
|
String regexpUrl,
|
201
|
Map<String, String> paramMap) {
|
202
|
String url = regexpUrl.replace(QUERY_PLACEHOLDER, query.getTnrRequest().getTaxonName().getName().getNameComplete());
|
203
|
return buildUriFromQueryString(url, paramMap);
|
204
|
}
|
205
|
|
206
|
protected abstract ChecklistInfo buildChecklistInfo();
|
207
|
|
208
|
public abstract void resolveNames(TnrMsg tnrMsg) throws DRFChecklistException;
|
209
|
|
210
|
public abstract HttpHost getHost();
|
211
|
|
212
|
public abstract int getMaxPageSize();
|
213
|
|
214
|
public static class ChecklistInfo {
|
215
|
|
216
|
private String id;
|
217
|
private String label;
|
218
|
private String url;
|
219
|
private String copyrightUrl;
|
220
|
private boolean use = false;
|
221
|
private List<ChecklistInfo> subChecklists = null;
|
222
|
|
223
|
public ChecklistInfo() {
|
224
|
}
|
225
|
|
226
|
public ChecklistInfo(String id, String label, String url) {
|
227
|
this(id,label,url,"");
|
228
|
}
|
229
|
|
230
|
public ChecklistInfo(String id, String label, String url, String copyrightUrl) {
|
231
|
this(id,label,url,copyrightUrl,false);
|
232
|
}
|
233
|
|
234
|
public ChecklistInfo(String id, String label, String url, String copyrightUrl, boolean use) {
|
235
|
this.id = id;
|
236
|
this.label = label;
|
237
|
this.url = url;
|
238
|
this.copyrightUrl = copyrightUrl;
|
239
|
this.use = use;
|
240
|
subChecklists = null;
|
241
|
}
|
242
|
|
243
|
public String getId() {
|
244
|
return id;
|
245
|
}
|
246
|
|
247
|
public String getLabel() {
|
248
|
return label;
|
249
|
}
|
250
|
|
251
|
public String getUrl() {
|
252
|
return url;
|
253
|
}
|
254
|
|
255
|
public String getCopyrightUrl() {
|
256
|
return copyrightUrl;
|
257
|
}
|
258
|
|
259
|
public boolean getUse() {
|
260
|
return use;
|
261
|
}
|
262
|
|
263
|
public void addSubChecklist(ChecklistInfo ci) {
|
264
|
if(subChecklists == null) {
|
265
|
subChecklists = new ArrayList<ChecklistInfo>();
|
266
|
}
|
267
|
subChecklists.add(ci);
|
268
|
}
|
269
|
|
270
|
public List<ChecklistInfo> getSubChecklists() {
|
271
|
return subChecklists;
|
272
|
}
|
273
|
|
274
|
public static ChecklistInfo create(String[] ciArray) throws DRFChecklistException {
|
275
|
if(ciArray.length != 4) {
|
276
|
throw new DRFChecklistException("Not correct number of elements to create Checklist Info");
|
277
|
}
|
278
|
return new ChecklistInfo(ciArray[0],ciArray[1],ciArray[2],ciArray[3]);
|
279
|
}
|
280
|
|
281
|
|
282
|
}
|
283
|
|
284
|
}
|