Example usage for org.apache.commons.httpclient HttpMethod releaseConnection

List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection

Introduction

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

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:info.jtrac.mylyn.JtracClient.java

private String doGet(String url) throws Exception {
    HttpMethod get = new GetMethod(url);
    String response = null;/*from  w w w.  j  a v a  2 s.co  m*/
    int code;
    try {
        code = httpClient.executeMethod(get);
        if (code != HttpURLConnection.HTTP_OK) {
            throw new HttpException("HTTP Response Code: " + code);
        }
        response = get.getResponseBodyAsString();
    } finally {
        get.releaseConnection();
    }
    return response;
}

From source file:com.zimbra.cs.servlet.ZimbraServlet.java

public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, Server server,
        String uri, AuthToken authToken) throws IOException, ServiceException {
    if (server == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "cannot find remote server");
        return;/*from   w  w w.j  av a2s .c  om*/
    }
    HttpMethod method;
    String url = getProxyUrl(req, server, uri);
    mLog.debug("Proxy URL = %s", url);
    if (req.getMethod().equalsIgnoreCase("GET")) {
        method = new GetMethod(url.toString());
    } else if (req.getMethod().equalsIgnoreCase("POST") || req.getMethod().equalsIgnoreCase("PUT")) {
        PostMethod post = new PostMethod(url.toString());
        post.setRequestEntity(new InputStreamRequestEntity(req.getInputStream()));
        method = post;
    } else {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "cannot proxy method: " + req.getMethod());
        return;
    }
    HttpState state = new HttpState();
    String hostname = method.getURI().getHost();
    if (authToken != null) {
        authToken.encode(state, false, hostname);
    }
    try {
        proxyServletRequest(req, resp, method, state);
    } finally {
        method.releaseConnection();
    }
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Execute method with httpClient, follow 30x redirects.
 *
 * @param httpClient Http client instance
 * @param method     Http method/*from   w  w w.  j  a  v  a 2s  .  co  m*/
 * @return last http method after redirects
 * @throws IOException on error
 */
public static HttpMethod executeFollowRedirects(HttpClient httpClient, HttpMethod method) throws IOException {
    HttpMethod currentMethod = method;
    try {
        DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS", currentMethod.getURI()));
        httpClient.executeMethod(currentMethod);
        checkNTLM(httpClient, currentMethod);

        String locationValue = getLocationValue(currentMethod);
        // check javascript redirect (multiple authentication pages)
        if (locationValue == null) {
            locationValue = getJavascriptRedirectUrl(currentMethod);
        }

        int redirectCount = 0;
        while (redirectCount++ < 10 && locationValue != null) {
            currentMethod.releaseConnection();
            currentMethod = new GetMethod(locationValue);
            currentMethod.setFollowRedirects(false);
            DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS_COUNT", currentMethod.getURI(),
                    redirectCount));
            httpClient.executeMethod(currentMethod);
            checkNTLM(httpClient, currentMethod);
            locationValue = getLocationValue(currentMethod);
        }
        if (locationValue != null) {
            currentMethod.releaseConnection();
            throw new HttpException("Maximum redirections reached");
        }
    } catch (IOException e) {
        currentMethod.releaseConnection();
        throw e;
    }
    // caller will need to release connection
    return currentMethod;
}

From source file:com.zimbra.cs.service.UserServlet.java

public static Pair<Header[], byte[]> getRemoteResource(ZAuthToken authToken, String url)
        throws ServiceException {
    HttpMethod get = null;
    try {// w  w  w. j av  a  2 s  .  co  m
        Pair<Header[], HttpMethod> pair = doHttpOp(authToken, new GetMethod(url));
        get = pair.getSecond();
        return new Pair<Header[], byte[]>(pair.getFirst(), get.getResponseBody());
    } catch (IOException x) {
        throw ServiceException.FAILURE("Can't read response body " + url, x);
    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }
}

From source file:com.sun.syndication.feed.weather.WeatherGateway.java

protected WeatherChannel buildWeather(HttpMethod method) {
    WeatherChannel weather = null;/*from ww w .  ja  v  a2s.  c  o  m*/
    try {
        // If Weather20Parser were registered with Rome, we could call WireFeedInput instead.
        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(method.getResponseBodyAsStream());
        method.releaseConnection();
        WireFeedParser parser = new Weather20Parser();
        weather = (WeatherChannel) parser.parse(document, false);
    } catch (Exception e) {
        LOG.error("Failed to parse XML document for weather:  " + e.getMessage());
    }
    return weather;
}

From source file:com.google.wave.api.robot.HttpRobotConnection.java

/**
 * Fetches the given URL, given a method ({@code GET} or {@code POST}).
 *
 * @param url the URL to be fetched./*from   ww w  .j  a  va  2s.c o m*/
 * @param method the method to fetch the URL, can be {@code GET} or
 *     {@code POST}.
 * @return the content of the URL.
 *
 * @throws RobotConnectionException if there is a problem fetching the URL,
 *     for example, if the response code is not HTTP OK (200).
 */
private String fetch(String url, HttpMethod method) throws RobotConnectionException {
    try {
        int statusCode = httpClient.executeMethod(method);
        return RobotConnectionUtil.validateAndReadResponse(url, statusCode, method.getResponseBodyAsStream());
    } catch (IOException e) {
        String msg = "Robot fetch http failure: " + url + ".";
        throw new RobotConnectionException(msg, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.kylinolap.common.restclient.RestClient.java

public void wipeCache(String type, String action, String name) throws IOException {
    String url = baseUrl + "/cache/" + type + "/" + name + "/" + action;
    HttpMethod get = new PutMethod(url);

    try {/*  ww w.  j a  v  a2  s  .com*/
        int code = client.executeMethod(get);
        String msg = Bytes.toString(get.getResponseBody());

        if (code != 200)
            throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg);

    } catch (HttpException ex) {
        throw new IOException(ex);
    } finally {
        get.releaseConnection();
    }
}

From source file:it.drwolf.ridire.session.async.JobDBDataUpdater.java

public String getCrawlerEngineStatus() {
    HttpMethod method = null;
    int status = -1;
    try {//from   ww  w  .jav a2 s  .  c o  m
        method = new GetMethod(this.engineUri);
        // TODO check status
        status = this.httpClient.executeMethod(method);
        method.releaseConnection();
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    if (status == 200) {
        return CrawlerManager.RUNNING;
    }
    return CrawlerManager.STOPPED;
}

From source file:mesquite.tol.lib.BaseHttpRequestMaker.java

protected static boolean executeMethod(HttpClient client, HttpMethod method, StringBuffer response) {
    boolean success = true;
    try {/*from   w  w w  .j ava2  s .com*/
        // 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 = method.getResponseBody();
        if (response != null)
            response.append(new String(responseBody));
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        System.out.println(new String(responseBody));

    } catch (HttpException e) {
        //  System.err.println("Fatal protocol violation: " + e.getMessage());
        // e.printStackTrace();
        success = false;
    } catch (IOException e) {
        //  System.err.println("Fatal transport error: " + e.getMessage());
        //  e.printStackTrace();
        success = false;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return success;
}

From source file:com.ikon.util.MailUtils.java

/**
 * User tinyurl service as url shorter Depends on commons-httpclient:commons-httpclient:jar:3.0 because of
 * org.apache.jackrabbit:jackrabbit-webdav:jar:1.6.4
 *///from  ww  w.ja va  2 s.  com
public static String getTinyUrl(String fullUrl) throws HttpException, IOException {
    HttpClient httpclient = new HttpClient();

    // Prepare a request object
    HttpMethod method = new GetMethod("http://tinyurl.com/api-create.php");
    method.setQueryString(new NameValuePair[] { new NameValuePair("url", fullUrl) });
    httpclient.executeMethod(method);
    InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");
    StringWriter sw = new StringWriter();
    int c;
    while ((c = isr.read()) != -1)
        sw.write(c);
    isr.close();
    method.releaseConnection();

    return sw.toString();
}