1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package org.bgbm.biovel.drf.query;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.nio.charset.Charset;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.EnumSet;
|
17
|
import java.util.Iterator;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
|
21
|
import org.apache.http.HttpHost;
|
22
|
import org.apache.http.HttpResponse;
|
23
|
import org.apache.http.client.ClientProtocolException;
|
24
|
import org.apache.http.client.HttpClient;
|
25
|
import org.apache.http.client.methods.HttpGet;
|
26
|
import org.apache.http.impl.client.DefaultHttpClient;
|
27
|
import org.apache.http.util.EntityUtils;
|
28
|
import org.bgbm.biovel.drf.checklist.DRFChecklistException;
|
29
|
import org.bgbm.biovel.drf.checklist.SearchMode;
|
30
|
import org.bgbm.biovel.drf.tnr.msg.Query;
|
31
|
import org.bgbm.biovel.drf.tnr.msg.Query.Request;
|
32
|
import org.slf4j.Logger;
|
33
|
import org.slf4j.LoggerFactory;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @date Sep 30, 2015
|
38
|
*
|
39
|
*/
|
40
|
public class RestClient implements IQueryClient{
|
41
|
|
42
|
protected Logger logger = LoggerFactory.getLogger(RestClient.class);
|
43
|
|
44
|
public final static String QUERY_PLACEHOLDER = "{q}";
|
45
|
|
46
|
private final HttpHost httpHost;
|
47
|
|
48
|
public HttpHost getHost() {
|
49
|
return this.httpHost;
|
50
|
}
|
51
|
|
52
|
public RestClient(HttpHost httpHost) {
|
53
|
this.httpHost = httpHost;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Sends a GET request to the REST service specified by the <code>uri</code>
|
58
|
* parameter and returns the response body as <code>String</code>
|
59
|
*
|
60
|
* @param uri
|
61
|
* the REST service uri to send a GET request to
|
62
|
* @return the response body
|
63
|
* @throws DRFChecklistException
|
64
|
*/
|
65
|
public String get(URI uri) throws DRFChecklistException {
|
66
|
HttpClient client = new DefaultHttpClient();
|
67
|
HttpGet request = new HttpGet(uri);
|
68
|
|
69
|
try {
|
70
|
logger.debug(">> Request URI: " + request.getRequestLine().getUri());
|
71
|
HttpResponse response = client.execute(request);
|
72
|
|
73
|
String responseBody = EntityUtils.toString(response.getEntity(),Charset.forName("UTF-8"));
|
74
|
logger.debug("<< Response: " + response.getStatusLine());
|
75
|
logger.trace(responseBody);
|
76
|
logger.trace("==============");
|
77
|
|
78
|
return responseBody;
|
79
|
|
80
|
} catch (ClientProtocolException e1) {
|
81
|
e1.printStackTrace();
|
82
|
// throw new DRFChecklistException(e1);
|
83
|
} catch (IOException e) {
|
84
|
e.printStackTrace();
|
85
|
throw new DRFChecklistException(e);
|
86
|
}
|
87
|
return null;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
*
|
92
|
* @param queryList
|
93
|
* @param endpointSuffix
|
94
|
* @param queryKey
|
95
|
* @param likeModeWildcard the wildcard to add to the query string in case of like search modes
|
96
|
* @param paramMap
|
97
|
* @return
|
98
|
*/
|
99
|
public URI buildUriFromQueryList(List<Query> queryList,
|
100
|
String endpointSuffix,
|
101
|
String queryKey,
|
102
|
String likeModeWildcard,
|
103
|
Map<String, String> paramMap) {
|
104
|
|
105
|
List<String> queries = new ArrayList<String>();
|
106
|
|
107
|
EnumSet<SearchMode> likeModes = EnumSet.of(SearchMode.scientificNameLike);
|
108
|
|
109
|
for(Query query : queryList) {
|
110
|
Request tnrRequest = query.getRequest();
|
111
|
String queryString = tnrRequest.getQueryString();
|
112
|
if(likeModes.contains(SearchMode.valueOf(tnrRequest.getSearchMode()))){
|
113
|
queryString += likeModeWildcard;
|
114
|
}
|
115
|
queries.add(queryString);
|
116
|
}
|
117
|
|
118
|
logger.debug("Query size : " + queries.size());
|
119
|
|
120
|
return buildUriFromQueryStringList(queries,
|
121
|
endpointSuffix,
|
122
|
queryKey,
|
123
|
paramMap);
|
124
|
}
|
125
|
|
126
|
public URI buildUriFromQuery(Query query,
|
127
|
String endpointSuffix,
|
128
|
String queryKey,
|
129
|
Map<String, String> paramMap) {
|
130
|
return buildUriFromQueryString(query.getRequest().getQueryString(),
|
131
|
endpointSuffix,
|
132
|
queryKey,
|
133
|
paramMap);
|
134
|
}
|
135
|
|
136
|
public URI buildUriFromQuery(Query query,
|
137
|
String regexpUrl,
|
138
|
Map<String, String> paramMap) {
|
139
|
String url = regexpUrl.replace(QUERY_PLACEHOLDER, query.getRequest().getQueryString());
|
140
|
return buildUriFromQueryString(url, paramMap);
|
141
|
}
|
142
|
|
143
|
public URI buildUriFromQueryStringList(List<String> queryList,
|
144
|
String endpointSuffix,
|
145
|
String queryKey,
|
146
|
Map<String, String> paramMap) {
|
147
|
|
148
|
RESTURIBuilder builder = new RESTURIBuilder(getHost().getHostName(),
|
149
|
getHost().getPort(),
|
150
|
endpointSuffix,
|
151
|
queryKey,
|
152
|
paramMap);
|
153
|
|
154
|
URI uri = null;
|
155
|
Iterator<String> itrQuery = queryList.iterator();
|
156
|
while(itrQuery.hasNext()) {
|
157
|
builder.addQuery(itrQuery.next());
|
158
|
}
|
159
|
|
160
|
try {
|
161
|
uri = builder.build();
|
162
|
} catch (URISyntaxException e) {
|
163
|
// TODO Auto-generated catch block
|
164
|
//e.printStackTrace();
|
165
|
}
|
166
|
return uri;
|
167
|
}
|
168
|
|
169
|
public URI buildUriFromQueryString(String endpointUrl,
|
170
|
Map<String, String> paramMap) {
|
171
|
|
172
|
RESTURIBuilder builder = new RESTURIBuilder(getHost().getHostName(), getHost().getPort(),endpointUrl, paramMap);
|
173
|
|
174
|
URI uri = null;
|
175
|
|
176
|
try {
|
177
|
uri = builder.build();
|
178
|
} catch (URISyntaxException e) {
|
179
|
// TODO Auto-generated catch block
|
180
|
//e.printStackTrace();
|
181
|
}
|
182
|
return uri;
|
183
|
}
|
184
|
|
185
|
public URI buildUriFromQueryString(String query,
|
186
|
String endpointSuffix,
|
187
|
String queryKey,
|
188
|
Map<String, String> paramMap) {
|
189
|
|
190
|
RESTURIBuilder builder = new RESTURIBuilder(getHost().getHostName(),
|
191
|
getHost().getPort(),
|
192
|
endpointSuffix,
|
193
|
queryKey,
|
194
|
paramMap);
|
195
|
|
196
|
URI uri = null;
|
197
|
|
198
|
builder.addQuery(query);
|
199
|
|
200
|
try {
|
201
|
uri = builder.build();
|
202
|
} catch (URISyntaxException e) {
|
203
|
// TODO Auto-generated catch block
|
204
|
//e.printStackTrace();
|
205
|
}
|
206
|
return uri;
|
207
|
}
|
208
|
|
209
|
|
210
|
|
211
|
}
|