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:org.eclipsetrader.kdb.internal.core.connector.BackfillConnector.java

private IOHLC[] backfillDailyHistory(IFeedIdentifier identifier, Date from, Date to) {
    List<OHLC> list = new ArrayList<OHLC>();

    Calendar c = Calendar.getInstance();
    c.setTime(from);/* ww  w . j  a  v  a2  s .c om*/
    int firstYear = c.get(Calendar.YEAR);
    c.setTime(to);
    int lastYear = c.get(Calendar.YEAR);
    firstYear = lastYear - 2;

    HttpClient client = new HttpClient();
    for (int ys = lastYear; ys > firstYear; ys--) {
        try {
            HttpMethod method = Util.get1YearHistoryFeedMethod(identifier, ys);
            Util.setupProxy(client, method.getURI().getHost());
            System.out.println(method.getURI());
            client.getHttpConnectionManager().closeIdleConnections(100);
            client.executeMethod(method);

            BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            readDailyBackfillStream(list, in);
            in.close();
            method.releaseConnection();
            Thread.sleep(3000);

        } catch (Exception e) {
            Status status = new Status(IStatus.ERROR, KdbActivator.PLUGIN_ID, 0, "Error reading data", e);
            KdbActivator.log(status);
        }
    }

    Collections.sort(list, new Comparator<OHLC>() {

        @Override
        public int compare(OHLC o1, OHLC o2) {
            return o1.getDate().compareTo(o2.getDate());
        }
    });

    for (Iterator<OHLC> iter = list.iterator(); iter.hasNext();) {
        OHLC ohlc = iter.next();
        if (ohlc.getDate().before(from) || ohlc.getDate().after(to)) {
            iter.remove();
        }
    }

    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }

    return list.toArray(new IOHLC[list.size()]);
}

From source file:org.eclipsetrader.kdb.internal.core.connector.BackfillConnector.java

private IOHLC[] backfill1DayHistory(IFeedIdentifier identifier) throws Exception {
    List<OHLC> list = new ArrayList<OHLC>();

    HttpMethod method = Util.get1DayHistoryFeedMethod(identifier);
    method.setFollowRedirects(true);//from   w w w.ja  v  a 2  s  .  c  o m

    HttpClient client = new HttpClient();
    Util.setupProxy(client, method.getURI().getHost());
    client.executeMethod(method);

    BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
    read1DayBackfillStream(list, in);
    in.close();
    method.releaseConnection();

    return list.toArray(new IOHLC[list.size()]);
}

From source file:org.eclipsetrader.kdb.internal.core.connector.SnapshotConnector.java

protected void fetchLatestSnapshot(HttpClient client, String[] symbols, boolean isStaleUpdate) {
    HttpMethod method = null;
    BufferedReader in = null;/*w  w w  .  j  ava2 s.c om*/
    String line = ""; //$NON-NLS-1$

    try {
        method = Util.getSnapshotFeedMethod(symbols);

        client.executeMethod(method);

        in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((line = in.readLine()) != null) {
            processSnapshotData(line, isStaleUpdate);
        }

        FeedSubscription[] subscriptions;
        synchronized (symbolSubscriptions) {
            Collection<FeedSubscription> c = symbolSubscriptions.values();
            subscriptions = c.toArray(new FeedSubscription[c.size()]);
        }
        for (int i = 0; i < subscriptions.length; i++) {
            subscriptions[i].fireNotification();
        }

    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, KdbActivator.PLUGIN_ID, 0, "Error reading data", e);
        KdbActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Status status = new Status(IStatus.WARNING, KdbActivator.PLUGIN_ID, 0,
                    "Connection wasn't closed cleanly", e);
            KdbActivator.log(status);
        }
    }
}

From source file:org.eclipsetrader.yahoo.internal.core.connector.SnapshotConnector.java

protected void fetchLatestSnapshot(HttpClient client, String[] symbols, boolean isStaleUpdate) {
    HttpMethod method = null;
    BufferedReader in = null;/* ww w  .  j  a  v a2s.c o  m*/
    String line = ""; //$NON-NLS-1$

    try {
        method = Util.getSnapshotFeedMethod(symbols);

        client.executeMethod(method);

        in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((line = in.readLine()) != null) {
            processSnapshotData(line, isStaleUpdate);
        }

        FeedSubscription[] subscriptions;
        synchronized (symbolSubscriptions) {
            Collection<FeedSubscription> c = symbolSubscriptions.values();
            subscriptions = c.toArray(new FeedSubscription[c.size()]);
        }
        for (int i = 0; i < subscriptions.length; i++) {
            subscriptions[i].fireNotification();
        }

    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Status status = new Status(IStatus.WARNING, YahooActivator.PLUGIN_ID, 0,
                    "Connection wasn't closed cleanly", e);
            YahooActivator.log(status);
        }
    }
}

From source file:org.eclipsetrader.yahoo.internal.core.connector.StreamingConnector.java

@Override
public void run() {
    BufferedReader in = null;/*from   w  w w.  j  a va 2 s  . co  m*/
    char[] buffer = new char[512];

    try {
        HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, Util.streamingFeedHost);

        HttpMethod method = null;

        while (!isStopping()) {
            // Check if the connection was not yet initialized or there are changed in the subscriptions.
            if (in == null || isSubscriptionsChanged()) {
                try {
                    if (method != null) {
                        method.releaseConnection();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e) {
                    // We can't do anything at this time, ignore
                }

                String[] symbols;
                synchronized (symbolSubscriptions) {
                    Set<String> s = new HashSet<String>(symbolSubscriptions.keySet());
                    s.add("MSFT");
                    symbols = s.toArray(new String[s.size()]);
                    setSubscriptionsChanged(false);
                    if (symbols.length == 0) {
                        break;
                    }
                }
                method = Util.getStreamingFeedMethod(symbols);

                client.executeMethod(method);

                in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));

                line = new StringBuilder();
                script = new StringBuilder();
                inTag = false;
                inScript = false;

                fetchLatestSnapshot(client, symbols, false);
            }

            if (in.ready()) {
                int length = in.read(buffer);
                if (length == -1) {
                    in.close();
                    in = null;
                    continue;
                }
                processIncomingChars(buffer, length);
            } else {
                // Check stale data
                List<String> updateList = new ArrayList<String>();
                synchronized (symbolSubscriptions) {
                    long currentTime = System.currentTimeMillis();
                    for (FeedSubscription subscription : symbolSubscriptions.values()) {
                        long elapsedTime = currentTime - subscription.getIdentifierType().getLastUpdate();
                        if (elapsedTime >= 60000) {
                            updateList.add(subscription.getIdentifierType().getSymbol());
                            subscription.getIdentifierType().setLastUpdate(currentTime / 60000 * 60000);
                        }
                    }
                }
                if (updateList.size() != 0) {
                    fetchLatestSnapshot(client, updateList.toArray(new String[updateList.size()]), true);
                }
            }

            Thread.sleep(100);
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            // We can't do anything at this time, ignore
        }
    }
}

From source file:org.eclipsetrader.yahoojapan.internal.core.connector.SnapshotConnector.java

protected void fetchLatestSnapshot(HttpClient client, String[] symbols, boolean isStaleUpdate) {
    HttpMethod method = null;
    BufferedReader in = null;/*from  w  w w.j  av  a2  s .  c o m*/
    String line = ""; //$NON-NLS-1$

    try {
        for (int page = 1; page < 11; page++) {
            StringBuilder work = new StringBuilder();
            boolean isNextPage = false;

            method = Util.getSnapshotFeedMethod(symbols, page);
            method.setFollowRedirects(true);

            client.executeMethod(method);

            in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            while ((line = in.readLine()) != null) {
                if (line.indexOf("<!-- .pfListView -->") >= 0) {
                    break;
                }
            }
            while ((line = in.readLine()) != null) {
                if (line.indexOf("<!---->") >= 0) {
                    isNextPage = (line.indexOf("?</a>") >= 0);
                    break;
                }
                if (line.startsWith("<a href=\"http://stocks.finance.yahoo.co.jp/stocks/detail/?code=")) {
                    //CODE
                    //                      work.append(line.substring(63, 67));
                    //                      work.append(",");
                    work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")));
                    work.append(",");
                    //LAST, DATE
                    while ((line = in.readLine()) != null) {
                        if (line.equals("<td nowrap>")) {
                            break;
                        }
                    }
                    if ((line = in.readLine()) != null) {
                        if (line.indexOf("<td>") >= 0) {
                            work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>"))
                                    .replaceAll(",", ""));
                            work.append(",");
                            String date = line.substring(0, line.indexOf("</td>"));
                            if (date.indexOf("/") >= 0) {
                                work.append(date);
                                work.append(",00:00,,");
                            } else {
                                Calendar c = Calendar.getInstance();
                                work.append(String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
                                        + String.valueOf(c.get(Calendar.DAY_OF_MONTH)));
                                work.append(",");
                                work.append(date);
                                work.append(",,");
                            }
                        }
                    }
                    //OPEN
                    while ((line = in.readLine()) != null) {
                        if (line.equals("<td nowrap>")) {
                            break;
                        }
                    }
                    if ((line = in.readLine()) != null) {
                        if (line.indexOf("</td>") >= 0) {
                            work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", ""));
                        }
                    }
                    work.append(",");
                    //HIGH
                    while ((line = in.readLine()) != null) {
                        if (line.equals("<td nowrap>")) {
                            break;
                        }
                    }
                    if ((line = in.readLine()) != null) {
                        if (line.indexOf("</td>") >= 0) {
                            work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", ""));
                        }
                    }
                    work.append(",");
                    //LOW
                    while ((line = in.readLine()) != null) {
                        if (line.equals("<td nowrap>")) {
                            break;
                        }
                    }
                    if ((line = in.readLine()) != null) {
                        if (line.indexOf("</td>") >= 0) {
                            work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", ""));
                        }
                    }
                    work.append(",,,,,,,@");
                    processSnapshotData(work.toString(), isStaleUpdate);
                    work = new StringBuilder();
                }
            }

            FeedSubscription[] subscriptions;
            synchronized (symbolSubscriptions) {
                Collection<FeedSubscription> c = symbolSubscriptions.values();
                subscriptions = c.toArray(new FeedSubscription[c.size()]);
            }
            for (int i = 0; i < subscriptions.length; i++) {
                subscriptions[i].fireNotification();
            }

            if (!isNextPage) {
                break;
            }
        }

    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooJapanActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Status status = new Status(IStatus.WARNING, YahooJapanActivator.PLUGIN_ID, 0,
                    "Connection wasn't closed cleanly", e);
            YahooJapanActivator.log(status);
        }
    }
}

From source file:org.eclipsetrader.yahoojapan.internal.core.connector.SnapshotConnector.java

protected void fetchLatestSnapshot1(HttpClient client, String[] symbols, boolean isStaleUpdate) {
    HttpMethod method = null;
    BufferedReader in = null;/*ww  w. ja v  a 2  s  . c o m*/
    String line = ""; //$NON-NLS-1$

    try {
        StringBuilder work = new StringBuilder();

        method = Util.getSnapshotFeedMethod(symbols, 1);
        method.setFollowRedirects(true);

        client.executeMethod(method);

        in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<!-- NEW STOCKS DETAIL -->") >= 0) {
                break;
            }
        }
        //CODE
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<dl class=\"stocksInfo\">") >= 0) {
                break;
            }
        }
        if ((line = in.readLine()) != null) {
            if (line.length() > 8) {
                work.append(line.substring(4, 8));
            }
        }
        work.append(",");
        //DATE
        String date = "";
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<dd class=\"yjSb real\">") >= 0) {
                break;
            }
        }
        if (line.length() > 33) {
            date = line.substring(28, line.indexOf("</span>"));
        }
        //LAST
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<td class=\"stoksPrice\">") >= 0) {
                break;
            }
        }
        if (line.length() > 23) {
            work.append(line.substring(line.indexOf("<td class=\"stoksPrice\">") + 23, line.indexOf("</td>"))
                    .replaceAll(",", ""));
        }
        work.append(",");
        if (date.indexOf("/") >= 0) {
            work.append(date);
            work.append(",00:00,,");
        } else {
            Calendar c = Calendar.getInstance();
            work.append(String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
                    + String.valueOf(c.get(Calendar.DAY_OF_MONTH)));
            work.append(",");
            work.append(date);
            work.append(",,");
        }
        //YESTERDAY LAST
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<div class=\"innerDate\">") >= 0) {
                break;
            }
        }
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<strong>") >= 0) {
                break;
            }
        }
        //OPEN
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<strong>") >= 0) {
                break;
            }
        }
        if (line.length() > 16) {
            work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",",
                    ""));
        }
        work.append(",");
        //HIGH
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<strong>") >= 0) {
                break;
            }
        }
        if (line.length() > 16) {
            work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",",
                    ""));
        }
        work.append(",");
        //LOW
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<strong>") >= 0) {
                break;
            }
        }
        if (line.length() > 16) {
            work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",",
                    ""));
        }
        work.append(",");
        //VOLUME
        while ((line = in.readLine()) != null) {
            if (line.indexOf("<strong>") >= 0) {
                break;
            }
        }
        if (line.length() > 16) {
            work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",",
                    ""));
        }
        work.append(",,,,,,@");

        processSnapshotData(work.toString(), isStaleUpdate);

        FeedSubscription[] subscriptions;
        synchronized (symbolSubscriptions) {
            Collection<FeedSubscription> c = symbolSubscriptions.values();
            subscriptions = c.toArray(new FeedSubscription[c.size()]);
        }
        for (int i = 0; i < subscriptions.length; i++) {
            subscriptions[i].fireNotification();
        }

    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooJapanActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Status status = new Status(IStatus.WARNING, YahooJapanActivator.PLUGIN_ID, 0,
                    "Connection wasn't closed cleanly", e);
            YahooJapanActivator.log(status);
        }
    }
}

From source file:org.eclipsetrader.yahoojapanfx.internal.core.connector.SnapshotConnector.java

protected void fetchLatestSnapshot(HttpClient client, String[] symbols) {
    try {/*from w w w .jav  a 2  s  .  com*/
        HttpMethod method = Util.getSnapshotFeedMethod(streamingServer);

        BufferedReader in = null;
        try {
            client.executeMethod(method);

            if ((method.getResponseHeader("Content-Encoding") != null)
                    && (method.getResponseHeader("Content-Encoding").getValue().equals("gzip"))) {
                in = new BufferedReader(
                        new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream())));
            } else {
                in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            }
            StringBuilder sb = new StringBuilder(1000);
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                sb.append(inputLine);
            }

            Calendar c = Calendar.getInstance();
            String year = String.valueOf(c.get(Calendar.YEAR));

            JSONObject obj = new JSONObject(sb.toString());
            JSONObject arate = obj.getJSONObject("rate");
            String date = obj.getString("date");
            //                String company = obj.getString("company");
            for (int i = 0; i < symbols.length; i++) {
                String symbol = (symbols[i].length() > 6 ? symbols[i].substring(0, 6) : symbols[i]);
                if (arate.has(symbol)) {
                    JSONObject o = arate.getJSONObject(symbol);
                    processSnapshotData(symbols[i], o, year + "/" + date);
                }
            }

        } catch (Exception e) {
            Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e); //$NON-NLS-1$
            Activator.log(status);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (method != null) {
                    method.releaseConnection();
                }
            } catch (Exception e) {
                Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 0,
                        "Connection wasn't closed cleanly", e);
                Activator.log(status);
            }
        }

        wakeupNotifyThread();
    } catch (Exception e) {
        Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e)); //$NON-NLS-1$
    }
}

From source file:org.encuestame.core.util.SocialUtils.java

/**
 * Get Google Stats from google short url.
 * @param googleShortUrl//  w w w. j  a  v a  2s. co  m
 * @return
 * @throws IOException
 */
public static String getGoGlStats(final String googleShortUrl) throws IOException {
    HttpClient httpclient = new HttpClient();
    String completeUrl = StringUtils.replace(SocialUtils.GOOGLE_SHORT_URL_STATS, "$1", googleShortUrl);
    HttpMethod method = new GetMethod(completeUrl);
    httpclient.executeMethod(method);
    String tinyUrl = method.getResponseBodyAsString();
    method.releaseConnection();
    return tinyUrl;
}

From source file:org.encuestame.core.util.SocialUtils.java

/**
 * Get TinyUrl.//from w ww .jav a  2 s. c om
 * @param url
 * @return
 * @throws HttpException
 * @throws IOException
 */
public static String getYourls(final String url) {
    String yourlsShortUrl = url;
    HttpClientParams params = new HttpClientParams();
    params.setConnectionManagerTimeout(EnMePlaceHolderConfigurer.getIntegerProperty("application.timeout"));
    params.setSoTimeout(EnMePlaceHolderConfigurer.getIntegerProperty("application.timeout"));
    HttpClient httpclient = new HttpClient(params); //TODO: time out??
    HttpMethod method = new GetMethod(EnMePlaceHolderConfigurer.getProperty("short.yourls.path"));
    method.setQueryString(new NameValuePair[] { new NameValuePair("url", url),
            new NameValuePair("action", "shorturl"), new NameValuePair("format", "json"),
            new NameValuePair("signature", EnMePlaceHolderConfigurer.getProperty("short.yourls.key")) });
    System.out.println("method--->" + method.getPath());
    System.out.println("method--->" + method.getQueryString());
    try {
        httpclient.executeMethod(method);
        final Object jsonObject = JSONValue.parse(method.getResponseBodyAsString());
        final JSONObject o = (JSONObject) jsonObject;
        //{"message":"Please log in","errorCode":403}"
        Long errorCode = (Long) o.get("errorCode");
        if (errorCode != null) {
            throw new EnMeException("Yourls error: " + errorCode);
        }
        yourlsShortUrl = (String) o.get("shorturl");
    } catch (HttpException e) {
        log.error("HttpException " + e);
        yourlsShortUrl = url;
    } catch (IOException e) {
        log.error("IOException" + e);
        yourlsShortUrl = url;
    } catch (Exception e) {
        //e.printStackTrace();
        log.error("IOException" + e);
        yourlsShortUrl = url;
    } finally {
        RequestSessionMap.setErrorMessage("short url is not well configured");
        method.releaseConnection();
    }
    return yourlsShortUrl;
}