use of org.eol.globi.service.PropertyEnricherException in project eol-globi-data by jhpoelen.
the class EOLService method getPageId.
protected Long getPageId(String taxonName, boolean shouldFollowAlternate) throws PropertyEnricherException {
try {
URI uri = createSearchURI(taxonName);
String response = getResponse(uri);
Long smallestPageId = null;
if (response != null) {
// pick first of non empty result, assuming that exact match parameter is yielding a valid result
if (!response.contains("totalResults>0<")) {
smallestPageId = findSmallestPageId(response);
} else if (shouldFollowAlternate) {
String[] alternates = response.split("<link rel=\"alternate\" href=\"http://eol.org/api/search/1.0/");
if (alternates.length > 1) {
String[] urlSplit = alternates[1].split("\"");
if (urlSplit.length > 1) {
String alternateTaxonName = urlSplit[0];
try {
String decodedName = URLDecoder.decode(alternateTaxonName, "UTF-8");
decodedName = decodedName.replace("/", "");
if (!decodedName.equals(taxonName)) {
smallestPageId = getPageId(decodedName, false);
}
} catch (UnsupportedEncodingException e) {
throw new PropertyEnricherException("failed to decode [" + alternateTaxonName + "]", e);
}
}
}
}
}
return smallestPageId;
} catch (URISyntaxException e) {
throw new PropertyEnricherException("failed to fetch pageid for [" + taxonName + "]", e);
}
}
use of org.eol.globi.service.PropertyEnricherException in project eol-globi-data by jhpoelen.
the class ResolvingTaxonIndex method resolveAndIndex.
private TaxonNode resolveAndIndex(Taxon origTaxon, Taxon taxon) throws NodeFactoryException {
TaxonNode indexedTaxon = findTaxon(taxon);
while (indexedTaxon == null) {
try {
taxon = TaxonUtil.enrich(enricher, taxon);
} catch (PropertyEnricherException e) {
throw new NodeFactoryException("failed to enrich taxon with name [" + taxon.getName() + "]", e);
}
indexedTaxon = findTaxon(taxon);
if (indexedTaxon == null) {
if (TaxonUtil.isResolved(taxon)) {
indexedTaxon = createAndIndexTaxon(origTaxon, taxon);
} else {
String truncatedName = NodeUtil.truncateTaxonName(taxon.getName());
if (StringUtils.equals(truncatedName, taxon.getName())) {
if (isIndexResolvedOnly()) {
break;
} else {
indexedTaxon = addNoMatchTaxon(origTaxon);
}
} else {
taxon = new TaxonImpl();
taxon.setName(truncatedName);
indexedTaxon = findTaxonByName(taxon.getName());
}
}
}
}
return indexedTaxon;
}
use of org.eol.globi.service.PropertyEnricherException in project eol-globi-data by jhpoelen.
the class EOLService method getResponse.
private String getResponse(URI uri) throws PropertyEnricherException {
HttpGet get = new HttpGet(uri);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String response = null;
try {
response = HttpUtil.executeWithTimer(get, responseHandler);
} catch (HttpResponseException e) {
if (e.getStatusCode() != 406 && e.getStatusCode() != 404) {
throw new PropertyEnricherException("failed to lookup [" + uri.toString() + "]: http status [" + e.getStatusCode() + "] ", e);
}
} catch (ClientProtocolException e) {
throw new PropertyEnricherException("failed to lookup [" + uri.toString() + "]", e);
} catch (IOException e) {
throw new PropertyEnricherException("failed to lookup [" + uri.toString() + "]", e);
}
return response;
}
use of org.eol.globi.service.PropertyEnricherException in project eol-globi-data by jhpoelen.
the class EOLService method addTaxonInfo.
protected void addTaxonInfo(Long pageId, Map<String, String> properties) throws PropertyEnricherException {
try {
URI uri = new URI("http", null, "eol.org", 80, "/api/pages/1.0/" + pageId + ".json", "images=1&videos=0&sounds=0&maps=0&text=0&iucn=false&subjects=overview&licenses=all&details=false&common_names=true&synonyms=false&references=false&taxonomy=true&format=json", null);
String response = getResponse(uri);
if (response != null) {
addCanonicalNamesAndRanks(properties, response);
StringBuilder commonNames = new StringBuilder();
addCommonNames(commonNames, response);
if (commonNames.length() > 0) {
properties.put(PropertyAndValueDictionary.COMMON_NAMES, commonNames.toString());
}
}
} catch (URISyntaxException e) {
throw new PropertyEnricherException("failed to create uri", e);
} catch (JsonProcessingException e) {
throw new PropertyEnricherException("failed to parse response", e);
} catch (IOException e) {
throw new PropertyEnricherException("failed to get response", e);
}
}
use of org.eol.globi.service.PropertyEnricherException in project eol-globi-data by jhpoelen.
the class EOLService method getPageIdFromProvider.
private Long getPageIdFromProvider(int eolProviderId, String providerTaxonId) throws PropertyEnricherException {
Long eolPageId = null;
try {
URI uri1 = new URI("http://eol.org/api/search_by_provider/1.0/" + providerTaxonId + ".json?hierarchy_id=" + eolProviderId);
String response1 = getResponse(uri1);
if (response1 == null) {
throw new PropertyEnricherException("failed to retrieve response for [" + uri1 + "]");
}
JsonNode jsonNode = new ObjectMapper().readTree(response1);
if (jsonNode.isArray() && jsonNode.size() > 0) {
JsonNode jsonNode1 = jsonNode.get(0);
if (jsonNode1.has("eol_page_id")) {
eolPageId = Long.parseLong(jsonNode1.get("eol_page_id").asText());
}
}
} catch (JsonProcessingException ex) {
throw new PropertyEnricherException("failed to create uri", ex);
} catch (URISyntaxException ex) {
throw new PropertyEnricherException("failed to create uri", ex);
} catch (IOException e) {
throw new PropertyEnricherException("failed to get response", e);
} catch (NumberFormatException e) {
throw new PropertyEnricherException("invalid page id", e);
}
return eolPageId;
}
Aggregations