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

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

Introduction

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

Prototype

@Override
public void releaseConnection() 

Source Link

Document

Releases the connection being used by this HTTP method.

Usage

From source file:org.geotools.data.wfs.protocol.http.DefaultHTTPProtocol.java

/**
 * /*from   www  . jav a 2 s  .  c o m*/
 * @param httpRequest
 *            either a {@link HttpMethod} or {@link PostMethod} set up with the request to be
 *            sent
 * @return
 * @throws IOException
 */
private HTTPResponse issueRequest(final HttpMethodBase httpRequest) throws IOException {
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Executing HTTP request: " + httpRequest.getURI());
    }
    if (client == null) {
        client = new HttpClient();
        client.getParams().setParameter("http.useragent",
                "GeoTools " + GeoTools.getVersion() + " WFS DataStore");
    }
    if (timeoutMillis > 0) {
        client.getParams().setSoTimeout(timeoutMillis);
    }

    // TODO: remove this
    System.err.println("Executing HTTP request: " + httpRequest);

    if (isTryGzip()) {
        LOGGER.finest("Adding 'Accept-Encoding=gzip' header to request");
        httpRequest.addRequestHeader("Accept-Encoding", "gzip");
    }

    int statusCode;
    try {
        statusCode = client.executeMethod(httpRequest);
    } catch (IOException e) {
        httpRequest.releaseConnection();
        throw e;
    }

    if (statusCode != HttpStatus.SC_OK) {
        httpRequest.releaseConnection();
        String statusText = HttpStatus.getStatusText(statusCode);
        throw new IOException("Request failed with status code " + statusCode + "(" + statusText + "): "
                + httpRequest.getURI());
    }

    HTTPResponse httpResponse = new HTTPClientResponse(httpRequest);
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Got " + httpResponse);
    }
    return httpResponse;
}

From source file:org.jboss.mod_cluster.Client.java

public int runit() throws Exception {

    PostMethod pm = null;/*w w w  .j  av a  2s .  c  om*/
    GetMethod gm = null;
    HttpMethodBase bm = null;
    long starttime, endtime;
    if (httpClient == null)
        httpClient = new HttpClient();
    if (fd != null) {
        pm = new PostMethod(URL);
        // InputStreamRequestEntity buf = new InputStreamRequestEntity(fd);
        // XXX: Ugly hack to test...
        byte[] buffet = new byte[6144];
        for (int i = 0; i < buffet.length; i++)
            buffet[i] = 'a';
        ByteArrayRequestEntity buf = new ByteArrayRequestEntity(buffet);
        pm.setRequestEntity(buf);
        // pm.setRequestBody(fd);
        pm.setHttp11(true);
        pm.setContentChunked(true);
        // pm.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
        bm = pm;
    } else if (post != null) {
        pm = new PostMethod(URL);
        pm.setRequestEntity(new StringRequestEntity(post, "application/x-www-form-urlencoded", "UTF8"));
        bm = pm;
    } else {
        gm = new GetMethod(URL);
        bm = gm;
    }
    if (user != null) {
        Credentials cred = new UsernamePasswordCredentials(user, pass);
        httpClient.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, cred);
    }

    // System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (VirtualHost != null)
        bm.getParams().setVirtualHost(VirtualHost);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (jsessionid != null) {
        // System.out.println("jsessionid: " + jsessionid);
        bm.setRequestHeader("Cookie", "JSESSIONID=" + jsessionid);
    }

    try {
        if (gm == null) {
            pm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(pm);
            endtime = System.currentTimeMillis();
        } else {
            gm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(gm);
            endtime = System.currentTimeMillis();
        }

        if (httpResponseCode == 200) {
            response = bm.getResponseBodyAsString();
            Cookie[] cookies = httpClient.getState().getCookies();
            // System.out.println( "Cookies: " + cookies);
            if (cookies != null && cookies.length != 0) {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    // System.out.println( "Cookie: " + cookie.getName() + ", Value: " + cookie.getValue());
                    if (cookie.getName().equals("JSESSIONID")) {
                        if (jsessionid == null) {
                            jsessionid = cookie.getValue();
                            String nodes[] = jsessionid.split("\\.");
                            if (nodes.length == 2)
                                node = nodes[1];
                            System.out.println("cookie first time: " + jsessionid);
                            bm.releaseConnection();
                            return 0; // first time ok.
                        } else {
                            if (jsessionid.compareTo(cookie.getValue()) == 0) {
                                if (logok)
                                    if (bm.getResponseHeader("Date") != null)
                                        System.out.println("cookie ok: "
                                                + bm.getResponseHeader("Date").toString().replace('\r', ' ')
                                                        .replace('\n', ' ')
                                                + " response time: " + (endtime - starttime));
                                    else {
                                        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                                        Date date = new Date();
                                        System.out.println("cookie ok: " + dateFormat.format(date)
                                                + " response time: " + (endtime - starttime));
                                    }
                                bm.releaseConnection();
                                return 0;
                            } else {
                                System.out.println(
                                        "cookie \"second\" time: " + cookie.getValue() + " : " + jsessionid);
                                System.out.println("cookie changed");
                                bm.releaseConnection();
                                if (checkcookie)
                                    return -1;
                                else if (checknode) {
                                    String nodes[] = cookie.getValue().split("\\.");
                                    if (nodes.length != 2) {
                                        System.out.println("Can't find node in cookie");
                                        return -1;
                                    }
                                    if (nodes[1].compareTo(node) == 0) {
                                        return 0;
                                    } else {
                                        System.out.println("node " + nodes[1] + " changed too");
                                        return -1;
                                    }
                                } else
                                    return 0;
                            }
                        }
                    }
                }
            } else {
                // Look in the response to make sure that there is a cookie.
                int len = (int) bm.getResponseContentLength();

                if (jsessionid != null && bm.getResponseBodyAsString(len).indexOf(jsessionid) != -1) {
                    bm.releaseConnection();
                    return 0;
                }
                if (jsessionid == null && !checkcookie) {
                    return 0;
                }
                System.out.println("No cookies");
            }
            Header head = bm.getResponseHeader("getRequestedSessionId");
            if (head != null) {
                HeaderElement[] heade = head.getElements();
                requestedSessionId = heade[0].getValue();
            } else {
                requestedSessionId = null;
            }
        } else {
            System.out.println("response: " + httpResponseCode);
            System.out.println("response: " + bm.getStatusLine());
            response = bm.getResponseBodyAsString();
            System.out.println("response: " + response);
            success = false;
            httpClient = null;
        }
        // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); 
    } catch (HttpException e) {
        e.printStackTrace();
        success = false;
        httpClient = null;
    }
    System.out.println("DONE: " + httpResponseCode);
    bm.releaseConnection();
    return httpResponseCode;
}

From source file:org.jboss.mod_cluster.ManagerClient.java

/**
  * Run the first test./* w w  w. j a  va 2  s  .  c om*/
  *
  * @param string a part of URL to connect to.
  *
  * @return ManagerClient object
  *
  * @throws IOException for any failures.
  */
public ManagerClient(String string) throws Exception {
    URL = "http://" + string + "/mod_cluster_manager/";
    GetMethod gm = null;
    HttpMethodBase bm = null;
    if (httpClient == null) {
        httpClient = new HttpClient();
        gm = new GetMethod(URL);
        bm = gm;
    }

    System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);

    try {
        httpResponseCode = httpClient.executeMethod(gm);

        if (httpResponseCode == 200) {
            // Read the nonce.
            String result = gm.getResponseBodyAsString();
            String[] records = result.split("\n");
            for (int i = 0; i < records.length; i++) {
                int j = records[i].indexOf("?nonce=");
                if (j < 0)
                    continue;
                j = j + 7;
                String nnonce = records[i].substring(j);
                int k = nnonce.indexOf('&');
                if (k > 0) {
                    nonce = nnonce.substring(0, k);
                    break;
                }
            }
        } else {
            System.out.println("response: " + httpResponseCode);
            System.out.println("response: " + bm.getStatusLine());
            throw (new Exception("Reponse notok"));
        }
        // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); 
    } catch (HttpException e) {
        System.out.println("error: " + e);
        throw (e);
    }
    bm.releaseConnection();
}

From source file:org.kuali.mobility.knowledgebase.service.KnowledgeBaseServiceImpl.java

private String callKnowledgeBase(String url, RequestEntity requestEntity, boolean isPost) throws Exception {
    String output = null;//from   w ww  .j av  a 2s  .co m
    //      Document doc = null;
    //      SAXBuilder builder = new SAXBuilder();
    //      BufferedReader in = null;

    HttpClient client = null;
    client = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials(this.username, this.password);
    client.getState().setCredentials(new AuthScope("remote.kb.iu.edu", 80, AuthScope.ANY_REALM), defaultcreds);
    HttpMethodBase method = null;
    if (isPost) {
        method = preparePostMethod(url, requestEntity);
    } else {
        method = new GetMethod(url);
    }
    method.setDoAuthentication(true);

    //      int timeout = getSocketTimeout(Constants.RSS_SOCKET_TIMEOUT_SECONDS, Constants.RSS_SOCKET_DEFAULT_TIMEOUT);
    int timeout = getSocketTimeout("blah", 5000);
    client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(timeout));
    client.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(timeout));
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(timeout));
    try {
        int status = client.executeMethod(method);
        //            System.out.println(status + "\n" + get.getResponseBodyAsString());
        //            in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        //            doc = builder.build(in);
        output = this.inputStreamToString(method.getResponseBodyAsStream());
    } finally {
        method.releaseConnection();
    }

    return output;
}

From source file:org.openlaszlo.data.HTTPDataSource.java

/**
 * @param since last modified time to use
 * @param req//from w ww  .j a  v  a 2s .c  om
 * @param url if null, ignored
 * @param redirCount number of redirs we've done
 */
public static HttpData getDataOnce(HttpServletRequest req, HttpServletResponse res, long since, String surl,
        int redirCount, int timeout)
        throws IOException, HttpException, DataSourceException, MalformedURLException {

    HttpMethodBase request = null;
    HostConfiguration hcfg = new HostConfiguration();

    /*
      [todo hqm 2006-02-01] Anyone know why this code was here? It is setting
      the mime type to something which just confuses the DHTML parser.
              
      if (res != null) {
    res.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
    }
    */

    try {

        // TODO: [2002-01-09 bloch] cope with cache-control
        // response headers (no-store, no-cache, must-revalidate, 
        // proxy-revalidate).

        if (surl == null) {
            surl = getURL(req);
        }
        if (surl == null || surl.equals("")) {
            throw new MalformedURLException(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="url is empty or null"
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                            "051018-312"));
        }

        String reqType = "";
        String headers = "";

        if (req != null) {
            reqType = req.getParameter("reqtype");
            headers = req.getParameter("headers");
        }

        boolean isPost = false;
        mLogger.debug("reqtype = " + reqType);

        if (reqType != null && reqType.equals("POST")) {
            request = new LZPostMethod();
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            isPost = true;
            mLogger.debug("setting POST req method");
        } else if (reqType != null && reqType.equals("PUT")) {
            request = new LZPutMethod();
            // todo [hqm 2007] treat PUT like POST? 
            isPost = true;
            mLogger.debug("setting PUT req method");
        } else if (reqType != null && reqType.equals("DELETE")) {
            request = new LZDeleteMethod();
            mLogger.debug("setting DELETE req method");
        } else {
            mLogger.debug("setting GET (default) req method");
            request = new LZGetMethod();
        }

        request.getParams().setVersion(mUseHttp11 ? HttpVersion.HTTP_1_1 : HttpVersion.HTTP_1_0);

        // Proxy the request headers
        if (req != null) {
            LZHttpUtils.proxyRequestHeaders(req, request);
        }

        // Set headers from query string
        if (headers != null && headers.length() > 0) {
            StringTokenizer st = new StringTokenizer(headers, "\n");
            while (st.hasMoreTokens()) {
                String h = st.nextToken();
                int i = h.indexOf(":");
                if (i > -1) {
                    String n = h.substring(0, i);
                    String v = h.substring(i + 2, h.length());
                    request.setRequestHeader(n, v);
                    mLogger.debug(
                            /* (non-Javadoc)
                             * @i18n.test
                             * @org-mes="setting header " + p[0] + "=" + p[1]
                             */
                            org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                    "051018-359", new Object[] { n, v }));
                }
            }
        }

        mLogger.debug("Parsing url");
        URI uri = LZHttpUtils.newURI(surl);
        try {
            hcfg.setHost(uri);
        } catch (Exception e) {
            throw new MalformedURLException(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="can't form uri from " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-376",
                            new Object[] { surl }));
        }

        // This gets us the url-encoded (escaped) path and query string
        String path = uri.getEscapedPath();
        String query = uri.getEscapedQuery();
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="encoded path:  " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-389",
                        new Object[] { path }));
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="encoded query: " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-397",
                        new Object[] { query }));

        // This call takes a decoded (unescaped) path
        request.setPath(path);

        boolean hasQuery = (query != null && query.length() > 0);

        String rawcontent = null;
        // Newer rawpost protocol puts lzpostbody as a separate
        // top level query arg in the request.
        rawcontent = req.getParameter("lzpostbody");

        if (isPost) {
            // Older rawpost protocol put the "lzpostbody" arg
            // embedded in the "url" args's query args
            if (rawcontent == null && hasQuery) {
                rawcontent = findQueryArg("lzpostbody", query);
            }
            if (rawcontent != null) {
                // Get the unescaped query string
                ((EntityEnclosingMethod) request).setRequestEntity(new StringRequestEntity(rawcontent));
            } else if (hasQuery) {
                StringTokenizer st = new StringTokenizer(query, "&");
                while (st.hasMoreTokens()) {
                    String it = st.nextToken();
                    int i = it.indexOf("=");
                    if (i > 0) {
                        String n = it.substring(0, i);
                        String v = it.substring(i + 1, it.length());
                        // POST encodes values during request
                        ((PostMethod) request).addParameter(n, URLDecoder.decode(v, "UTF-8"));
                    } else {
                        mLogger.warn(
                                /* (non-Javadoc)
                                 * @i18n.test
                                 * @org-mes="ignoring bad token (missing '=' char) in query string: " + p[0]
                                 */
                                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                        "051018-429", new Object[] { it }));
                    }
                }
            }
        } else {
            // This call takes an encoded (escaped) query string
            request.setQueryString(query);
        }

        // Put in the If-Modified-Since headers
        if (since != -1) {
            String lms = LZHttpUtils.getDateString(since);
            request.setRequestHeader(LZHttpUtils.IF_MODIFIED_SINCE, lms);
            mLogger.debug(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="proxying lms: " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-450",
                            new Object[] { lms }));
        }

        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="setting up http client"
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-460"));
        HttpClient htc = null;
        if (mConnectionMgr != null) {
            htc = new HttpClient(mConnectionMgr);
        } else {
            htc = new HttpClient();
        }

        htc.setHostConfiguration(hcfg);

        // This is the data timeout
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="timeout set to " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-478",
                        new Object[] { timeout }));
        htc.getParams().setSoTimeout(timeout);

        // Set connection timeout the same
        htc.getHttpConnectionManager().getParams().setConnectionTimeout(mConnectionTimeout);

        // Set timeout for getting a connection
        htc.getParams().setConnectionManagerTimeout(mConnectionPoolTimeout);

        // TODO: [2003-03-05 bloch] this should be more configurable (per app?)
        if (!isPost) {
            request.setFollowRedirects(mFollowRedirects > 0);
        }

        long t1 = System.currentTimeMillis();
        mLogger.debug("starting remote request");
        int rc = htc.executeMethod(hcfg, request);
        String status = HttpStatus.getStatusText(rc);
        if (status == null) {
            status = "" + rc;
        }
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="remote response status: " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-504",
                        new Object[] { status }));

        HttpData data = null;
        if (isRedirect(rc) && mFollowRedirects > redirCount) {
            String loc = request.getResponseHeader("Location").toString();
            String hostURI = loc.substring(loc.indexOf(": ") + 2, loc.length());
            mLogger.info(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="Following URL from redirect: " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-517",
                            new Object[] { hostURI }));
            long t2 = System.currentTimeMillis();
            if (timeout > 0) {
                timeout -= (t2 - t1);
                if (timeout < 0) {
                    throw new InterruptedIOException(
                            /* (non-Javadoc)
                             * @i18n.test
                             * @org-mes=p[0] + " timed out after redirecting to " + p[1]
                             */
                            org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                    "051018-529", new Object[] { surl, loc }));
                }
            }

            data = getDataOnce(req, res, since, hostURI, redirCount++, timeout);
        } else {
            data = new HttpData(request, rc);
        }

        if (req != null && res != null) {
            // proxy response headers
            LZHttpUtils.proxyResponseHeaders(request, res, req.isSecure());
        }

        return data;

    } catch (ConnectTimeoutException ce) {
        // Transduce to an InterrupedIOException, since lps takes these to be timeouts.
        if (request != null) {
            request.releaseConnection();
        }
        throw new InterruptedIOException(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="connecting to " + p[0] + ":" + p[1] + " timed out beyond " + p[2] + " msecs."
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-557",
                        new Object[] { hcfg.getHost(), hcfg.getPort(), mConnectionTimeout }));
    } catch (HttpRecoverableException hre) {
        if (request != null) {
            request.releaseConnection();
        }
        throw hre;
    } catch (HttpException e) {
        if (request != null) {
            request.releaseConnection();
        }
        throw e;
    } catch (IOException ie) {
        if (request != null) {
            request.releaseConnection();
        }
        throw ie;
    } catch (RuntimeException e) {
        if (request != null) {
            request.releaseConnection();
        }
        throw e;
    }
}

From source file:org.opentides.util.UrlUtil.java

/**
 * Returns the HTML code of the original engine. Takes the URL to connect to
 * the engine. Also takes encoding type that overrides default if not null
 * "UTF8" is typical encoding type//w w  w.j a  v  a2  s . co  m
 * 
 * @param queryURL
 *            - URL of engine to retrieve
 * @param request
 *            - request object
 * @param param
 *            - additional parameters
 *              - Valid parameters are:
 *              - methodName - Either "POST" or "GET". Default is "POST"   
 *            - forwardCookie - if true, will forward cookies found on request object
 *            - IPAddress - if specified, this IP will be used for the request
 *        
 */
public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request,
        final Map<String, Object> param) {
    // determine if get or post method
    HttpMethodBase httpMethodBase;
    Boolean forwardCookie = false;
    InetAddress IPAddress = null;

    if (param != null) {
        if (param.get("forwardCookie") != null)
            forwardCookie = (Boolean) param.get("forwardCookie");

        if (param.get("IPAddress") != null) {
            String IPString = (String) param.get("IPAddress");
            if (!StringUtil.isEmpty(IPString)) {
                IPAddress = convertIPString(IPString);
            }
        }
    }

    if (param != null && "GET".equals((String) param.get("methodName"))) {
        httpMethodBase = new GetMethod(queryURL);
    } else {
        httpMethodBase = new PostMethod(queryURL);
    }

    try {
        // declare the connection objects
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String userAgent = request.getHeader("User-Agent");

        // for debugging
        if (_log.isDebugEnabled())
            _log.debug("Retrieving page from " + queryURL);

        // initialize the connection settings
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
        client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        httpMethodBase.addRequestHeader("accept", "*/*");
        httpMethodBase.addRequestHeader("accept-language", "en-us");
        httpMethodBase.addRequestHeader("user-agent", userAgent);

        if (forwardCookie) {
            // get cookies from request
            Cookie[] cookies = request.getCookies();
            String cookieString = "";
            for (Cookie c : cookies) {
                cookieString += c.getName() + "=" + c.getValue() + "; ";
            }

            // forward cookies to httpMethod
            httpMethodBase.setRequestHeader("Cookie", cookieString);
        }

        if (IPAddress != null) {
            hostConfig.setLocalAddress(IPAddress);
        }

        // Setup for 3 retry  
        httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // now let's retrieve the data
        client.executeMethod(hostConfig, httpMethodBase);

        // Read the response body.
        UrlResponseObject response = new UrlResponseObject();
        response.setResponseBody(httpMethodBase.getResponseBody());
        Header contentType = httpMethodBase.getResponseHeader("Content-Type");
        if (contentType != null)
            response.setResponseType(contentType.getValue());
        else
            response.setResponseType(Widget.TYPE_HTML);
        return response;

    } catch (Exception ex) {
        _log.error("Failed to request from URL: [" + queryURL + "]", ex);
        return null;
    } finally {
        try {
            httpMethodBase.releaseConnection();
        } catch (Exception ignored) {
        }
    }
}

From source file:org.opentides.util.WidgetUtil.java

/**
 * Returns the HTML code of the original engine. Takes the URL to connect to
 * the engine. Also takes encoding type that overrides default if not null
 * "UTF8" is typical encoding type//  w  ww.  j  a v a 2  s .  c om
 * 
 * @param queryURL
 *            - URL of engine to retrieve
 * @param request
 *            - request object
 * @param param
 *            - additional parameters
 *              - Valid parameters are:
 *              - methodName - Either "POST" or "GET". Default is "POST"   
 *            - forwardCookie - if true, will forward cookies found on request object
 *            - IPAddress - if specified, this IP will be used for the request
 *        
 */
public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request,
        final Map<String, Object> param) {
    // determine if get or post method
    HttpMethodBase httpMethodBase;
    Boolean forwardCookie = false;
    InetAddress IPAddress = null;

    if (param != null) {
        if (param.get("forwardCookie") != null)
            forwardCookie = (Boolean) param.get("forwardCookie");

        if (param.get("IPAddress") != null) {
            String IPString = (String) param.get("IPAddress");
            if (!StringUtil.isEmpty(IPString)) {
                IPAddress = UrlUtil.convertIPString(IPString);
            }
        }
    }

    if (param != null && "GET".equals((String) param.get("methodName"))) {
        httpMethodBase = new GetMethod(queryURL);
    } else {
        httpMethodBase = new PostMethod(queryURL);
    }

    try {
        // declare the connection objects
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String userAgent = request.getHeader("User-Agent");

        // for debugging
        if (_log.isDebugEnabled())
            _log.debug("Retrieving page from " + queryURL);

        // initialize the connection settings
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
        client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        httpMethodBase.addRequestHeader("accept", "*/*");
        httpMethodBase.addRequestHeader("accept-language", "en-us");
        httpMethodBase.addRequestHeader("user-agent", userAgent);

        if (forwardCookie) {
            // get cookies from request
            Cookie[] cookies = request.getCookies();
            String cookieString = "";
            for (Cookie c : cookies) {
                cookieString += c.getName() + "=" + c.getValue() + "; ";
            }

            // forward cookies to httpMethod
            httpMethodBase.setRequestHeader("Cookie", cookieString);
        }

        if (IPAddress != null) {
            hostConfig.setLocalAddress(IPAddress);
        }

        // Setup for 3 retry  
        httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // now let's retrieve the data
        client.executeMethod(hostConfig, httpMethodBase);

        // Read the response body.
        UrlResponseObject response = new UrlResponseObject();
        response.setResponseBody(httpMethodBase.getResponseBody());
        Header contentType = httpMethodBase.getResponseHeader("Content-Type");
        if (contentType != null)
            response.setResponseType(contentType.getValue());
        else
            response.setResponseType("html");
        return response;

    } catch (Exception ex) {
        _log.error("Failed to request from URL: [" + queryURL + "]", ex);
        return null;
    } finally {
        try {
            httpMethodBase.releaseConnection();
        } catch (Exception ignored) {
        }
    }
}

From source file:org.paxml.bean.HttpTag.java

/**
 * {@inheritDoc}/*from   w ww  .  j  a v a 2 s .  co m*/
 */
@Override
protected Object doInvoke(Context context) throws Exception {
    String lowUrl = url.toLowerCase();
    if (!lowUrl.startsWith("http://") && !lowUrl.startsWith("https://")) {
        url = "http://" + url;
    }
    HttpClient client = new HttpClient();
    final HttpMethodBase m;
    if ("post".equalsIgnoreCase(method)) {
        m = setPostBody(new PostMethod(url));
    } else if ("get".equalsIgnoreCase(method)) {
        m = new GetMethod(url);
    } else {
        throw new PaxmlRuntimeException("Unknown method: " + method);
    }
    setHeader(m);
    setQueryString(m);
    // Provide custom retry handler is necessary
    m.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(maxRetry, false));

    onBeforeSend(m);
    // method.setr
    try {
        // Execute the method.

        final int statusCode = client.executeMethod(m);

        if (responseless) {
            return statusCode;
        }

        // Read the response body.
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", statusCode);
        result.put("body", m.getResponseBodyAsString());
        result.put("all", m);
        return result;

    } finally {
        // Release the connection.
        m.releaseConnection();
    }
}

From source file:org.portletbridge.portlet.DefaultHttpClientTemplate.java

public Object service(HttpMethodBase method, HttpClientState state, HttpClientCallback callback)
        throws ResourceException {
    try {//from  www. java2 s  .  c o m
        HostConfiguration hostConfiguration = new HostConfiguration();

        if (state.getProxyHost() != null && state.getProxyHost().trim().length() > 0) {
            hostConfiguration.setProxy(state.getProxyHost(), state.getProxyPort());
        }
        hostConfiguration.setHost(method.getURI());
        int statusCode = httpClient.executeMethod(hostConfiguration, method, state.getHttpState());
        return callback.doInHttpClient(statusCode, method);
    } catch (ResourceException e) {
        throw e;
    } catch (Throwable e) {
        throw new ResourceException("error.httpclient", e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.review_board.ereviewboard.core.client.ReviewboardHttpClient.java

private String executeMethod(HttpMethodBase request, IProgressMonitor monitor) throws ReviewboardException {

    monitor = Policy.monitorFor(monitor);

    ensureIsLoggedIn(monitor);//  w  w w. ja v a2  s. c o m

    try {
        monitor.beginTask("Executing request", IProgressMonitor.UNKNOWN);

        int statusCode = executeRequest(request, monitor);

        if (statusCode == HttpStatus.SC_NOT_FOUND)
            throw new ReviewboardResourceNotFoundException(request.getPath());

        return getResponseBodyAsString(request, monitor);
    } finally {
        request.releaseConnection();
        monitor.done();
    }
}