PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/dxp/apps/akismet/akismet-client/src/main/java/com/liferay/akismet/client/AkismetClient.java

http://github.com/liferay/liferay-portal
Java | 305 lines | 220 code | 69 blank | 16 comment | 19 complexity | 8142d0137f0ee9f02393172bd69163aa MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * The contents of this file are subject to the terms of the Liferay Enterprise
  5. * Subscription License ("License"). You may not use this file except in
  6. * compliance with the License. You can obtain a copy of the License by
  7. * contacting Liferay, Inc. See the License for the specific language governing
  8. * permissions and limitations under the License, including but not limited to
  9. * distribution rights of the Software.
  10. *
  11. *
  12. *
  13. */
  14. package com.liferay.akismet.client;
  15. import com.liferay.akismet.client.constants.AkismetConstants;
  16. import com.liferay.akismet.client.util.AkismetServiceConfigurationUtil;
  17. import com.liferay.akismet.model.AkismetEntry;
  18. import com.liferay.akismet.service.AkismetEntryLocalService;
  19. import com.liferay.message.boards.model.MBMessage;
  20. import com.liferay.petra.string.StringPool;
  21. import com.liferay.portal.kernel.exception.PortalException;
  22. import com.liferay.portal.kernel.log.Log;
  23. import com.liferay.portal.kernel.log.LogFactoryUtil;
  24. import com.liferay.portal.kernel.model.Company;
  25. import com.liferay.portal.kernel.model.User;
  26. import com.liferay.portal.kernel.service.CompanyLocalService;
  27. import com.liferay.portal.kernel.service.UserLocalService;
  28. import com.liferay.portal.kernel.servlet.HttpHeaders;
  29. import com.liferay.portal.kernel.util.HashMapBuilder;
  30. import com.liferay.portal.kernel.util.Http;
  31. import com.liferay.portal.kernel.util.Portal;
  32. import com.liferay.portal.kernel.util.StringBundler;
  33. import com.liferay.portal.kernel.util.StringUtil;
  34. import com.liferay.portal.kernel.util.Validator;
  35. import java.io.IOException;
  36. import java.util.Map;
  37. import org.osgi.service.component.annotations.Component;
  38. import org.osgi.service.component.annotations.Reference;
  39. /**
  40. * @author Jamie Sammons
  41. */
  42. @Component(immediate = true, service = AkismetClient.class)
  43. public class AkismetClient {
  44. public boolean hasRequiredInfo(String userIP, Map<String, String> headers) {
  45. if (headers == null) {
  46. return false;
  47. }
  48. String userAgent = headers.get(
  49. StringUtil.toLowerCase(HttpHeaders.USER_AGENT));
  50. if (Validator.isNull(userAgent)) {
  51. return false;
  52. }
  53. if (Validator.isNull(userIP)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. public boolean isSpam(
  59. long userId, String content, AkismetEntry akismetEntry)
  60. throws PortalException {
  61. StringBundler sb = new StringBundler(5);
  62. sb.append(Http.HTTP_WITH_SLASH);
  63. sb.append(AkismetServiceConfigurationUtil.getAPIKey());
  64. sb.append(StringPool.PERIOD);
  65. sb.append(AkismetConstants.URL_REST);
  66. sb.append(AkismetConstants.PATH_CHECK_SPAM);
  67. String location = sb.toString();
  68. User user = _userLocalService.getUser(userId);
  69. String response = _sendRequest(
  70. location, user.getCompanyId(), akismetEntry.getUserIP(),
  71. akismetEntry.getUserAgent(), akismetEntry.getReferrer(),
  72. akismetEntry.getPermalink(), akismetEntry.getType(),
  73. user.getFullName(), user.getEmailAddress(), content);
  74. if (Validator.isNull(response) || response.equals("invalid")) {
  75. _log.error("There was an issue with Akismet comment validation");
  76. return false;
  77. }
  78. else if (response.equals("true")) {
  79. if (_log.isDebugEnabled()) {
  80. _log.debug("Spam detected: " + akismetEntry.getPermalink());
  81. }
  82. return true;
  83. }
  84. if (_log.isDebugEnabled()) {
  85. _log.debug("Passed: " + akismetEntry.getPermalink());
  86. }
  87. return false;
  88. }
  89. public void submitHam(
  90. long companyId, String ipAddress, String userAgent, String referrer,
  91. String permalink, String commentType, String userName,
  92. String emailAddress, String content)
  93. throws PortalException {
  94. if (_log.isDebugEnabled()) {
  95. _log.debug("Submitting message as ham: " + permalink);
  96. }
  97. StringBundler sb = new StringBundler(5);
  98. sb.append(Http.HTTP_WITH_SLASH);
  99. sb.append(AkismetServiceConfigurationUtil.getAPIKey());
  100. sb.append(StringPool.PERIOD);
  101. sb.append(AkismetConstants.URL_REST);
  102. sb.append(AkismetConstants.PATH_SUBMIT_HAM);
  103. String location = sb.toString();
  104. String response = _sendRequest(
  105. location, companyId, ipAddress, userAgent, referrer, permalink,
  106. commentType, userName, emailAddress, content);
  107. if (Validator.isNull(response)) {
  108. _log.error("There was an issue submitting message as ham");
  109. }
  110. }
  111. public void submitHam(MBMessage mbMessage) throws PortalException {
  112. AkismetEntry akismetEntry = _akismetEntryLocalService.fetchAkismetEntry(
  113. MBMessage.class.getName(), mbMessage.getMessageId());
  114. if (akismetEntry == null) {
  115. return;
  116. }
  117. User user = _userLocalService.getUser(mbMessage.getUserId());
  118. String content = mbMessage.getSubject() + "\n\n" + mbMessage.getBody();
  119. submitHam(
  120. mbMessage.getCompanyId(), akismetEntry.getUserIP(),
  121. akismetEntry.getUserAgent(), akismetEntry.getReferrer(),
  122. akismetEntry.getPermalink(), akismetEntry.getType(),
  123. user.getFullName(), user.getEmailAddress(), content);
  124. }
  125. public void submitSpam(
  126. long companyId, String ipAddress, String userAgent, String referrer,
  127. String permalink, String commentType, String userName,
  128. String emailAddress, String content)
  129. throws PortalException {
  130. if (_log.isDebugEnabled()) {
  131. _log.debug("Submitting message as spam: " + permalink);
  132. }
  133. StringBundler sb = new StringBundler(5);
  134. sb.append(Http.HTTP_WITH_SLASH);
  135. sb.append(AkismetServiceConfigurationUtil.getAPIKey());
  136. sb.append(StringPool.PERIOD);
  137. sb.append(AkismetConstants.URL_REST);
  138. sb.append(AkismetConstants.PATH_SUBMIT_SPAM);
  139. String location = sb.toString();
  140. String response = _sendRequest(
  141. location, companyId, ipAddress, userAgent, referrer, permalink,
  142. commentType, userName, emailAddress, content);
  143. if (Validator.isNull(response) ||
  144. !verifyApiKey(
  145. companyId, AkismetServiceConfigurationUtil.getAPIKey())) {
  146. _log.error("There was an issue submitting message as spam");
  147. }
  148. }
  149. public void submitSpam(MBMessage mbMessage) throws PortalException {
  150. AkismetEntry akismetData = _akismetEntryLocalService.fetchAkismetEntry(
  151. MBMessage.class.getName(), mbMessage.getMessageId());
  152. if (akismetData == null) {
  153. return;
  154. }
  155. User user = _userLocalService.getUser(mbMessage.getUserId());
  156. String content = mbMessage.getSubject() + "\n\n" + mbMessage.getBody();
  157. submitSpam(
  158. mbMessage.getCompanyId(), akismetData.getUserIP(),
  159. akismetData.getUserAgent(), akismetData.getReferrer(),
  160. akismetData.getPermalink(), akismetData.getType(),
  161. user.getFullName(), user.getEmailAddress(), content);
  162. }
  163. public boolean verifyApiKey(long companyId, String apiKey)
  164. throws PortalException {
  165. String location =
  166. Http.HTTP_WITH_SLASH + AkismetConstants.URL_REST +
  167. AkismetConstants.PATH_VERIFY;
  168. Map<String, String> parts = HashMapBuilder.put(
  169. "blog", _getPortalURL(companyId)
  170. ).put(
  171. "key", apiKey
  172. ).build();
  173. String response = _sendRequest(location, parts);
  174. if (response.equals("valid")) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. private String _getPortalURL(long companyId) throws PortalException {
  180. Company company = _companyLocalService.getCompany(companyId);
  181. return _portal.getPortalURL(
  182. company.getVirtualHostname(), _portal.getPortalServerPort(false),
  183. false);
  184. }
  185. private String _sendRequest(
  186. String location, long companyId, String ipAddress, String userAgent,
  187. String referrer, String permalink, String commentType,
  188. String userName, String emailAddress, String content)
  189. throws PortalException {
  190. Map<String, String> parts = HashMapBuilder.put(
  191. "blog", _getPortalURL(companyId)
  192. ).put(
  193. "comment_author", userName
  194. ).put(
  195. "comment_author_email", emailAddress
  196. ).put(
  197. "comment_content", content
  198. ).put(
  199. "comment_type", commentType
  200. ).put(
  201. "permalink", permalink
  202. ).put(
  203. "referrer", referrer
  204. ).put(
  205. "user_agent", userAgent
  206. ).put(
  207. "user_ip", ipAddress
  208. ).build();
  209. return _sendRequest(location, parts);
  210. }
  211. private String _sendRequest(String location, Map<String, String> parts) {
  212. Http.Options options = new Http.Options();
  213. options.addHeader(HttpHeaders.USER_AGENT, "Akismet/2.5.3");
  214. options.setLocation(location);
  215. options.setParts(parts);
  216. options.setPost(true);
  217. try {
  218. return _http.URLtoString(options);
  219. }
  220. catch (IOException ioException) {
  221. _log.error(ioException, ioException);
  222. }
  223. return StringPool.BLANK;
  224. }
  225. private static final Log _log = LogFactoryUtil.getLog(AkismetClient.class);
  226. @Reference
  227. private AkismetEntryLocalService _akismetEntryLocalService;
  228. @Reference
  229. private CompanyLocalService _companyLocalService;
  230. @Reference
  231. private Http _http;
  232. @Reference
  233. private Portal _portal;
  234. @Reference
  235. private UserLocalService _userLocalService;
  236. }