Example usage for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler

List of usage examples for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler

Introduction

In this page you can find the example usage for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler.

Prototype

public DefaultHttpMethodRetryHandler(int paramInt, boolean paramBoolean) 

Source Link

Usage

From source file:org.sakaiproject.nakamura.grouper.util.GrouperHttpUtil.java

/**
 * Construct an {@link HttpClient} which is configured to authenticate to Nakamura.
 * @return the configured client./* ww  w.  j ava  2s .c  o m*/
 */
public static HttpClient getHttpClient(GrouperConfiguration grouperConfiguration) {
    HttpClient client = new HttpClient();

    DefaultHttpParams.getDefaultParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));

    HttpState state = client.getState();
    state.setCredentials(
            new AuthScope(grouperConfiguration.getUrl().getHost(), getPort(grouperConfiguration.getUrl())),
            new UsernamePasswordCredentials(grouperConfiguration.getUsername(),
                    grouperConfiguration.getPassword()));
    client.getParams().setAuthenticationPreemptive(true);
    client.getParams().setSoTimeout(grouperConfiguration.getHttpTimeout());
    return client;
}

From source file:org.scohen.juploadr.upload.HttpClientFactory.java

public static HttpClient getHttpClient(CommunityAccount account) {
    HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
    Protocol http;//from ww w. jav a 2  s.  c o  m
    if (account.isBandwidthLimited()) {
        http = new Protocol(HTTP, new BandwidthLimitingProtocolSocketFactory(account.getBandwidth()), 80);
    } else {
        http = new Protocol(HTTP, new DefaultProtocolSocketFactory(), 80);
    }

    Protocol.registerProtocol(HTTP, http);
    NetActivator activator = NetActivator.getDefault();
    IProxyService proxyService = activator.getProxyService();
    String home = ((IConfigurationElement) account.getConfiguration().getParent()).getAttribute("home"); //$NON-NLS-1$
    home = Core.furnishWebUrl(home);
    IProxyData proxyData = null;
    try {
        IProxyData[] select = proxyService.select(new URI(home));
        if (select.length > 0)
            proxyData = select[0];
    } catch (URISyntaxException e) {
        activator.logError(Messages.HttpClientFactory_bad_uri_for_proxy, e);
    } finally {
        activator.ungetProxyService(proxyService);
    }
    if (proxyData != null && proxyData.getHost() != null) {
        String proxyHost = proxyData.getHost();
        String proxyPassword = proxyData.getPassword();
        String proxyUsername = proxyData.getUserId();
        int proxyPort = proxyData.getPort();
        HostConfiguration hc = client.getHostConfiguration();
        if (proxyPort < 0)
            hc.setHost(proxyHost);
        else
            hc.setProxy(proxyHost, proxyPort);
        if (proxyData.isRequiresAuthentication() && proxyUsername.length() > 0) {
            Credentials creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
            client.getParams().setAuthenticationPreemptive(true);
            AuthScope scope = new AuthScope(proxyHost, proxyPort);
            client.getState().setProxyCredentials(scope, creds);
        }
    }
    client.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
    client.getHttpConnectionManager().getParams().setSoTimeout(60000);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    return client;
}

From source file:org.sharegov.cirm.utils.GenUtils.java

public static Json httpGetJson(String url) {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    try {/* w  w w . j av  a  2s .c o m*/
        // disable retries from within the HTTP client
        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
            throw new RuntimeException("HTTP Error " + statusCode + " while calling " + url.toString());
        return Json.read(method.getResponseBodyAsString());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.sharegov.cirm.utils.GenUtils.java

public static String httpDelete(String url, String... headers) {
    HttpClient client = new HttpClient();
    DeleteMethod method = new DeleteMethod(url);
    if (headers != null) {
        if (headers.length % 2 != 0)
            throw new IllegalArgumentException(
                    "Odd number of headers argument, specify HTTP headers in pairs: name then value, etc.");
        for (int i = 0; i < headers.length; i++)
            method.addRequestHeader(headers[i], headers[++i]);
    }/*  w  w  w  . j  a  v a 2s. c  om*/
    try {
        // disable retries from within the HTTP client          
        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
            throw new RuntimeException("HTTP Error " + statusCode + " while deleting " + url.toString());
        return method.getResponseBodyAsString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.sharegov.cirm.utils.GenUtils.java

@SuppressWarnings("deprecation")
public static String httpPost(String url, String data, String... headers) {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    if (headers != null) {
        if (headers.length % 2 != 0)
            throw new IllegalArgumentException(
                    "Odd number of headers argument, specify HTTP headers in pairs: name then value, etc.");
        for (int i = 0; i < headers.length; i++)
            method.addRequestHeader(headers[i], headers[++i]);
    }/*from  w  ww  .j av a2 s.c om*/
    method.setRequestBody(data);
    try {
        // disable retries from within the HTTP client          
        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        client.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 0);
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
            throw new RuntimeException(
                    "HTTP Error " + statusCode + " while post to " + url.toString() + ", body " + data);
        return method.getResponseBodyAsString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.siberia.image.searcher.impl.GoogleImageSearcher.java

/** search */
public void search() {

    //        http://images.google.fr/images?imgsz=xxlarge&gbv=2&hl=fr&q=b+e&btnG=Recherche+d%27images
    //        http://images.google.fr/images?imgsz=xxlarge&hl=fr&q=b+e

    Runnable run = new Runnable() {
        public void run() {
            fireSearchHasBegan(new ImageSearcherEvent(GoogleImageSearcher.this));

            StringBuffer buffer = new StringBuffer(50);

            if (getCriterions() != null) {
                boolean oneTokenAlreadyApplied = false;

                for (int i = 0; i < getCriterions().length; i++) {
                    String current = getCriterions()[i];

                    if (current != null) {
                        if (oneTokenAlreadyApplied) {
                            buffer.append("+");
                        }/*from   w  w  w.j a  v  a  2  s .  c o m*/

                        buffer.append(current);

                        oneTokenAlreadyApplied = true;
                    }
                }
            }

            Locale locale = getLocale();
            if (locale == null) {
                locale = Locale.getDefault();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("uri : " + buffer.toString());
            }

            HttpClient client = new HttpClient();

            HttpMethod method = new GetMethod(GOOGLE_URL);

            NameValuePair[] pairs = new NameValuePair[3];
            pairs[0] = new NameValuePair("imgsz", convertImageSizeCriterion(getImageSize()));
            pairs[1] = new NameValuePair("hl", locale.getCountry().toLowerCase());
            pairs[2] = new NameValuePair("q", buffer.toString());
            method.setQueryString(pairs);

            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(3, false));

            InputStream stream = null;

            try {
                // Execute the method.
                int statusCode = client.executeMethod(method);

                if (statusCode == HttpStatus.SC_OK) {
                    /** on recherche  partir des motifs suivants
                     *  la premire occurrence de http://  partir de l, on prend jusqu'au prochaine espace ou '>' :
                     *
                     *  exemple :
                     *      <img src=http://tbn0.google.com/images?q=tbn:GIJo-j_dSy4FiM:http://www.discogs.com/image/R-378796-1136999170.jpeg width=135 height=135>
                     *
                     *  on trouve le motif, puis, on prend  partir de http://www.discogs jusqu'au prochain espace...
                     *
                     *  --> http://www.discogs.com/image/R-378796-1136999170.jpeg
                     */
                    String searchMotif = "<img src=http://tbn0.google.com/images?q";
                    String urlMotif = "http://";

                    int indexInSearchMotif = -1;
                    int indexInUrlMotif = -1;
                    boolean motifFound = false;
                    boolean foundUrl = false;

                    StringBuffer urlBuffer = new StringBuffer(50);

                    // Read the response body.
                    byte[] bytes = new byte[1024 * 8];
                    stream = method.getResponseBodyAsStream();

                    if (stream != null) {
                        int read = -1;

                        int linksRetrieved = 0;

                        while ((read = stream.read(bytes)) != -1) {
                            for (int i = 0; i < read; i++) {
                                byte currentByte = bytes[i];

                                if (motifFound) {
                                    if (foundUrl) {
                                        if (currentByte == ' ' || currentByte == '>') {
                                            /* add current url to list of result */
                                            try {
                                                URL url = new URL(urlBuffer.toString());

                                                fireImageFound(new ImageFoundEvent(GoogleImageSearcher.this,
                                                        url, linksRetrieved));
                                                linksRetrieved++;

                                                if (linksRetrieved >= getMaximumLinksRetrieved()) {
                                                    break;
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            } finally {
                                                urlBuffer.delete(0, urlBuffer.length());

                                                foundUrl = false;
                                                motifFound = false;
                                            }
                                        } else {
                                            /* add current byte to url buffer */
                                            urlBuffer.append((char) currentByte);
                                        }
                                    } else {
                                        if (indexInUrlMotif == urlMotif.length() - 1) {
                                            urlBuffer.append(urlMotif);
                                            urlBuffer.append((char) currentByte);
                                            foundUrl = true;
                                            indexInUrlMotif = -1;
                                        }

                                        /* if the current byte is the same as that attempted on the url motif let's continue */
                                        if (((char) currentByte) == urlMotif.charAt(indexInUrlMotif + 1)) {
                                            indexInUrlMotif++;
                                        } else {
                                            indexInUrlMotif = -1;
                                        }
                                    }
                                } else {
                                    if (indexInSearchMotif == searchMotif.length() - 1) {
                                        motifFound = true;
                                        indexInSearchMotif = -1;
                                    }

                                    if (((char) currentByte) == searchMotif.charAt(indexInSearchMotif + 1)) {
                                        indexInSearchMotif++;
                                    } else {
                                        indexInSearchMotif = -1;
                                    }
                                }
                            }
                            if (linksRetrieved >= getMaximumLinksRetrieved()) {
                                break;
                            }
                        }
                    }
                } else {
                    System.err.println("Method failed: " + method.getStatusLine());
                }
            } catch (HttpException e) {
                System.err.println("Fatal protocol violation: " + e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException ex) {
                        System.err.println("Fatal transport error: " + ex.getMessage());
                    }
                }
                System.err.println("Fatal transport error: " + e.getMessage());
                e.printStackTrace();
            } finally { // Release the connection.
                method.releaseConnection();

                fireSearchFinished(new ImageSearcherEvent(GoogleImageSearcher.this));
            }
        }
    };

    this.service.submit(run);
}

From source file:org.sonatype.nexus.bundle.launcher.support.RequestUtils.java

public static boolean isNexusRESTStarted(final String nexusBaseURI) throws IOException, HttpException {
    Preconditions.checkNotNull(nexusBaseURI);
    final String serviceStatusURI = nexusBaseURI.endsWith("/") ? nexusBaseURI + "service/local/status"
            : nexusBaseURI + "/service/local/status";
    org.apache.commons.httpclient.HttpMethod method = null;
    try {//from   w  w  w  .  ja v  a  2s.co  m
        method = new GetMethod(serviceStatusURI);
        // only try once makes sense by default
        DefaultHttpMethodRetryHandler oneRetry = new DefaultHttpMethodRetryHandler(1, true);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, oneRetry);

        method = executeHTTPClientMethodAsAdmin(method);
        final int statusCode = method.getStatusCode();
        if (statusCode != 200) {
            LOG.debug("Status check returned status " + statusCode);
            return false;
        }

        final String entityText = method.getResponseBodyAsString();
        if (entityText == null || !entityText.contains("<state>STARTED</state>")) {
            LOG.debug("Status check returned invalid system state. Status: " + entityText);
            return false;
        }

        return true;
    } finally {
        if (method != null) {
            method.releaseConnection(); // request facade does this but just making sure
        }
    }
}

From source file:org.tgta.tagger.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from   w ww .  j a  v  a  2 s.co m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody;
        InputStream in = method.getResponseBodyAsStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        //System.out.println(out.toString());   //Prints the string content read from input stream
        reader.close();

        response = out.toString();

        //TODO Going to buffer response body of large or unknown size. 
        //Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        //response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:org.trec.liveqa.GetYAnswersPropertiesFromQid.java

/**
 * /*w w  w .  j a v  a 2  s .c  o m*/
 * @param iQid question ID
 * @return map of features and attributes: question title, body, category, best answer, date
 * @throws Exception
 */
public static Map<String, String> extractData(String iQid) throws Exception {

    Map<String, String> res = new LinkedHashMap<>();
    res.put("qid", iQid);

    // parse date from qid
    res.put("Date", DATE_FORMAT.parse(iQid.substring(0, 14)).toString());

    // get and mine html page
    String url = URL_PREFIX + iQid;
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        InputStream responseBody = method.getResponseBodyAsStream();

        // strip top levels
        Document doc = Jsoup.parse(responseBody, "UTF8", url);
        Element html = doc.child(0);

        Element body = html.child(1);
        Element head = html.child(0);

        // get category
        res.put("Top level Category", findElementText(body, cc));

        // get title
        res.put("Title", findElementText(head, ct));

        // get body
        res.put("Body", findElementText(head, cb));

        // get keywords
        res.put("Keywords", findElementText(head, ck));

        // get best answer
        Element best_answer_div = html.select("div#ya-best-answer").first();
        if (best_answer_div != null) {
            res.put("Best Answer", findElementText(best_answer_div, cba));
        }

        responseBody.close();

    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }

    return res;
}

From source file:org.wise.portal.domain.admin.DailyAdminJob.java

/**
 * POSTs WISE usage statistics to central hub
 *//* w w w .ja  va2  s. c  o m*/
public void postStatistics(String wiseStatisticsString) {

    if (WISE_HUB_URL != null) {
        HttpClient client = new HttpClient();

        // Create a method instance.
        PostMethod method = new PostMethod(WISE_HUB_URL);

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        method.addParameter("name", wiseProperties.getProperty("wise.name"));
        method.addParameter("wiseBaseURL", wiseProperties.getProperty("wiseBaseURL"));
        method.addParameter("stats", wiseStatisticsString);

        try {

            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            // Read the response body.
            //byte[] responseBody = null;
            //responseBody = method.getResponseBody();
            //String responseString = new String(responseBody);
            //System.out.println(responseString);

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
    }
}