001    /**
002     * Copyright (c) 2000-2010 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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.Tuple;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    import com.liferay.portal.util.Portal;
024    import com.liferay.portlet.blogs.model.BlogsEntry;
025    import com.liferay.portlet.messageboards.model.MBMessage;
026    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    
032    /**
033     * @author Alexander Chow
034     */
035    public class LinkbackConsumerUtil {
036    
037            public static void addNewTrackback(
038                    long messageId, String url, String entryUrl) {
039    
040                    _trackbacks.add(new Tuple(messageId, url, entryUrl));
041            }
042    
043            public static void verifyNewTrackbacks() {
044                    Tuple tuple = null;
045    
046                    while (!_trackbacks.isEmpty()) {
047                            synchronized (_trackbacks) {
048                                    tuple = _trackbacks.remove(0);
049                            }
050    
051                            long messageId = (Long)tuple.getObject(0);
052                            String url = (String)tuple.getObject(1);
053                            String entryUrl = (String)tuple.getObject(2);
054    
055                            _verifyTrackback(messageId, url, entryUrl);
056                    }
057            }
058    
059            public static void verifyPost(BlogsEntry entry, MBMessage message)
060                    throws Exception {
061    
062                    long messageId = message.getMessageId();
063                    String entryURL =
064                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
065                    String body = message.getBody();
066                    String url = null;
067    
068                    int start = body.indexOf("[url=");
069    
070                    if (start > -1) {
071                            start += "[url=".length();
072    
073                            int end = body.indexOf("]", start);
074    
075                            if (end > -1) {
076                                    url = body.substring(start, end);
077                            }
078                    }
079    
080                    if (Validator.isNotNull(url)) {
081                            long companyId = message.getCompanyId();
082                            long userId = message.getUserId();
083                            long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
084                                    companyId);
085    
086                            if (userId == defaultUserId) {
087                                    _verifyTrackback(messageId, url, entryURL);
088                            }
089                    }
090            }
091    
092            private static void _verifyTrackback(
093                    long messageId, String url, String entryURL) {
094    
095                    try {
096                            String result = HttpUtil.URLtoString(url);
097    
098                            if (result.contains(entryURL)) {
099                                    return;
100                            }
101                    }
102                    catch (Exception e) {
103                    }
104    
105                    try {
106                            MBMessageLocalServiceUtil.deleteDiscussionMessage(
107                                    messageId);
108                    }
109                    catch (Exception e) {
110                            _log.error(
111                                    "Error trying to delete trackback message " + messageId, e);
112                    }
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(LinkbackConsumerUtil.class);
116    
117            private static List<Tuple> _trackbacks =
118                    Collections.synchronizedList(new ArrayList<Tuple>());
119    
120    }