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; 029import java.io.Serializable; 030import java.text.DateFormat; 031import java.text.SimpleDateFormat; 032import java.util.GregorianCalendar; 033 034import org.codehaus.jackson.JsonGenerator; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.NuxeoPrincipal; 038import org.nuxeo.ecm.core.io.download.DownloadService; 039import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 040import org.nuxeo.ecm.core.io.registry.context.RenderingContext; 041import org.nuxeo.ecm.core.io.registry.reflect.Setup; 042import org.nuxeo.ecm.user.center.profile.UserProfileService; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 047 * @since 8.1 048 */ 049@Setup(mode = SINGLETON, priority = REFERENCE) 050public class UserProfileEnricher extends AbstractJsonEnricher<NuxeoPrincipal> { 051 052 private static final DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 053 054 public static final String NAME = "userprofile"; 055 056 public UserProfileEnricher() { 057 super(NAME); 058 } 059 060 @Override 061 public void write(JsonGenerator jg, NuxeoPrincipal nuxeoPrincipal) throws IOException { 062 063 UserProfileService ups = Framework.getLocalService(UserProfileService.class); 064 try (RenderingContext.SessionWrapper sw = ctx.getSession(null)) { 065 DocumentModel up = ups.getUserProfileDocument(nuxeoPrincipal.getName(), sw.getSession()); 066 067 jg.writeFieldName(NAME); 068 if (up == null) { 069 jg.writeNull(); 070 } else { 071 try { 072 jg.writeStartObject(); 073 074 Serializable propertyValue = up.getPropertyValue(USER_PROFILE_BIRTHDATE_FIELD); 075 jg.writeStringField( 076 "birthdate", 077 propertyValue == null ? null 078 : FORMATTER.format(((GregorianCalendar) propertyValue).getTime())); 079 jg.writeStringField("phonenumber", (String) up.getPropertyValue(USER_PROFILE_PHONENUMBER_FIELD)); 080 081 Blob avatar = (Blob) up.getPropertyValue(USER_PROFILE_AVATAR_FIELD); 082 if (avatar != null) { 083 DownloadService downloadService = Framework.getService(DownloadService.class); 084 String url = downloadService.getDownloadUrl(up, USER_PROFILE_AVATAR_FIELD, avatar.getFilename()); 085 jg.writeStringField("avatar", ctx.getBaseUrl() + url); 086 } else { 087 jg.writeNullField("avatar"); 088 } 089 } finally { 090 jg.writeEndObject(); 091 } 092 } 093 } 094 } 095}