001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger <troger@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.activity; 019 020import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD; 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027import org.apache.commons.io.FilenameUtils; 028import org.apache.commons.lang.StringEscapeUtils; 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentLocation; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.IdRef; 034import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl; 035import org.nuxeo.ecm.core.io.download.DownloadService; 036import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService; 037import org.nuxeo.ecm.platform.url.DocumentViewImpl; 038import org.nuxeo.ecm.platform.url.api.DocumentView; 039import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 040import org.nuxeo.ecm.user.center.profile.UserProfileService; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Helper class to compute links for {@link ActivityMessage} content from an activity attributes. 045 * 046 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 047 * @since 5.6 048 */ 049public class ActivityMessageHelper { 050 051 private ActivityMessageHelper() { 052 // helper class 053 } 054 055 public static String getDocumentLink(String documentActivityObject, String displayValue) { 056 documentActivityObject = StringEscapeUtils.escapeHtml(documentActivityObject); 057 displayValue = StringEscapeUtils.escapeHtml(displayValue); 058 String link = "<a href=\"%s\" target=\"_top\">%s</a>"; 059 return String.format( 060 link, 061 getDocumentURL(ActivityHelper.getRepositoryName(documentActivityObject), 062 ActivityHelper.getDocumentId(documentActivityObject)), displayValue); 063 } 064 065 public static String getDocumentURL(String repositoryName, String documentId) { 066 DocumentLocation docLoc = new DocumentLocationImpl(repositoryName, new IdRef(documentId)); 067 DocumentView docView = new DocumentViewImpl(docLoc, "view_documents"); 068 URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class); 069 return urlPolicyService.getUrlFromDocumentView("id", docView, VirtualHostHelper.getContextPathProperty()); 070 } 071 072 public static String getUserProfileLink(String userActivityObject, String displayValue) { 073 userActivityObject = StringEscapeUtils.escapeHtml(userActivityObject); 074 displayValue = StringEscapeUtils.escapeHtml(displayValue); 075 String link = "<span class=\"username\"><a href=\"%s\" target=\"_top\" title=\"%s\">%s</a></span>"; 076 String username = ActivityHelper.getUsername(userActivityObject); 077 return String.format(link, getUserProfileURL(username), username, displayValue); 078 } 079 080 public static String getUserProfileURL(String username) { 081 Map<String, String> params = new HashMap<String, String>(); 082 params.put("username", username); 083 DocumentView docView = new DocumentViewImpl(null, null, params); 084 URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class); 085 return VirtualHostHelper.getContextPathProperty() + "/" 086 + urlPolicyService.getUrlFromDocumentView("user", docView, null); 087 } 088 089 public static String getUserAvatarURL(CoreSession session, String username) { 090 UserProfileService userProfileService = Framework.getLocalService(UserProfileService.class); 091 DocumentModel profile = userProfileService.getUserProfileDocument(username, session); 092 Blob avatar = (Blob) profile.getPropertyValue(USER_PROFILE_AVATAR_FIELD); 093 if (avatar != null) { 094 DownloadService downloadService = Framework.getService(DownloadService.class); 095 String filename = username + "." + FilenameUtils.getExtension(avatar.getFilename()); 096 return VirtualHostHelper.getContextPathProperty() + "/" 097 + downloadService.getDownloadUrl(profile, USER_PROFILE_AVATAR_FIELD, filename); 098 } else { 099 return VirtualHostHelper.getContextPathProperty() + "/icons/missing_avatar.png"; 100 } 101 } 102 103 public static Pattern HTTP_URL_PATTERN = Pattern.compile("\\b(https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])"); 104 105 public static String replaceURLsByLinks(String message) { 106 String escapedMessage = StringEscapeUtils.escapeHtml(message); 107 Matcher m = HTTP_URL_PATTERN.matcher(escapedMessage); 108 StringBuffer sb = new StringBuffer(escapedMessage.length()); 109 while (m.find()) { 110 String url = m.group(1); 111 m.appendReplacement(sb, computeLinkFor(url)); 112 } 113 m.appendTail(sb); 114 return sb.toString(); 115 } 116 117 private static String computeLinkFor(String url) { 118 return "<a href=\"" + url + "\" target=\"_top\">" + url + "</a>"; 119 } 120 121}