Example usage for com.liferay.portal.kernel.webcache WebCacheException WebCacheException

List of usage examples for com.liferay.portal.kernel.webcache WebCacheException WebCacheException

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.webcache WebCacheException WebCacheException.

Prototype

public WebCacheException(Throwable cause) 

Source Link

Usage

From source file:com.liferay.portlet.currencyconverter.util.CurrencyWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    String symbol = _symbol;//w w  w.jav  a 2  s .c  o  m
    double rate = 0.0;

    try {
        if (symbol.length() == 6) {
            String fromSymbol = symbol.substring(0, 3);
            String toSymbol = symbol.substring(3, 6);

            if (!CurrencyUtil.isCurrency(fromSymbol) || !CurrencyUtil.isCurrency(toSymbol)) {

                throw new WebCacheException(symbol);
            }
        } else if (symbol.length() == 3) {
            if (!CurrencyUtil.isCurrency(symbol)) {
                throw new WebCacheException(symbol);
            }
        } else {
            throw new WebCacheException(symbol);
        }

        String text = HttpUtil
                .URLtoString("http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "=X&f=sl1d1t1c1ohgv&e=.csv");

        StringTokenizer st = new StringTokenizer(text, StringPool.COMMA);

        // Skip symbol

        st.nextToken();

        rate = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());
    } catch (Exception e) {
        throw new WebCacheException(e);
    }

    return new Currency(symbol, rate);
}

From source file:com.liferay.portlet.network.util.DNSLookupWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    DNSLookup dnsLookup = null;//w w  w .jav a 2 s . c  o m

    try {
        String results = null;

        char[] array = _domain.trim().toCharArray();

        for (int i = 0; i < array.length; i++) {
            if ((array[i] != '.') && !Character.isDigit(array[i])) {
                InetAddress ia = InetAddress.getByName(_domain);

                results = ia.getHostAddress();

                break;
            }
        }

        if (results == null) {
            InetAddress[] ia = InetAddress.getAllByName(_domain);

            if (ia.length == 0) {
                results = StringPool.BLANK;
            } else {
                StringBundler sb = new StringBundler(ia.length * 2 - 1);

                for (int i = 0; i < ia.length; i++) {
                    sb.append(ia[i].getHostName());

                    if (i + 1 <= ia.length) {
                        sb.append(",");
                    }
                }

                results = sb.toString();
            }
        }

        dnsLookup = new DNSLookup(_domain, results);
    } catch (Exception e) {
        throw new WebCacheException(_domain + " " + e.toString());
    }

    return dnsLookup;
}

From source file:com.liferay.portlet.network.util.WhoisWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    Whois whois = null;/*from  ww w  .j  ava 2s  . c  o  m*/

    try {
        Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);

        UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
                new InputStreamReader(socket.getInputStream()));

        PrintStream out = new PrintStream(socket.getOutputStream());

        out.println(_domain);

        StringBundler sb = new StringBundler();
        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
            if (line.startsWith("Results ")) {
                break;
            }

            sb.append(line).append("\n");
        }

        unsyncBufferedReader.close();
        socket.close();

        whois = new Whois(_domain, StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
    } catch (Exception e) {
        throw new WebCacheException(_domain + " " + e.toString());
    }

    return whois;
}

From source file:com.liferay.portlet.rss.util.RSSWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    SyndFeed feed = null;/*w ww  .  j  a v a  2 s  . co m*/

    try {

        // com.liferay.portal.kernel.util.HttpUtil will break the connection
        // if it spends more than 5 seconds looking up a location. However,
        // German umlauts do not get encoded correctly. This may be a bug
        // with commons-httpclient or with how FeedParser uses
        // java.io.Reader.

        // Use http://xml.newsisfree.com/feeds/29/629.xml and
        // http://test.domosoft.com/up/RSS to test if German umlauts show
        // up correctly.

        /*Reader reader = new StringReader(
           new String(HttpUtil.URLtoByteArray(_url)));
                
        channel = FeedParser.parse(builder, reader);*/

        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(_url);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        httpImpl.proxifyState(httpClient.getState(), hostConfiguration);

        HttpClientParams httpClientParams = httpClient.getParams();

        httpClientParams.setConnectionManagerTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);
        httpClientParams.setSoTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);

        GetMethod getMethod = new GetMethod(_url);

        httpClient.executeMethod(hostConfiguration, getMethod);

        SyndFeedInput input = new SyndFeedInput();

        feed = input.build(new XmlReader(getMethod.getResponseBodyAsStream()));
    } catch (Exception e) {
        throw new WebCacheException(_url + " " + e.toString());
    }

    return feed;
}

From source file:com.liferay.portlet.translator.util.TranslationWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    Translation translation = new Translation(_translationId, _fromText);

    try {//w w w.  jav  a  2  s.co m
        StringBundler sb = new StringBundler(6);

        sb.append("http://babelfish.yahoo.com/translate_txt?");
        sb.append("ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext");
        sb.append("&trtext=");
        sb.append(HttpUtil.encodeURL(_fromText));
        sb.append("&lp=");
        sb.append(_translationId);

        String text = HttpUtil.URLtoString(new URL(sb.toString()));

        int x = text.indexOf("<div id=\"result\">");

        x = text.indexOf(">", x) + 1;
        x = text.indexOf(">", x) + 1;

        int y = text.indexOf("</div>", x);

        String toText = text.substring(x, y).trim();

        toText = StringUtil.replace(toText, CharPool.NEW_LINE, CharPool.SPACE);

        translation.setToText(toText);
    } catch (Exception e) {
        throw new WebCacheException(e);
    }

    return translation;
}

From source file:com.liferay.randombibleverse.util.VerseWebCacheItem.java

License:Open Source License

@Override
public Object convert(String key) throws WebCacheException {
    try {/*from w w w  .ja  va2 s . c o m*/
        Verse verse = null;

        if (StringUtil.equalsIgnoreCase(_language, "fi")) {
            verse = _getUskonkirjat(verse);
        } else {
            verse = _getBiblegateway(verse);
        }

        return verse;
    } catch (Exception e) {
        throw new WebCacheException(_location + " " + _versionId + " " + e.toString());
    }
}

From source file:com.liferay.stocks.util.StocksWebCacheItem.java

License:Open Source License

@Override
public Object convert(String key) throws WebCacheException {
    String symbol = _symbol;/*  w ww . j a  v a2 s . c o m*/
    double lastTrade = 0.0;
    double change = 0.0;
    double open = 0.0;
    double dayHigh = 0.0;
    double dayLow = 0.0;
    long volume = 0;

    Stocks stocks = new Stocks(symbol, lastTrade, change, open, dayHigh, dayLow, volume);

    try {
        String text = HttpUtil
                .URLtoString("http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv");

        StringTokenizer st = new StringTokenizer(text, StringPool.COMMA);

        // Skip symbol

        st.nextToken();

        try {
            lastTrade = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());

            stocks.setLastTrade(lastTrade);
        } catch (NumberFormatException nfe) {
            stocks.setLastTradeAvailable(false);
        }

        // Skip date and time

        st.nextToken();
        st.nextToken();

        try {
            change = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());

            stocks.setChange(change);
        } catch (NumberFormatException nfe) {
            stocks.setChangeAvailable(false);
        }

        try {
            open = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());

            stocks.setOpen(open);
        } catch (NumberFormatException nfe) {
            stocks.setOpenAvailable(false);
        }

        try {
            dayHigh = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());

            stocks.setDayHigh(dayHigh);
        } catch (NumberFormatException nfe) {
            stocks.setDayHighAvailable(false);
        }

        try {
            dayLow = GetterUtil.getDouble(st.nextToken().replace('"', ' ').trim());

            stocks.setDayLow(dayLow);
        } catch (NumberFormatException nfe) {
            stocks.setDayLowAvailable(false);
        }

        try {
            volume = GetterUtil.getLong(st.nextToken().replace('"', ' ').trim());

            stocks.setVolume(volume);
        } catch (NumberFormatException nfe) {
            stocks.setVolumeAvailable(false);
        }

        if (!stocks.isValid()) {
            throw new WebCacheException(symbol);
        }
    } catch (Exception e) {
        throw new WebCacheException(symbol + " " + e.toString());
    }

    return stocks;
}

From source file:com.liferay.taglib.util.GetUrlWebCacheItem.java

License:Open Source License

public Object convert(String key) throws WebCacheException {
    String url = _url;//from  ww  w.  j  a v  a 2s .co m

    String content = null;

    try {
        content = HttpUtil.URLtoString(_url);
    } catch (Exception e) {
        throw new WebCacheException(url + " " + e.toString());
    }

    return content;
}

From source file:com.liferay.tich.util.EventsWebCacheItem.java

License:Open Source License

@Override
public Object convert(String key) throws WebCacheException {
    List<Event> events = new ArrayList<Event>();

    try {// w w w.j a  va2s.  c o m
        String text = HttpUtil.URLtoString("http://www.studylight.org/his/tich");

        int x = text.indexOf("<div class=\"border_bottom_medium\"");
        int y = text.indexOf("<p class=\"small\">", x);

        text = HtmlUtil.stripHtml(text.substring(x, y));

        String[] entries = StringUtil.split(text, "\n" + StringPool.DOUBLE_SPACE + "\n\t");

        for (String entry : entries) {
            String[] entryArray = StringUtil.split(entry, "\n\t");

            if (entryArray.length > 1) {
                Event event = new Event(GetterUtil.getInteger(entryArray[0]), entryArray[1]);

                events.add(event);
            }
        }
    } catch (Exception e) {
        throw new WebCacheException(e);
    }

    return events;
}

From source file:com.liferay.weather.util.WeatherWebCacheItem.java

License:Open Source License

@Override
public Object convert(String key) throws WebCacheException {
    Weather weather = null;/*from   ww  w  .  j a va 2s  .  c om*/

    try {
        weather = doConvert();
    } catch (Exception e) {
        throw new WebCacheException(_zip);
    }

    return weather;
}