Example usage for com.liferay.portal.kernel.util HttpUtil URLtoString

List of usage examples for com.liferay.portal.kernel.util HttpUtil URLtoString

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util HttpUtil URLtoString.

Prototype

public static String URLtoString(URL url) throws IOException 

Source Link

Document

This method only uses the default Commons HttpClient implementation when the URL object represents a HTTP resource.

Usage

From source file:br.com.thiagomoreira.liferay.plugins.MarkdownDisplayPortlet.java

License:Apache License

@Override
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {

    PortletPreferences preferences = request.getPreferences();
    String markdownURL = preferences.getValue("markdownURL", null);
    int timeToLive = 60 * GetterUtil.getInteger(preferences.getValue("timeToLive", "1"));
    boolean autolinks = GetterUtil.getBoolean(preferences.getValue("autolinks", "false"));
    boolean fencedBlockCodes = GetterUtil.getBoolean(preferences.getValue("fencedBlockCodes", "false"));
    boolean tables = GetterUtil.getBoolean(preferences.getValue("tables", "false"));

    if (Validator.isNotNull(markdownURL)) {
        PortalCache<Serializable, Object> portalCache = SingleVMPoolUtil
                .getCache(MarkdownDisplayPortlet.class.getName());

        String content = (String) portalCache.get(markdownURL);

        if (content == null) {
            int options = Extensions.NONE;
            if (autolinks) {
                options = options | Extensions.AUTOLINKS;
            }/*from  w w w. j  a v a  2  s  . c o  m*/
            if (fencedBlockCodes) {
                options = options | Extensions.FENCED_CODE_BLOCKS;
            }
            if (tables) {
                options = options | Extensions.TABLES;
            }

            PegDownProcessor processor = new PegDownProcessor(options);

            String markdownSource = HttpUtil.URLtoString(markdownURL);
            content = processor.markdownToHtml(markdownSource);

            portalCache.put(markdownURL, content, timeToLive);
        }

        request.setAttribute("content", content);
    } else {
        request.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);
    }

    super.render(request, response);
}

From source file:com.beorn.onlinepayment.geoip.GeoIpUtil.java

License:Open Source License

/**
 * Geolocalize an ip using a json service.
 * //ww  w.j  a v a2s.c  om
 * The url defined in the porlet.properties by geoip.url will be called,
 * replacing the {0} marker by the ip, and the
 * geoip.response.country-code-parameter key of the returned json response
 * will be extracted and returned as the country code.
 * 
 * @param ip
 *            the ip to geolocalize
 * @return a country code corresponding to the ip parameter
 * @throws IOException
 *             when there is an error calling the json service
 * @throws JSONException
 *             when the response isn't a valid json string
 */
public static String geolocalizeIp(String ip) throws IOException, JSONException {

    ip = StringUtil.trim(ip);

    String key = ip;
    String countryCode = (String) _portalCache.get(key);
    if (countryCode == null) {
        Options options = new Options();
        options.setLocation(MessageFormat.format(PropsValues.GEOIP_URL, ip));

        String response = HttpUtil.URLtoString(options);
        JSONObject jsonResponse = new JSONObject(response);

        countryCode = jsonResponse.getString(PropsValues.GEOIP_RESPONSE_COUNTRY_CODE_PARAMETER);
        _log.info("Found country code " + countryCode + " for ip " + ip);

        _portalCache.put(key, countryCode);
    }

    return countryCode;
}

From source file:com.liferay.akismet.util.AkismetUtil.java

License:Open Source License

private static String _sendRequest(String location, Map<String, String> parts) {

    Http.Options options = new Http.Options();

    options.addHeader(HttpHeaders.USER_AGENT, "Akismet/2.5.3");
    options.setLocation(location);//  ww w .j  a  v a  2s. c  o  m
    options.setParts(parts);
    options.setPost(true);

    try {
        return HttpUtil.URLtoString(options);
    } catch (IOException ioe) {
        _log.error(ioe, ioe);
    }

    return StringPool.BLANK;
}

From source file:com.liferay.bbb.util.BBBAPIUtil.java

License:Open Source License

protected static Document execute(BBBServer bbbServer, String methodName, String queryString)
        throws PortalException {

    try {/*w  w  w.ja  v  a 2s. co  m*/
        String url = getURL(bbbServer, methodName, queryString);

        String xml = HttpUtil.URLtoString(url);

        return SAXReaderUtil.read(xml);
    } catch (DocumentException de) {
        throw new SystemException(de);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}

From source file:com.liferay.blogs.internal.util.PingbackMethodImpl.java

License:Open Source License

protected String getExcerpt() throws IOException {
    String html = HttpUtil.URLtoString(_sourceURI);

    Source source = new Source(html);

    source.fullSequentialParse();/*from   ww w.  j a va 2  s  . c  om*/

    List<Element> elements = source.getAllElements("a");

    for (Element element : elements) {
        String href = GetterUtil.getString(element.getAttributeValue("href"));

        if (href.equals(_targetURI)) {
            element = element.getParentElement();

            TextExtractor textExtractor = new TextExtractor(element);

            String body = textExtractor.toString();

            if (body.length() < PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH) {
                element = element.getParentElement();

                if (element != null) {
                    textExtractor = new TextExtractor(element);

                    body = textExtractor.toString();
                }
            }

            return StringUtil.shorten(body, PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH);
        }
    }

    return StringPool.BLANK;
}

From source file:com.liferay.blogs.internal.util.PingbackMethodImpl.java

License:Open Source License

protected void validateSource() throws Exception {
    Source source = null;//from ww  w  .j  a va 2  s .  com

    try {
        String html = HttpUtil.URLtoString(_sourceURI);

        source = new Source(html);
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug(e, e);
        }

        throw new UnavailableSourceURIException("Error accessing source URI", e);
    }

    List<StartTag> startTags = source.getAllStartTags("a");

    for (StartTag startTag : startTags) {
        String href = GetterUtil.getString(startTag.getAttributeValue("href"));

        if (href.equals(_targetURI)) {
            return;
        }
    }

    throw new InvalidSourceURIException("Unable to find target URI in source");
}

From source file:com.liferay.blogs.service.impl.BlogsEntryLocalServiceImpl.java

License:Open Source License

protected void pingGoogle(BlogsEntry entry, ServiceContext serviceContext) throws PortalException {

    if (!PropsValues.BLOGS_PING_GOOGLE_ENABLED || !entry.isApproved()) {
        return;/*from w  ww  . j  av  a2s  . c o m*/
    }

    String portletId = PortletProviderUtil.getPortletId(BlogsEntry.class.getName(),
            PortletProvider.Action.MANAGE);

    if (Validator.isNull(portletId)) {
        if (_log.isDebugEnabled()) {
            _log.debug("Not pinging Google because there is no blogs portlet " + "provider");
        }

        return;
    }

    String layoutFullURL = PortalUtil.getLayoutFullURL(serviceContext.getScopeGroupId(), portletId);

    if (Validator.isNull(layoutFullURL)) {
        return;
    }

    if (layoutFullURL.contains("://localhost")) {
        if (_log.isDebugEnabled()) {
            _log.debug("Not pinging Google because of localhost URL " + layoutFullURL);
        }

        return;
    }

    Group group = groupPersistence.findByPrimaryKey(entry.getGroupId());

    StringBundler sb = new StringBundler(6);

    String name = group.getDescriptiveName();
    String url = layoutFullURL + Portal.FRIENDLY_URL_SEPARATOR + "blogs";
    String changesURL = serviceContext.getPathMain() + "/blogs/rss";

    sb.append("http://blogsearch.google.com/ping?name=");
    sb.append(HttpUtil.encodeURL(name));
    sb.append("&url=");
    sb.append(HttpUtil.encodeURL(url));
    sb.append("&changesURL=");
    sb.append(HttpUtil.encodeURL(changesURL));

    String location = sb.toString();

    if (_log.isInfoEnabled()) {
        _log.info("Pinging Google at " + location);
    }

    try {
        String response = HttpUtil.URLtoString(sb.toString());

        if (_log.isInfoEnabled()) {
            _log.info("Google ping response: " + response);
        }
    } catch (IOException ioe) {
        _log.error("Unable to ping Google at " + location, ioe);
    }
}

From source file:com.liferay.calendarimporter.source.LotusNotesImportSource.java

License:Open Source License

@Override
protected void doExecuteImport(Calendar calendar, UnicodeProperties typeSettingsProperties) throws Exception {

    String url = typeSettingsProperties.getProperty("url");
    String userName = typeSettingsProperties.getProperty("user-name");
    String password = typeSettingsProperties.getProperty("password");

    if (!StringUtil.endsWith(url, CharPool.SLASH)) {
        url += CharPool.SLASH;// ww  w.ja  va2 s. com
    }

    url += _EVENTS_PATH;

    Http.Options options = new Http.Options();

    options.setAuth(null, 0, null, userName, password);
    options.setLocation(url);

    String json = HttpUtil.URLtoString(options);

    if (Validator.isNull(json)) {
        return;
    }

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(json);

    JSONArray eventsJSON = jsonObject.getJSONArray("events");

    importEvents(calendar, eventsJSON);
}

From source file:com.liferay.content.targeting.rule.weather.WeatherRule.java

License:Open Source License

protected String getUserWeather(AnonymousUser anonymousUser) throws PortalException, SystemException {

    //User user = anonymousUser.getUser();

    //String city = getCityFromUserProfile(user.getContactId(), user.getCompanyId());

    String city = getCityFromIPAddress(anonymousUser.getLastIp());

    Http.Options options = new Http.Options();

    String location = HttpUtil.addParameter(API_URL, "q", city);
    location = HttpUtil.addParameter(location, "format", "json");

    options.setLocation(location);//ww w . ja  v  a 2  s . co m

    int weatherCode = 0;

    try {
        String text = HttpUtil.URLtoString(options);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(text);

        weatherCode = jsonObject.getJSONArray("weather").getJSONObject(0).getInt("id");
    } catch (Exception e) {
        _log.error(e);
    }

    return getWeatherFromCode(weatherCode);
}

From source file:com.liferay.contenttargeting.rules.ipgeocode.util.IPGeocodeUtil.java

License:Open Source License

public static IPInfo getIPInfo(String ipAddress) {
    Http.Options options = new Http.Options();

    options.setLocation("http://freegeoip.net/json/" + ipAddress);

    try {//  w  w w.  j  a va 2s.  c om
        String text = HttpUtil.URLtoString(options);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(text);

        return new IPInfo(jsonObject);
    } catch (Exception e) {
    }

    return null;
}