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            public LinkbackConsumerImpl() {
035                    this(CommentManagerUtil.getCommentManager());
036            }
037    
038            public LinkbackConsumerImpl(CommentManager commentManager) {
039                    _commentManager = commentManager;
040            }
041    
042            @Override
043            public void addNewTrackback(long commentId, String url, String entryURL) {
044                    _trackbacks.add(new Tuple(commentId, url, entryURL));
045            }
046    
047            @Override
048            public void verifyNewTrackbacks() {
049                    Tuple tuple = null;
050    
051                    while (!_trackbacks.isEmpty()) {
052                            synchronized (_trackbacks) {
053                                    tuple = _trackbacks.remove(0);
054                            }
055    
056                            long commentId = (Long)tuple.getObject(0);
057                            String url = (String)tuple.getObject(1);
058                            String entryUrl = (String)tuple.getObject(2);
059    
060                            verifyTrackback(commentId, url, entryUrl);
061                    }
062            }
063    
064            @Override
065            public void verifyTrackback(long commentId, String url, String entryURL) {
066                    try {
067                            String result = HttpUtil.URLtoString(url);
068    
069                            if (result.contains(entryURL)) {
070                                    return;
071                            }
072                    }
073                    catch (Exception e) {
074                    }
075    
076                    try {
077                            _commentManager.deleteComment(commentId);
078                    }
079                    catch (Exception e) {
080                            _log.error("Unable to delete trackback comment " + commentId, e);
081                    }
082            }
083    
084            private static final Log _log = LogFactoryUtil.getLog(
085                    LinkbackConsumerImpl.class);
086    
087            private final CommentManager _commentManager;
088            private final List<Tuple> _trackbacks = Collections.synchronizedList(
089                    new ArrayList<Tuple>());
090    
091    }