Search in sources :

Example 6 with PropertyEnricherException

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);
    }
}
Also used : PropertyEnricherException(org.eol.globi.service.PropertyEnricherException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 7 with PropertyEnricherException

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;
}
Also used : PropertyEnricherException(org.eol.globi.service.PropertyEnricherException) TaxonNode(org.eol.globi.domain.TaxonNode) NodeFactoryException(org.eol.globi.data.NodeFactoryException) TaxonImpl(org.eol.globi.domain.TaxonImpl)

Example 8 with PropertyEnricherException

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;
}
Also used : PropertyEnricherException(org.eol.globi.service.PropertyEnricherException) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 9 with PropertyEnricherException

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);
    }
}
Also used : PropertyEnricherException(org.eol.globi.service.PropertyEnricherException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 10 with PropertyEnricherException

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;
}
Also used : PropertyEnricherException(org.eol.globi.service.PropertyEnricherException) JsonNode(org.codehaus.jackson.JsonNode) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

PropertyEnricherException (org.eol.globi.service.PropertyEnricherException)12 IOException (java.io.IOException)6 Taxon (org.eol.globi.domain.Taxon)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 TaxonNode (org.eol.globi.domain.TaxonNode)3 LabeledCSVParser (com.Ostermiller.util.LabeledCSVParser)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)2 TaxonImpl (org.eol.globi.domain.TaxonImpl)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1