1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.Tuple;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.service.UserLocalServiceUtil;
23  import com.liferay.portal.util.Portal;
24  import com.liferay.portlet.blogs.model.BlogsEntry;
25  import com.liferay.portlet.messageboards.model.MBMessage;
26  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * <a href="LinkbackConsumerUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   */
37  public class LinkbackConsumerUtil {
38  
39      public static void addNewTrackback(
40          long messageId, String url, String entryUrl) {
41  
42          _trackbacks.add(new Tuple(messageId, url, entryUrl));
43      }
44  
45      public static void verifyNewTrackbacks() {
46          Tuple tuple = null;
47  
48          while (!_trackbacks.isEmpty()) {
49              synchronized (_trackbacks) {
50                  tuple = _trackbacks.remove(0);
51              }
52  
53              long messageId = (Long)tuple.getObject(0);
54              String url = (String)tuple.getObject(1);
55              String entryUrl = (String)tuple.getObject(2);
56  
57              _verifyTrackback(messageId, url, entryUrl);
58          }
59      }
60  
61      public static void verifyPost(BlogsEntry entry, MBMessage message)
62          throws Exception {
63  
64          long messageId = message.getMessageId();
65          String entryURL =
66              Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
67          String body = message.getBody();
68          String url = null;
69  
70          int start = body.indexOf("[url=");
71  
72          if (start > -1) {
73              start += "[url=".length();
74  
75              int end = body.indexOf("]", start);
76  
77              if (end > -1) {
78                  url = body.substring(start, end);
79              }
80          }
81  
82          if (Validator.isNotNull(url)) {
83              long companyId = message.getCompanyId();
84              long userId = message.getUserId();
85              long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
86                  companyId);
87  
88              if (userId == defaultUserId) {
89                  _verifyTrackback(messageId, url, entryURL);
90              }
91          }
92      }
93  
94      private static void _verifyTrackback(
95          long messageId, String url, String entryURL) {
96  
97          try {
98              String result = HttpUtil.URLtoString(url);
99  
100             if (result.contains(entryURL)) {
101                 return;
102             }
103         }
104         catch (Exception e) {
105         }
106 
107         try {
108             MBMessageLocalServiceUtil.deleteDiscussionMessage(
109                 messageId);
110         }
111         catch (Exception e) {
112             _log.error(
113                 "Error trying to delete trackback message " + messageId, e);
114         }
115     }
116 
117     private static Log _log = LogFactoryUtil.getLog(LinkbackConsumerUtil.class);
118 
119     private static List<Tuple> _trackbacks =
120         Collections.synchronizedList(new ArrayList<Tuple>());
121 
122 }