001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.activity;
021
022import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.commons.io.FilenameUtils;
030import org.apache.commons.lang.StringEscapeUtils;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentLocation;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
037import org.nuxeo.ecm.core.io.download.DownloadService;
038import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService;
039import org.nuxeo.ecm.platform.url.DocumentViewImpl;
040import org.nuxeo.ecm.platform.url.api.DocumentView;
041import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
042import org.nuxeo.ecm.user.center.profile.UserProfileService;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * Helper class to compute links for {@link ActivityMessage} content from an activity attributes.
047 *
048 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
049 * @since 5.6
050 */
051public class ActivityMessageHelper {
052
053    private ActivityMessageHelper() {
054        // helper class
055    }
056
057    public static String getDocumentLink(String documentActivityObject, String displayValue) {
058        documentActivityObject = StringEscapeUtils.escapeHtml(documentActivityObject);
059        displayValue = StringEscapeUtils.escapeHtml(displayValue);
060        String link = "<a href=\"%s\" target=\"_top\">%s</a>";
061        return String.format(
062                link,
063                getDocumentURL(ActivityHelper.getRepositoryName(documentActivityObject),
064                        ActivityHelper.getDocumentId(documentActivityObject)), displayValue);
065    }
066
067    public static String getDocumentURL(String repositoryName, String documentId) {
068        DocumentLocation docLoc = new DocumentLocationImpl(repositoryName, new IdRef(documentId));
069        DocumentView docView = new DocumentViewImpl(docLoc, "view_documents");
070        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
071        return urlPolicyService.getUrlFromDocumentView("id", docView, VirtualHostHelper.getContextPathProperty());
072    }
073
074    public static String getUserProfileLink(String userActivityObject, String displayValue) {
075        userActivityObject = StringEscapeUtils.escapeHtml(userActivityObject);
076        displayValue = StringEscapeUtils.escapeHtml(displayValue);
077        String link = "<span class=\"username\"><a href=\"%s\" target=\"_top\" title=\"%s\">%s</a></span>";
078        String username = ActivityHelper.getUsername(userActivityObject);
079        return String.format(link, getUserProfileURL(username), username, displayValue);
080    }
081
082    public static String getUserProfileURL(String username) {
083        Map<String, String> params = new HashMap<String, String>();
084        params.put("username", username);
085        DocumentView docView = new DocumentViewImpl(null, null, params);
086        URLPolicyService urlPolicyService = Framework.getLocalService(URLPolicyService.class);
087        return VirtualHostHelper.getContextPathProperty() + "/"
088                + urlPolicyService.getUrlFromDocumentView("user", docView, null);
089    }
090
091    public static String getUserAvatarURL(CoreSession session, String username) {
092        UserProfileService userProfileService = Framework.getLocalService(UserProfileService.class);
093        DocumentModel profile = userProfileService.getUserProfileDocument(username, session);
094        Blob avatar = (Blob) profile.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
095        if (avatar != null) {
096            DownloadService downloadService = Framework.getService(DownloadService.class);
097            String filename = username + "." + FilenameUtils.getExtension(avatar.getFilename());
098            return VirtualHostHelper.getContextPathProperty() + "/"
099                    + downloadService.getDownloadUrl(profile, USER_PROFILE_AVATAR_FIELD, filename);
100        } else {
101            return VirtualHostHelper.getContextPathProperty() + "/icons/missing_avatar.png";
102        }
103    }
104
105    public static Pattern HTTP_URL_PATTERN = Pattern.compile("\\b(https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
106
107    public static String replaceURLsByLinks(String message) {
108        String escapedMessage = StringEscapeUtils.escapeHtml(message);
109        Matcher m = HTTP_URL_PATTERN.matcher(escapedMessage);
110        StringBuffer sb = new StringBuffer(escapedMessage.length());
111        while (m.find()) {
112            String url = m.group(1);
113            m.appendReplacement(sb, computeLinkFor(url));
114        }
115        m.appendTail(sb);
116        return sb.toString();
117    }
118
119    private static String computeLinkFor(String url) {
120        return "<a href=\"" + url + "\" target=\"_top\">" + url + "</a>";
121    }
122
123}