Search in sources :

Example 86 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method sample.

/**
     * Get records for this query for the provided fields.
     *
     * @param fields QueryFields to return in the sample.
     * @return records as String in CSV format.
     */
@Override
public String sample(List<QueryField> fields) {
    HttpClient client = new HttpClient();
    String url = biocacheServer + SAMPLING_SERVICE_CSV_GZIP + DEFAULT_ROWS + "&q=" + getQ() + paramQueryFields(fields).replace("&fl=", "&fields=") + getQc();
    LOGGER.debug(url);
    GetMethod get = new GetMethod(url);
    String sample = null;
    long start = System.currentTimeMillis();
    try {
        client.executeMethod(get);
        sample = decompressZip(get.getResponseBodyAsStream());
        //in the first line do field name replacement
        for (QueryField f : fields) {
            String t = translateFieldForSolr(f.getName());
            if (!f.getName().equals(t)) {
                sample = sample.replaceFirst(t, f.getName());
            }
        }
    } catch (Exception e) {
        LOGGER.error("error sampling", e);
    }
    LOGGER.debug("get sample in " + (System.currentTimeMillis() - start) + "ms");
    return sample;
}
Also used : QueryField(au.org.ala.legend.QueryField) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 87 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method getGuid.

/**
     * Performs a scientific name or common name lookup and returns the guid if it exists in the BIE
     * <p/>
     * TODO Move getGuid and getClassification to BIE Utilities...
     *
     * @param name
     * @return
     */
public static String getGuid(String name) {
    if ("true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie"))) {
        String url = CommonData.getBieServer() + "/ws/species/lookup/bulk";
        try {
            HttpClient client = new HttpClient();
            PostMethod get = new PostMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            get.setRequestEntity(new StringRequestEntity("{\"names\":[\"" + name.replace("\"", "\\\"") + "\"]}"));
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else if (jo != null && jo.containsKey("acceptedIdentifierGuid") && jo.get("acceptedIdentifierGuid") != null) {
                    return jo.get("acceptedIdentifierGuid").toString();
                } else if (jo != null && jo.containsKey("acceptedConceptID") && jo.get("acceptedConceptID") != null) {
                    return jo.get("acceptedConceptID").toString();
                } else if (jo != null && jo.containsKey("guid") && jo.get("guid") != null) {
                    return jo.get("guid").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, e);
            return null;
        }
    } else {
        String url = CommonData.getBieServer() + "/ws/guid/" + name.replaceAll(" ", "%20");
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, e);
            return null;
        }
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 88 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method initDownloadReasons.

public static void initDownloadReasons() {
    copyDownloadReasons = null;
    LOGGER.debug("CommonData::initDownloadReasons()");
    String url = CommonData.getSettings().getProperty("logger.url") + "/service/logger/reasons";
    try {
        LOGGER.debug(url);
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        int result = client.executeMethod(get);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            copyDownloadReasons = (JSONArray) jp.parse(get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        copyDownloadReasons = null;
        LOGGER.error("error getting reasons: " + url, e);
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) Point(com.vividsolutions.jts.geom.Point)

Example 89 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method initJournalmap.

private static void initJournalmap() {
    if (journalMapArticles != null && journalMapArticles.size() > 0) {
        return;
    }
    journalMapArticles = new ArrayList<JSONObject>();
    journalMapLocations = new ArrayList<JournalMapLocation>();
    try {
        String journalmapUrl = CommonData.getSettings().getProperty("journalmap.url", null);
        String journalmapKey = CommonData.getSettings().getProperty("journalmap.api_key", null);
        //try disk cache
        File jaFile = new File("/data/webportal/journalmapArticles.json");
        if (jaFile.exists()) {
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(FileUtils.readFileToString(jaFile));
            for (int i = 0; i < ja.size(); i++) {
                journalMapArticles.add((JSONObject) ja.get(i));
            }
        } else if (journalmapKey != null && !journalmapKey.isEmpty()) {
            int page = 1;
            int maxpage = 0;
            List<String> publicationsIds = new ArrayList<String>();
            while (page == 1 || page <= maxpage) {
                HttpClient client = new HttpClient();
                String url = journalmapUrl + "api/publications.json?version=1.0&key=" + journalmapKey + "&page=" + page;
                page = page + 1;
                LOGGER.debug("journalmap url: " + url);
                GetMethod get = new GetMethod(url);
                int result = client.executeMethod(get);
                //update maxpage
                maxpage = Integer.parseInt(get.getResponseHeader("X-Pages").getValue());
                //cache
                JSONParser jp = new JSONParser();
                JSONArray jcollection = (JSONArray) jp.parse(get.getResponseBodyAsString());
                for (int i = 0; i < jcollection.size(); i++) {
                    if (((JSONObject) jcollection.get(i)).containsKey("id")) {
                        publicationsIds.add(((JSONObject) jcollection.get(i)).get("id").toString());
                        LOGGER.debug("found publication: " + ((JSONObject) jcollection.get(i)).get("id").toString() + ", article_count: " + ((JSONObject) jcollection.get(i)).get("articles_count").toString());
                    }
                }
            }
            for (String publicationsId : publicationsIds) {
                //allow for collection failure
                try {
                    page = 1;
                    maxpage = 0;
                    while (page == 1 || page <= maxpage) {
                        HttpClient client = new HttpClient();
                        String url = journalmapUrl + "api/articles.json?version=1.0&key=" + journalmapKey + "&page=" + page + "&publication_id=" + publicationsId;
                        page = page + 1;
                        LOGGER.debug("journalmap url: " + url);
                        GetMethod get = new GetMethod(url);
                        int result = client.executeMethod(get);
                        //update maxpage
                        maxpage = Integer.parseInt(get.getResponseHeader("X-Pages").getValue());
                        //cache
                        JSONParser jp = new JSONParser();
                        JSONArray jarticles = (JSONArray) jp.parse(get.getResponseBodyAsString());
                        for (int j = 0; j < jarticles.size(); j++) {
                            JSONObject o = (JSONObject) jarticles.get(j);
                            if (o.containsKey("locations")) {
                                journalMapArticles.add(o);
                            }
                        }
                    }
                } catch (Exception e) {
                    LOGGER.error("journalmap - failure to get articles from publicationsId: " + publicationsId);
                }
            }
            //save to disk cache
            FileWriter fw = new FileWriter(jaFile);
            JSONValue.writeJSONString(journalMapArticles, fw);
            fw.flush();
            fw.close();
        }
    } catch (Exception e) {
        LOGGER.error("error initialising journalmap data", e);
    }
    //construct locations list
    for (int i = 0; i < journalMapArticles.size(); i++) {
        JSONArray locations = (JSONArray) journalMapArticles.get(i).get("locations");
        for (int j = 0; j < locations.size(); j++) {
            JSONObject l = (JSONObject) locations.get(j);
            double longitude = Double.parseDouble(l.get("longitude").toString());
            double latitude = Double.parseDouble(l.get("latitude").toString());
            journalMapLocations.add(new JournalMapLocation(longitude, latitude, i));
        }
    }
}
Also used : JSONArray(org.json.simple.JSONArray) Point(com.vividsolutions.jts.geom.Point) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 90 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method getScientificNameRank.

public static String getScientificNameRank(String lsid) {
    String snUrl = "true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie")) ? CommonData.getBieServer() + BIE_SPECIES_WS + lsid + ".json" : CommonData.getBieServer() + BIE_SPECIES + lsid + ".json";
    LOGGER.debug(snUrl);
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(snUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(slist);
        String scientficName = ((JSONObject) jo.get("taxonConcept")).get("nameString").toString();
        String r = ((JSONObject) jo.get("taxonConcept")).get("rankString").toString();
        LOGGER.debug("Arrays.binarySearch(COMMON_TAXON_RANKS, rank): " + Arrays.binarySearch(COMMON_TAXON_RANKS, r));
        if (Arrays.binarySearch(COMMON_TAXON_RANKS, r) > -1) {
            r = StringConstants.TAXON;
        }
        return scientficName + "," + r;
    } catch (Exception e) {
        LOGGER.error("error getting scientific name:" + snUrl, e);
    }
    return StringConstants.OCCURRENCES;
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20