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