001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.platform.usermanager.io; 019 020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 022 023import java.io.IOException; 024 025import org.codehaus.jackson.JsonGenerator; 026import org.nuxeo.ecm.core.api.NuxeoGroup; 027import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter; 028import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 029import org.nuxeo.ecm.core.io.registry.reflect.Setup; 030 031import com.thoughtworks.xstream.io.json.JsonWriter; 032 033/** 034 * Convert {@link NuxeoGroup} to Json. 035 * <p> 036 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing 037 * {@link NuxeoGroup}. 038 * </p> 039 * <p> 040 * This marshaller is also extensible: extend it and simply override 041 * {@link ExtensibleEntityJsonWriter#extend(NuxeoGroup, JsonWriter)}. 042 * </p> 043 * <p> 044 * Format is: 045 * 046 * <pre> 047 * {@code 048 * { 049 * "entity-type":"group", 050 * "groupname": "GROUP_NAME", 051 * "grouplabel": "GROUP_DISPLAY_NAME", 052 * "memberUsers": [ 053 * "USERNAME1", 054 * "USERNAME2", 055 * ... 056 * ], 057 * "memberGroups": [ 058 * "GROUPNAME1", 059 * "GROUPNAME2", 060 * ... 061 * ] 062 * <-- contextParameters if there are enrichers activated 063 * <-- additional property provided by extend() method 064 * } 065 * </pre> 066 * 067 * </p> 068 * 069 * @since 7.2 070 */ 071@Setup(mode = SINGLETON, priority = REFERENCE) 072public class NuxeoGroupJsonWriter extends ExtensibleEntityJsonWriter<NuxeoGroup> { 073 074 public static final String ENTITY_TYPE = "group"; 075 076 public NuxeoGroupJsonWriter() { 077 super(ENTITY_TYPE, NuxeoGroup.class); 078 } 079 080 @Override 081 protected void writeEntityBody(NuxeoGroup group, JsonGenerator jg) throws IOException { 082 jg.writeStringField("groupname", group.getName()); 083 jg.writeStringField("grouplabel", group.getLabel()); 084 jg.writeArrayFieldStart("memberUsers"); 085 for (String user : group.getMemberUsers()) { 086 jg.writeString(user); 087 } 088 jg.writeEndArray(); 089 jg.writeArrayFieldStart("memberGroups"); 090 for (String user : group.getMemberGroups()) { 091 jg.writeString(user); 092 } 093 jg.writeEndArray(); 094 } 095 096}