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;
026
027import org.apache.commons.io.FilenameUtils;
028import org.apache.commons.text.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 * Default {@link ActivityLinkBuilder} computing URLs with the default id codec for documents and user codec for users.
045 *
046 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
047 * @since 5.6
048 */
049public class DefaultActivityLinkBuilder implements ActivityLinkBuilder {
050
051    @Override
052    public String getDocumentLink(String documentActivityObject, String displayValue) {
053        documentActivityObject = StringEscapeUtils.escapeHtml4(documentActivityObject);
054        displayValue = StringEscapeUtils.escapeHtml4(displayValue);
055        String link = "<a href=\"%s\" target=\"_top\">%s</a>";
056        return String.format(link, getDocumentURL(ActivityHelper.getRepositoryName(documentActivityObject),
057                ActivityHelper.getDocumentId(documentActivityObject)), displayValue);
058    }
059
060    protected String getDocumentURL(String repositoryName, String documentId) {
061        DocumentLocation docLoc = new DocumentLocationImpl(repositoryName, new IdRef(documentId));
062        DocumentView docView = new DocumentViewImpl(docLoc, "view_documents");
063        URLPolicyService urlPolicyService = Framework.getService(URLPolicyService.class);
064        return urlPolicyService.getUrlFromDocumentView("id", docView, VirtualHostHelper.getContextPathProperty());
065    }
066
067    @Override
068    public String getUserProfileLink(String userActivityObject, String displayValue) {
069        userActivityObject = StringEscapeUtils.escapeHtml4(userActivityObject);
070        displayValue = StringEscapeUtils.escapeHtml4(displayValue);
071        String link = "<span class=\"username\"><a href=\"%s\" target=\"_top\" title=\"%s\">%s</a></span>";
072        String username = ActivityHelper.getUsername(userActivityObject);
073        return String.format(link, getUserProfileURL(username), username, displayValue);
074    }
075
076    protected String getUserProfileURL(String username) {
077        Map<String, String> params = new HashMap<>();
078        params.put("username", username);
079        DocumentView docView = new DocumentViewImpl(null, null, params);
080        URLPolicyService urlPolicyService = Framework.getService(URLPolicyService.class);
081        if (urlPolicyService == null) {
082            return "";
083        }
084        return urlPolicyService.getUrlFromDocumentView("user", docView, VirtualHostHelper.getContextPathProperty());
085    }
086
087    @Override
088    public String getUserAvatarURL(CoreSession session, String username) {
089        UserProfileService userProfileService = Framework.getService(UserProfileService.class);
090        DocumentModel profile = userProfileService.getUserProfileDocument(username, session);
091        Blob avatar = (Blob) profile.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
092        if (avatar != null) {
093            DownloadService downloadService = Framework.getService(DownloadService.class);
094            String filename = username + "." + FilenameUtils.getExtension(avatar.getFilename());
095            return VirtualHostHelper.getContextPathProperty() + "/"
096                    + downloadService.getDownloadUrl(profile, USER_PROFILE_AVATAR_FIELD, filename);
097        } else {
098            return VirtualHostHelper.getContextPathProperty() + "/icons/missing_avatar.png";
099        }
100    }
101}