001/*
002 * (C) Copyright 2016 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 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.user.center.profile.rest;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
025import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_BIRTHDATE_FIELD;
026import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_PHONENUMBER_FIELD;
027
028import java.io.IOException;
029
030import org.codehaus.jackson.JsonGenerator;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.ecm.core.io.download.DownloadService;
035import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
036import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
037import org.nuxeo.ecm.core.io.registry.reflect.Setup;
038import org.nuxeo.ecm.user.center.profile.UserProfileService;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
043 * @since 8.1
044 */
045@Setup(mode = SINGLETON, priority = REFERENCE)
046public class UserProfileEnricher extends AbstractJsonEnricher<NuxeoPrincipal> {
047
048    public static final String NAME = "userprofile";
049
050    public UserProfileEnricher() {
051        super(NAME);
052    }
053
054    @Override
055    public void write(JsonGenerator jg, NuxeoPrincipal nuxeoPrincipal) throws IOException {
056
057        UserProfileService ups = Framework.getLocalService(UserProfileService.class);
058        try (RenderingContext.SessionWrapper sw = ctx.getSession(null)) {
059            DocumentModel up = ups.getUserProfileDocument(nuxeoPrincipal.getName(), sw.getSession());
060
061            jg.writeFieldName(NAME);
062            if (up == null) {
063                jg.writeNull();
064            } else {
065                jg.writeStartObject();
066
067                jg.writeStringField("birthdate", (String) up.getPropertyValue(USER_PROFILE_BIRTHDATE_FIELD));
068                jg.writeStringField("phonenumber", (String) up.getPropertyValue(USER_PROFILE_PHONENUMBER_FIELD));
069
070                Blob avatar = (Blob) up.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
071                if (avatar != null) {
072                    DownloadService downloadService = Framework.getService(DownloadService.class);
073                    String url = downloadService.getDownloadUrl(up, USER_PROFILE_AVATAR_FIELD, avatar.getFilename());
074                    jg.writeStringField("avatar", ctx.getBaseUrl() + url);
075                } else {
076                    jg.writeNullField("avatar");
077                }
078
079                jg.writeEndObject();
080            }
081        }
082    }
083}