001/*
002 * (C) Copyright 2006-2018 Nuxeo (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.text.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.escapeHtml4(documentActivityObject);
059        displayValue = StringEscapeUtils.escapeHtml4(displayValue);
060        String link = "<a href=\"%s\" target=\"_top\">%s</a>";
061        return String.format(link, 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.getService(URLPolicyService.class);
069        return urlPolicyService.getUrlFromDocumentView("id", docView, VirtualHostHelper.getContextPathProperty());
070    }
071
072    public static String getUserProfileLink(String userActivityObject, String displayValue) {
073        userActivityObject = StringEscapeUtils.escapeHtml4(userActivityObject);
074        displayValue = StringEscapeUtils.escapeHtml4(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.getService(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.getService(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 final Pattern HTTP_URL_PATTERN = Pattern.compile(
104            "\\b(https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
105
106    public static String replaceURLsByLinks(String message) {
107        String escapedMessage = StringEscapeUtils.escapeHtml4(message);
108        Matcher m = HTTP_URL_PATTERN.matcher(escapedMessage);
109        StringBuffer sb = new StringBuffer(escapedMessage.length());
110        while (m.find()) {
111            String url = m.group(1);
112            m.appendReplacement(sb, computeLinkFor(url));
113        }
114        m.appendTail(sb);
115        return sb.toString();
116    }
117
118    private static String computeLinkFor(String url) {
119        return "<a href=\"" + url + "\" target=\"_top\">" + url + "</a>";
120    }
121
122}