Search in sources :

Example 76 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Ebselen by Ardesco.

the class FileDownloader method downloader.

public String downloader(WebElement element, String attribute) throws Exception {
    //Assuming that getAttribute does some magic to return a fully qualified URL
    String downloadLocation = element.getAttribute(attribute);
    if (downloadLocation.trim().equals("")) {
        throw new Exception("The element you have specified does not link to anything!");
    }
    URL downloadURL = new URL(downloadLocation);
    HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
    client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort()));
    client.setState(mimicCookieState(driver.manage().getCookies()));
    HttpMethod getRequest = new GetMethod(downloadURL.getPath());
    FileHandler downloadedFile = new FileHandler(downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true);
    try {
        int status = client.executeMethod(getRequest);
        LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm());
        BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream());
        int offset = 0;
        int len = 4096;
        int bytes = 0;
        byte[] block = new byte[len];
        while ((bytes = in.read(block, offset, len)) > -1) {
            downloadedFile.getWritableFileOutputStream().write(block, 0, bytes);
        }
        downloadedFile.close();
        in.close();
        LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile());
    } catch (Exception Ex) {
        LOGGER.error("Download failed: {}", Ex);
        throw new Exception("Download failed!");
    } finally {
        getRequest.releaseConnection();
    }
    return downloadedFile.getAbsoluteFile();
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 77 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project bigbluebutton by bigbluebutton.

the class PresentationUrlDownloadService method savePresentation.

public boolean savePresentation(final String meetingId, final String filename, final String urlString) {
    String finalUrl = followRedirect(meetingId, urlString, 0, urlString);
    if (finalUrl == null)
        return false;
    boolean success = false;
    GetMethod method = new GetMethod(finalUrl);
    HttpClient client = new HttpClient();
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode == HttpStatus.SC_OK) {
            FileUtils.copyInputStreamToFile(method.getResponseBodyAsStream(), new File(filename));
            log.info("Downloaded presentation at [{}]", finalUrl);
            success = true;
        }
    } catch (HttpException e) {
        log.error("HttpException while downloading presentation at [{}]", finalUrl);
    } catch (IOException e) {
        log.error("IOException while downloading presentation at [{}]", finalUrl);
    } finally {
        method.releaseConnection();
    }
    return success;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) File(java.io.File)

Example 78 with GetMethod

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

the class BiocacheQuery method retrieveCustomFacets.

/**
     * Retrieves a list of custom facets for the supplied query.
     *
     * @return
     */
private List<QueryField> retrieveCustomFacets() {
    List<QueryField> customFacets = new ArrayList<QueryField>();
    //custom fields can only be retrieved with specific query types
    String full = getFullQ(false);
    Matcher match = Pattern.compile("data_resource_uid:\"??dr[t][0-9]+\"??").matcher(full);
    if (!match.find()) {
        return customFacets;
    }
    //look up facets
    try {
        final String jsonUri = biocacheServer + "/upload/dynamicFacets?q=" + URLEncoder.encode(match.group(), "UTF-8");
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(jsonUri);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONArray ja = (JSONArray) jp.parse(slist);
        for (Object arrayElement : ja) {
            JSONObject jsonObject = (JSONObject) arrayElement;
            String facetName = jsonObject.get(StringConstants.NAME).toString();
            String facetDisplayName = jsonObject.get("displayName").toString();
            //TODO: remove this when _RNG fields work in legend &cm= parameter
            if (!(facetDisplayName.contains("(Range)") && facetName.endsWith("_RNG"))) {
                LOGGER.debug("Adding custom index : " + arrayElement);
                customFacets.add(new QueryField(facetName, facetDisplayName, QueryField.GroupType.CUSTOM, QueryField.FieldType.STRING));
            }
        }
    } catch (Exception e) {
        LOGGER.error("error loading custom facets for: " + getFullQ(false), e);
    }
    return customFacets;
}
Also used : QueryField(au.org.ala.legend.QueryField) JSONObject(org.json.simple.JSONObject) Matcher(java.util.regex.Matcher) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) LegendObject(au.org.ala.legend.LegendObject)

Example 79 with GetMethod

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

the class BiocacheQuery method getQidDetails.

private JSONObject getQidDetails(String qidTerm) {
    HttpClient client = new HttpClient();
    String url = biocacheServer + QID_DETAILS + qidTerm.replace("qid:", "");
    GetMethod get = new GetMethod(url);
    try {
        int result = client.executeMethod(get);
        String response = get.getResponseBodyAsString();
        if (result == 200) {
            JSONParser jp = new JSONParser();
            JSONObject jo = (JSONObject) jp.parse(response);
            return jo;
        } else {
            LOGGER.debug("error with url:" + url + " getting qid details for " + qidTerm + " > response_code:" + result + " response:" + response);
        }
    } catch (Exception e) {
        LOGGER.error("error getting biocache param details from " + url, e);
    }
    return null;
}
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)

Example 80 with GetMethod

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

the class BiocacheQuery method getLegend.

/**
     * Get legend for a facet field.
     *
     * @param colourmode
     * @return
     */
@Override
public LegendObject getLegend(String colourmode) {
    if ("-1".equals(colourmode) || StringConstants.GRID.equals(colourmode)) {
        return null;
    }
    LegendObject lo = legends.get(colourmode);
    if (lo != null && lo.getColourMode() != null && !lo.getColourMode().equals(colourmode)) {
        LOGGER.debug("lo not empty and lo=" + lo);
        lo = legends.get(lo.getColourMode());
    }
    if (lo == null && getOccurrenceCount() > 0) {
        HttpClient client = new HttpClient();
        String facetToColourBy = StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : translateFieldForSolr(colourmode);
        try {
            String url = biocacheServer + LEGEND_SERVICE_CSV + DEFAULT_ROWS + "&q=" + getQ() + "&cm=" + URLEncoder.encode(facetToColourBy, StringConstants.UTF_8) + getQc();
            LOGGER.debug(url);
            GetMethod get = new GetMethod(url);
            //NQ: Set the header type to JSON so that we can parse JSON instead of CSV (CSV has issue with quoted field where a quote is the escape character)
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String s = get.getResponseBodyAsString();
            //in the first line do field name replacement
            String t = translateFieldForSolr(colourmode);
            if (!colourmode.equals(t)) {
                s = s.replaceFirst(t, colourmode);
            }
            lo = new BiocacheLegendObject(colourmode, s);
            //test for exceptions
            if (!colourmode.contains(",") && (StringConstants.UNCERTAINTY.equals(colourmode) || StringConstants.DECADE.equals(colourmode) || StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.COORDINATE_UNCERTAINTY.equals(colourmode))) {
                lo = ((BiocacheLegendObject) lo).getAsIntegerLegend();
                //apply cutpoints to colourMode string
                Legend l = lo.getNumericLegend();
                if (l == null || l.getCutoffFloats() == null) {
                    return null;
                }
                float[] cutpoints = l.getCutoffFloats();
                float[] cutpointmins = l.getCutoffMinFloats();
                StringBuilder sb = new StringBuilder();
                //NQ 20140109: use the translated SOLR field as the colour mode so that Constants.DECADE does not cause an issue
                String newFacet = StringConstants.DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : colourmode;
                sb.append(newFacet);
                int i = 0;
                while (i < cutpoints.length) {
                    if (i == cutpoints.length - 1 || cutpoints[i] != cutpoints[i + 1]) {
                        if (i > 0) {
                            sb.append(",").append(cutpointmins[i]);
                            if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                                sb.append(StringConstants.DATE_TIME_BEGINNING_OF_YEAR);
                            }
                        } else {
                            sb.append(",*");
                        }
                        sb.append(",").append(cutpoints[i]);
                        if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                            sb.append(StringConstants.DATE_TIME_END_OF_YEAR);
                        }
                    } else if (i < cutpoints.length - 1 && cutpoints[i] == cutpoints[i + 1]) {
                        cutpointmins[i + 1] = cutpointmins[i];
                    }
                    i++;
                }
                String newColourMode = sb.toString();
                if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                    newColourMode = newColourMode.replace(".0", "");
                }
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (StringConstants.MONTH.equals(colourmode)) {
                String newColourMode = "month,00,00,01,01,02,02,03,03,04,04,05,05,06,06,07,07,08,08,09,09,10,10,11,11,12,12";
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (!colourmode.contains(",") && (StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) || StringConstants.DECADE.equals(colourmode))) {
                Set<Integer> decades = new TreeSet<Integer>();
                for (double d : ((BiocacheLegendObject) lo).categoriesNumeric.keySet()) {
                    decades.add((int) (d / 10));
                }
                List<Integer> d = new ArrayList<Integer>(decades);
                StringBuilder sb = new StringBuilder();
                sb.append(StringConstants.OCCURRENCE_YEAR);
                for (int i = !d.isEmpty() && d.get(0) > 0 ? 0 : 1; i < d.size(); i++) {
                    if (i > 0) {
                        sb.append(",").append(d.get(i));
                        sb.append("0-01-01T00:00:00Z");
                    } else {
                        sb.append(",*");
                    }
                    sb.append(",").append(d.get(i));
                    sb.append("9-12-31T00:00:00Z");
                }
                String newColourMode = sb.toString();
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else {
                legends.put(colourmode, lo);
            }
        } catch (Exception e) {
            LOGGER.error("error getting legend for : " + colourmode, e);
        }
    }
    return lo;
}
Also used : Legend(au.org.ala.legend.Legend) LegendObject(au.org.ala.legend.LegendObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17