1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.Tuple;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.util.Portal;
32  import com.liferay.portlet.blogs.model.BlogsEntry;
33  import com.liferay.portlet.messageboards.model.MBMessage;
34  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
35  
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  
40  /**
41   * <a href="TrackbackVerifierUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Alexander Chow
44   *
45   */
46  public class TrackbackVerifierUtil {
47  
48      public static void addNewPost(
49          long messageId, String url, String entryUrl) {
50  
51          _trackbacks.add(new Tuple(messageId, url, entryUrl));
52      }
53  
54      public static void verifyNewPosts() {
55          Tuple tuple = null;
56  
57          while (!_trackbacks.isEmpty()) {
58              synchronized (_trackbacks) {
59                  tuple = _trackbacks.remove(0);
60              }
61  
62              long messageId = (Long)tuple.getObject(0);
63              String url = (String)tuple.getObject(1);
64              String entryUrl = (String)tuple.getObject(2);
65  
66              _verifyPost(messageId, url, entryUrl);
67          }
68      }
69  
70      public static void verifyPost(BlogsEntry entry, MBMessage message)
71          throws Exception {
72  
73          long messageId = message.getMessageId();
74          String entryURL =
75              Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
76          String body = message.getBody();
77          String url = null;
78  
79          int start = body.indexOf("[url=");
80  
81          if (start > -1) {
82              start += "[url=".length();
83  
84              int end = body.indexOf("]", start);
85  
86              if (end > -1) {
87                  url = body.substring(start, end);
88              }
89          }
90  
91          if (Validator.isNotNull(url)) {
92              long companyId = message.getCompanyId();
93              long userId = message.getUserId();
94              long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
95                  companyId);
96  
97              if (userId == defaultUserId) {
98                  _verifyPost(messageId, url, entryURL);
99              }
100         }
101     }
102 
103     private static void _verifyPost(
104         long messageId, String url, String entryURL) {
105 
106         try {
107             String result = HttpUtil.URLtoString(url);
108 
109             if (result.contains(entryURL)) {
110                 return;
111             }
112         }
113         catch (Exception e) {
114         }
115 
116         try {
117             MBMessageLocalServiceUtil.deleteDiscussionMessage(
118                 messageId);
119         }
120         catch (Exception e) {
121             _log.error(
122                 "Error trying to delete trackback message " + messageId, e);
123         }
124     }
125 
126     private static Log _log =
127         LogFactoryUtil.getLog(TrackbackVerifierUtil.class);
128 
129     private static List<Tuple> _trackbacks =
130         Collections.synchronizedList(new ArrayList<Tuple>());
131 
132 }