001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.linkback;
016    
017    import com.liferay.portal.kernel.comment.CommentManager;
018    import com.liferay.portal.kernel.comment.CommentManagerUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.Tuple;
023    
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * @author Alexander Chow
030     * @author Andr?? de Oliveira
031     */
032    public class LinkbackConsumerImpl implements LinkbackConsumer {
033    
034            @Override
035            public void addNewTrackback(long commentId, String url, String entryURL) {
036                    _trackbacks.add(new Tuple(commentId, url, entryURL));
037            }
038    
039            @Override
040            public void verifyNewTrackbacks() {
041                    Tuple tuple = null;
042    
043                    while (!_trackbacks.isEmpty()) {
044                            synchronized (_trackbacks) {
045                                    tuple = _trackbacks.remove(0);
046                            }
047    
048                            long commentId = (Long)tuple.getObject(0);
049                            String url = (String)tuple.getObject(1);
050                            String entryUrl = (String)tuple.getObject(2);
051    
052                            verifyTrackback(commentId, url, entryUrl);
053                    }
054            }
055    
056            @Override
057            public void verifyTrackback(long commentId, String url, String entryURL) {
058                    try {
059                            String result = HttpUtil.URLtoString(url);
060    
061                            if (result.contains(entryURL)) {
062                                    return;
063                            }
064                    }
065                    catch (Exception e) {
066                    }
067    
068                    try {
069                            _commentManager.deleteComment(commentId);
070                    }
071                    catch (Exception e) {
072                            _log.error("Unable to delete trackback comment " + commentId, e);
073                    }
074            }
075    
076            protected void setCommentManager(CommentManager commentManager) {
077                    _commentManager = commentManager;
078            }
079    
080            private static final Log _log = LogFactoryUtil.getLog(
081                    LinkbackConsumerImpl.class);
082    
083            private CommentManager _commentManager =
084                    CommentManagerUtil.getCommentManager();
085            private final List<Tuple> _trackbacks = Collections.synchronizedList(
086                    new ArrayList<Tuple>());
087    
088    }