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:net.kenware.langtrans.GoogleTranslater.java

/**
 * translates source string (sin) into a response using the source/target
 * languages.//  w ww .j  a va  2  s .  c om
 *
 * @param sin String in
 * @param sourcelang source language (ISO3166 code)
 * @param targetlang target language (ISO3166 code)
 * @return returns a text string in the target language
 */
public String urlTranslate(String sin, String sourcelang, String targetlang) {
    String response = "";
    String url = null;
    HttpClient client = new HttpClient();
    HttpMethod method = null;
    String responsebody = null;

    try {
        String s = URLEncoder.encode(sin, "UTF-8");
        url = "http://google.com/translate_t?hl=en&ie=UTF8&text=" + s + "&langpair=" + sourcelang + "%7C"
                + targetlang;
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        method = new GetMethod(url);
        method.setFollowRedirects(true);

        client.executeMethod(method);
        responsebody = method.getResponseBodyAsString();
        method.releaseConnection();

        response = extractResponse(responsebody);

    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + url + "'");
        System.err.println(he.getMessage());

    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + url + "'");

    } catch (Exception ex) {
        System.err.println("EXCEPTION: " + ex.getMessage());
    } // end-try-catch

    return (response);
}

From source file:com.sun.jersey.client.apache.ApacheHttpClientHandler.java

@Override
public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException {

    final HttpMethod method = getHttpMethod(cr);

    methodExecutor.executeMethod(method, cr);

    try {/* w w  w  .  j a v a 2  s.c  o  m*/
        ClientResponse r = new ClientResponse(method.getStatusCode(), getInBoundHeaders(method),
                new HttpClientResponseInputStream(method), workers);
        if (!r.hasEntity()) {
            r.bufferEntity();
            r.close();
        }
        return r;
    } catch (Exception e) {
        method.releaseConnection();
        throw new ClientHandlerException(e);
    }
}

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

private String[] getJobsArray() throws HttpException, IOException, SAXException, XPathExpressionException {
    if (this.getCrawlerEngineStatus().equals(CrawlerManager.STOPPED)) {
        return new String[] {};
    }//from w  w w .  j  a va2  s . c o  m
    HttpMethod method = null;
    List<String> ret = new ArrayList<String>();
    try {
        method = new PostMethod(this.engineUri);
        // method.setFollowRedirects(true);
        ((PostMethod) method).addParameter(new NameValuePair("action", "rescan"));
        // TODO check status code
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        method = new GetMethod(this.engineUri);
        status = this.httpClient.executeMethod(method);
        String body = method.getResponseBodyAsString();
        Matcher m = CrawlerManager.pJob.matcher(body);
        int start = 0;
        while (m.find(start)) {
            ret.add(m.group(1));
            start = m.end();
        }
        method.releaseConnection();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return ret.toArray(new String[ret.size()]);
}

From source file:de.micromata.jira.rest.core.util.RestException.java

public RestException(HttpMethod method) {
    this.statusCode = method.getStatusCode();
    this.reasonPhrase = method.getStatusText();
    try {/*  w  ww.  jav a 2s  .co  m*/
        InputStream inputStream = method.getResponseBodyAsStream();
        if (inputStream != null) {
            InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);
            Gson gson = new Gson();
            restErrorMessage = gson.fromJson(jsonReader, ErrorBean.class);
        }
    } catch (IOException e) {
        // nothing to say
    } finally {
        method.releaseConnection();
    }

}

From source file:ccc.api.http.SiteBrowserImpl.java

private String invoke(final HttpMethod m) {
    try {//from  w  ww  .  java  2 s  . co m
        _httpClient.executeMethod(m);
        final int status = m.getStatusCode();
        if (OK == status) {
            return m.getResponseBodyAsString();
        }
        throw new RuntimeException(status + ": " + m.getResponseBodyAsString());
    } catch (final HttpException e) {
        throw new InternalError(); // FIXME: Report error.
    } catch (final IOException e) {
        throw new InternalError(); // FIXME: Report error.
    } finally {
        m.releaseConnection();
    }
}

From source file:com.markwatson.linkeddata.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+");
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {//  w  ww.jav a 2s.c  o  m
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java

/**
 * HttpMethod.//w ww  .ja  v a  2  s . com
 * 
 * @param httpMethod
 *            httpMethod
 * @param httpClientConfig
 *            the http client config
 * @return the http method response body as string
 * @throws HttpClientException
 *             ?HttpClientUtilException?
 */
private static String getHttpMethodResponseBodyAsString(HttpMethod httpMethod,
        HttpClientConfig httpClientConfig) throws HttpClientException {

    try {
        httpMethod = executeMethod(httpMethod, httpClientConfig);
        //httpMethod.getParams().setContentCharset(charSet);

        // ?getResponseBodybyte?
        // ?getResponseBodyAsStringString?String????String??"?"??
        // ?getResponseBodyAsStream????

        // ?
        String responseBodyAsString = httpMethod.getResponseBodyAsString();

        if (log.isDebugEnabled()) {
            Map<String, Object> map = getHttpMethodResponseAttributeMapForLog(httpMethod, httpClientConfig);
            log.debug("getHttpMethodResponseAttributeMapForLog:{}", JsonUtil.format(map));
        }

        return responseBodyAsString;

    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
        throw new HttpClientException(e);
    } finally {
        // 
        httpMethod.releaseConnection();
    }
}

From source file:com.creditease.bdp.axis2.transport.http.PooledHTTPTransportSender.java

public void cleanup(MessageContext msgContext) throws AxisFault {
    HttpMethod httpMethod = (HttpMethod) msgContext.getProperty(HTTPConstants.HTTP_METHOD);

    if (httpMethod != null) {
        // TODO : Don't do this if we're not on the right thread! Can we confirm?
        log.trace("cleanup() releasing connection for " + httpMethod);

        httpMethod.releaseConnection();
        msgContext.removeProperty(HTTPConstants.HTTP_METHOD); // guard against multiple calls
    }/*from   w  ww. jav  a  2s  .  c  om*/
}

From source file:com.thoughtworks.twist.mingle.core.MingleClient.java

public TaskDataList getAllTaskData(String queryUrl) {
    HttpMethod method = getMethod(queryUrl(queryUrl));
    try {//from   w  w  w . j a v a2 s  .  co m
        switch (executeMethod(method)) {
        case HttpStatus.SC_OK:
            return new TaskDataList((List) parse(getResponse(method)));
        case HttpStatus.SC_UNAUTHORIZED:
            throw new MingleAuthenticationException(
                    "Could not authenticate user. Check username and password.");
        }
        return new TaskDataList();
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}

From source file:com.knowledgebooks.rdf.SparqlClient.java

public SparqlClient(String endpoint_URL, String sparql) throws Exception {
    //System.out.println("SparqlClient("+endpoint_URL+", "+sparql+")");
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);

    String req = URLEncoder.encode(sparql, "utf-8");
    HttpMethod method = new GetMethod(endpoint_URL + "?query=" + req);
    method.setFollowRedirects(false);/*  ww w .  j  a v a 2s. co  m*/
    try {
        client.executeMethod(method);
        //System.out.println(method.getResponseBodyAsString());
        //if (true) return;
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + endpoint_URL + "'");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + endpoint_URL + "'");
    }
    method.releaseConnection();
}