001/* 002 * (C) Copyright 2015 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 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.platform.usermanager.io; 021 022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 024 025import java.io.IOException; 026 027import org.codehaus.jackson.JsonGenerator; 028import org.nuxeo.ecm.core.api.NuxeoGroup; 029import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter; 030import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 031import org.nuxeo.ecm.core.io.registry.reflect.Setup; 032 033import com.thoughtworks.xstream.io.json.JsonWriter; 034 035/** 036 * Convert {@link NuxeoGroup} to Json. 037 * <p> 038 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing 039 * {@link NuxeoGroup}. 040 * </p> 041 * <p> 042 * This marshaller is also extensible: extend it and simply override 043 * {@link ExtensibleEntityJsonWriter#extend(NuxeoGroup, JsonWriter)}. 044 * </p> 045 * <p> 046 * Format is: 047 * 048 * <pre> 049 * {@code 050 * { 051 * "entity-type":"group", 052 * "groupname": "GROUP_NAME", 053 * "grouplabel": "GROUP_DISPLAY_NAME", 054 * "memberUsers": [ 055 * "USERNAME1", 056 * "USERNAME2", 057 * ... 058 * ], 059 * "memberGroups": [ 060 * "GROUPNAME1", 061 * "GROUPNAME2", 062 * ... 063 * ] 064 * <-- contextParameters if there are enrichers activated 065 * <-- additional property provided by extend() method 066 * } 067 * </pre> 068 * </p> 069 * 070 * @since 7.2 071 */ 072@Setup(mode = SINGLETON, priority = REFERENCE) 073public class NuxeoGroupJsonWriter extends ExtensibleEntityJsonWriter<NuxeoGroup> { 074 075 public static final String ENTITY_TYPE = "group"; 076 077 public NuxeoGroupJsonWriter() { 078 super(ENTITY_TYPE, NuxeoGroup.class); 079 } 080 081 @Override 082 protected void writeEntityBody(NuxeoGroup group, JsonGenerator jg) throws IOException { 083 jg.writeStringField("groupname", group.getName()); 084 jg.writeStringField("grouplabel", group.getLabel()); 085 if (ctx.getFetched(ENTITY_TYPE).contains("memberUsers")) { 086 jg.writeArrayFieldStart("memberUsers"); 087 for (String user : group.getMemberUsers()) { 088 jg.writeString(user); 089 } 090 jg.writeEndArray(); 091 } 092 if (ctx.getFetched(ENTITY_TYPE).contains("memberGroups")) { 093 jg.writeArrayFieldStart("memberGroups"); 094 for (String user : group.getMemberGroups()) { 095 jg.writeString(user); 096 } 097 jg.writeEndArray(); 098 } 099 } 100 101}