Project

General

Profile

« Previous | Next » 

Revision c5e6fc74

Added by Andreas Kohlbecker over 8 years ago

separating query protocol implementation from checklist clients

View differences:

src/main/java/org/bgbm/biovel/drf/checklist/GBIFBackboneClient.java
10 10

  
11 11
import org.apache.http.HttpHost;
12 12
import org.apache.http.client.utils.URIBuilder;
13
import org.bgbm.biovel.drf.rest.ServiceProviderInfo;
13
import org.bgbm.biovel.drf.client.ServiceProviderInfo;
14
import org.bgbm.biovel.drf.query.RestClient;
14 15
import org.bgbm.biovel.drf.tnr.msg.Classification;
15 16
import org.bgbm.biovel.drf.tnr.msg.Query;
17
import org.bgbm.biovel.drf.tnr.msg.Response;
16 18
import org.bgbm.biovel.drf.tnr.msg.Source;
17 19
import org.bgbm.biovel.drf.tnr.msg.Synonym;
18 20
import org.bgbm.biovel.drf.tnr.msg.Taxon;
19 21
import org.bgbm.biovel.drf.tnr.msg.TaxonName;
20 22
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
21
import org.bgbm.biovel.drf.tnr.msg.Response;
22 23
import org.bgbm.biovel.drf.utils.JSONUtils;
23 24
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
24 25
import org.json.simple.JSONArray;
25 26
import org.json.simple.JSONObject;
26 27

  
27
public class GBIFBackboneClient extends AggregateChecklistClient {
28
public class GBIFBackboneClient extends AggregateChecklistClient<RestClient> {
28 29

  
30
    /**
31
     *
32
     */
33
    private static final HttpHost HTTP_HOST = new HttpHost("api.gbif.org",80);
29 34
    public static final String ID = "gbif";
30 35
    public static final String LABEL = "GBIF Checklist Bank";
31 36
    public static final String URL = "http://uat.gbif.org/developer/species";
32 37
    public static final String DATA_AGR_URL = "http://data.gbif.org/tutorial/datauseagreement";
33 38
    private static final String MAX_PAGING_LIMIT = "1000";
34
    private static final String VERSION = "v0.9";
39
    private static final String VERSION = "v1";
35 40
    public static final ServiceProviderInfo CINFO = new ServiceProviderInfo(ID,LABEL,ServiceProviderInfo.DEFAULT_SEARCH_MODE,URL,DATA_AGR_URL, VERSION);
36 41

  
37 42
    public static final EnumSet<SearchMode> SEARCH_MODES = EnumSet.of(SearchMode.scientificNameExact);
......
49 54
    }
50 55

  
51 56
    @Override
52
    public HttpHost getHost() {
53
        // TODO Auto-generated method stub
54
        return new HttpHost("api.gbif.org",80);
57
    public void initQueryClient() {
58
        queryClient = new RestClient(HTTP_HOST);
55 59
    }
56 60

  
57 61

  
......
62 66

  
63 67
        URIBuilder uriBuilder = new URIBuilder();
64 68
        uriBuilder.setScheme("http");
65
        uriBuilder.setHost(getHost().getHostName());
69
        uriBuilder.setHost(HTTP_HOST.getHostName());
66 70
        uriBuilder.setPath("/" + checklistInfo.getVersion() + "/dataset/search");
67 71
        uriBuilder.setParameter("type", "CHECKLIST");
68 72
        uriBuilder.setParameter("limit", MAX_PAGING_LIMIT);
......
74 78
                uriBuilder.setParameter("offset", Integer.toString(offset));
75 79
                uri = uriBuilder.build();
76 80
                System.out.println("buildChecklistMap");
77
                String responseBody = processRESTService(uri);
81
                String responseBody = queryClient.processRESTService(uri);
78 82

  
79
                JSONObject jsonResponse = (JSONObject) JSONUtils.parseJsonToObject(responseBody);
83
                JSONObject jsonResponse = JSONUtils.parseJsonToObject(responseBody);
80 84
                JSONArray results = (JSONArray) jsonResponse.get("results");
81 85
                Iterator<JSONObject> itrResults = results.iterator();
82 86
                while(itrResults.hasNext()) {
......
120 124
            paramMap.put("datasetKey", checklistInfo.getId());
121 125
            paramMap.put("limit", MAX_PAGING_LIMIT);
122 126

  
123
            URI namesUri = buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species",	"name", paramMap);
127
            URI namesUri = queryClient.buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species",	"name", paramMap);
124 128

  
125
            String responseBody = processRESTService(namesUri);
129
            String responseBody = queryClient.processRESTService(namesUri);
126 130

  
127 131
            updateQueryWithResponse(query,responseBody, paramMap, checklistInfo);
128 132
        }
......
138 142
            Map<String, String> paramMap,
139 143
            ServiceProviderInfo ci) throws DRFChecklistException {
140 144

  
141
        JSONObject jsonResponse = (JSONObject) JSONUtils.parseJsonToObject(response);
145
        JSONObject jsonResponse = JSONUtils.parseJsonToObject(response);
142 146
        JSONArray results = (JSONArray) jsonResponse.get("results");
143 147

  
144 148
        if(results != null) {
......
165 169
                    Long key = (Long)res.get("acceptedKey");
166 170
                    accTaxonId = key.toString();
167 171

  
168
                    URI taxonUri = buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species/" + accTaxonId, null);
169
                    String responseBody = processRESTService(taxonUri);
172
                    URI taxonUri = queryClient.buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species/" + accTaxonId, null);
173
                    String responseBody = queryClient.processRESTService(taxonUri);
170 174

  
171
                    JSONObject taxon = (JSONObject) JSONUtils.parseJsonToObject(responseBody);
175
                    JSONObject taxon = JSONUtils.parseJsonToObject(responseBody);
172 176
                    Taxon accName = generateAccName(taxon);
173 177
                    tnrResponse.setTaxon(accName);
174 178

  
......
188 192
                do {
189 193
                    paramMap.put("offset", Integer.toString(offset));
190 194

  
191
                    URI synonymsUri = buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species/" + accTaxonId + "/synonyms", paramMap);
192
                    String synResponse = processRESTService(synonymsUri);
195
                    URI synonymsUri = queryClient.buildUriFromQuery(query, "/" + CINFO.getVersion() + "/species/" + accTaxonId + "/synonyms", paramMap);
196
                    String synResponse = queryClient.processRESTService(synonymsUri);
193 197

  
194
                    JSONObject pagedSynonyms = (JSONObject) JSONUtils.parseJsonToObject(synResponse);
198
                    JSONObject pagedSynonyms = JSONUtils.parseJsonToObject(synResponse);
195 199
                    generateSynonyms(pagedSynonyms, tnrResponse);
196 200

  
197 201
                    endOfRecords = (Boolean) pagedSynonyms.get("endOfRecords");
......
256 260
        Iterator<JSONObject> itrSynonyms = synonyms.iterator();
257 261
        while(itrSynonyms.hasNext()) {
258 262
            Synonym synonym = new Synonym();
259
            JSONObject synonymjs = (JSONObject) itrSynonyms.next();
263
            JSONObject synonymjs = itrSynonyms.next();
260 264
            TaxonName taxonName = new TaxonName();
261 265

  
262 266
            String resName = (String) synonymjs.get("scientificName");
......
297 301
            //Add a comment to this line
298 302
            URIBuilder uriBuilder = new URIBuilder();
299 303
            uriBuilder.setScheme("http");
300
            uriBuilder.setHost(getHost().getHostName());
304
            uriBuilder.setHost(HTTP_HOST.getHostName());
301 305
            uriBuilder.setPath("/" + CINFO.getVersion() + "/dataset/" + datasetKey);
302 306

  
303 307
            URI uri = uriBuilder.build();
304
            String responseBody = processRESTService(uri);
308
            String responseBody = queryClient.processRESTService(uri);
305 309

  
306
            JSONObject datasetInfo = (JSONObject) JSONUtils.parseJsonToObject(responseBody);
310
            JSONObject datasetInfo = JSONUtils.parseJsonToObject(responseBody);
307 311

  
308 312
            return  (String) datasetInfo.get("title");
309 313
        }

Also available in: Unified diff