Example usage for com.liferay.portal.util PropsValues BLOGS_LINKBACK_EXCERPT_LENGTH

List of usage examples for com.liferay.portal.util PropsValues BLOGS_LINKBACK_EXCERPT_LENGTH

Introduction

In this page you can find the example usage for com.liferay.portal.util PropsValues BLOGS_LINKBACK_EXCERPT_LENGTH.

Prototype

int BLOGS_LINKBACK_EXCERPT_LENGTH

To view the source code for com.liferay.portal.util PropsValues BLOGS_LINKBACK_EXCERPT_LENGTH.

Click Source Link

Usage

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.jav a 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.PingbackMethodImplTest.java

License:Open Source License

@Test
public void testGetExcerpt() throws Exception {
    int previous = PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH;

    Whitebox.setInternalState(PropsValues.class, "BLOGS_LINKBACK_EXCERPT_LENGTH", 4);

    try {//from  ww  w  .ja v  a 2  s.  c  o  m
        whenHttpURLToString("<body><a href='http://" + _TARGET_URI + "'>12345</a></body>");

        execute();

        verifyExcerpt("1...");
    } finally {
        Whitebox.setInternalState(PropsValues.class, "BLOGS_LINKBACK_EXCERPT_LENGTH", previous);
    }
}

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

License:Open Source License

@Test
public void testGetExcerptWhenAnchorHasTwoParents() throws Exception {
    int previous = PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH;

    Whitebox.setInternalState(PropsValues.class, "BLOGS_LINKBACK_EXCERPT_LENGTH", 18);

    try {// w w  w. j  a  va2 s.  com
        whenHttpURLToString("<body>_____<p>12345<span>67890" + "<a href='http://" + _TARGET_URI
                + "'>Liferay</a>" + "12345</span>67890</p>_____</body>");

        execute();

        verifyExcerpt("1234567890Lifer...");
    } finally {
        Whitebox.setInternalState(PropsValues.class, "BLOGS_LINKBACK_EXCERPT_LENGTH", previous);
    }
}

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

License:Open Source License

protected void pingTrackbacks(BlogsEntry entry, String[] trackbacks, boolean pingOldTrackbacks,
        ServiceContext serviceContext) throws PortalException {

    if (!PropsValues.BLOGS_TRACKBACK_ENABLED || !entry.isAllowTrackbacks() || !entry.isApproved()) {

        return;/*w w w. ja v a 2s  .c  om*/
    }

    HttpServletRequest request = serviceContext.getRequest();

    if (request == null) {
        return;
    }

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);

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

    Map<String, String> parts = new HashMap<>();

    String excerpt = StringUtil.shorten(HtmlUtil.extractText(entry.getContent()),
            PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH);
    String url = layoutFullURL + Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();

    parts.put("title", entry.getTitle());
    parts.put("excerpt", excerpt);
    parts.put("url", url);
    parts.put("blog_name", entry.getUserName());

    Set<String> trackbacksSet = null;

    if (ArrayUtil.isNotEmpty(trackbacks)) {
        trackbacksSet = SetUtil.fromArray(trackbacks);
    } else {
        trackbacksSet = new HashSet<>();
    }

    if (pingOldTrackbacks) {
        trackbacksSet.addAll(SetUtil.fromArray(StringUtil.split(entry.getTrackbacks())));

        entry.setTrackbacks(StringPool.BLANK);

        blogsEntryPersistence.update(entry);
    }

    Set<String> oldTrackbacks = SetUtil.fromArray(StringUtil.split(entry.getTrackbacks()));

    Set<String> validTrackbacks = new HashSet<>();

    for (String trackback : trackbacksSet) {
        if (oldTrackbacks.contains(trackback)) {
            continue;
        }

        try {
            if (LinkbackProducerUtil.sendTrackback(trackback, parts)) {
                validTrackbacks.add(trackback);
            }
        } catch (Exception e) {
            _log.error("Error while sending trackback at " + trackback, e);
        }
    }

    if (!validTrackbacks.isEmpty()) {
        String newTrackbacks = StringUtil.merge(validTrackbacks);

        if (Validator.isNotNull(entry.getTrackbacks())) {
            newTrackbacks += StringPool.COMMA + entry.getTrackbacks();
        }

        entry.setTrackbacks(newTrackbacks);

        blogsEntryPersistence.update(entry);
    }
}

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

License:Open Source License

protected void pingTrackbacks(BlogsEntry entry, String[] trackbacks, boolean pingOldTrackbacks,
        ServiceContext serviceContext) throws SystemException {

    if (!PropsValues.BLOGS_TRACKBACK_ENABLED || !entry.isAllowTrackbacks() || !entry.isApproved()) {

        return;/*w  w  w .  j  a v  a2  s  .  c o m*/
    }

    String layoutFullURL = serviceContext.getLayoutFullURL();

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

    Map<String, String> parts = new HashMap<String, String>();

    String excerpt = StringUtil.shorten(HtmlUtil.extractText(entry.getContent()),
            PropsValues.BLOGS_LINKBACK_EXCERPT_LENGTH);
    String url = layoutFullURL + Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();

    parts.put("title", entry.getTitle());
    parts.put("excerpt", excerpt);
    parts.put("url", url);
    parts.put("blog_name", entry.getUserName());

    Set<String> trackbacksSet = null;

    if (Validator.isNotNull(trackbacks)) {
        trackbacksSet = SetUtil.fromArray(trackbacks);
    } else {
        trackbacksSet = new HashSet<String>();
    }

    if (pingOldTrackbacks) {
        trackbacksSet.addAll(SetUtil.fromArray(StringUtil.split(entry.getTrackbacks())));

        entry.setTrackbacks(StringPool.BLANK);

        blogsEntryPersistence.update(entry, false);
    }

    Set<String> oldTrackbacks = SetUtil.fromArray(StringUtil.split(entry.getTrackbacks()));

    Set<String> validTrackbacks = new HashSet<String>();

    for (String trackback : trackbacksSet) {
        if (oldTrackbacks.contains(trackback)) {
            continue;
        }

        try {
            if (LinkbackProducerUtil.sendTrackback(trackback, parts)) {
                validTrackbacks.add(trackback);
            }
        } catch (Exception e) {
            _log.error("Error while sending trackback at " + trackback, e);
        }
    }

    if (!validTrackbacks.isEmpty()) {
        String newTrackbacks = StringUtil.merge(validTrackbacks);

        if (Validator.isNotNull(entry.getTrackbacks())) {
            newTrackbacks += StringPool.COMMA + entry.getTrackbacks();
        }

        entry.setTrackbacks(newTrackbacks);

        blogsEntryPersistence.update(entry, false);
    }
}

From source file:com.liferay.portlet.blogs.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   w w w  . j ava 2s  .  co  m

    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;
}