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.core.io.marshallers.json.document; 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.security.ACE; 029import org.nuxeo.ecm.core.api.security.ACL; 030import org.nuxeo.ecm.core.api.security.ACP; 031import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter; 032import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 033import org.nuxeo.ecm.core.io.registry.reflect.Setup; 034import org.nuxeo.ecm.core.schema.utils.DateParser; 035 036/** 037 * Convert {@link ACP} to Json. 038 * <p> 039 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing {@link ACP}. 040 * </p> 041 * <p> 042 * This marshaller is also extensible: extend it and simply override 043 * {@link ExtensibleEntityJsonWriter#extend(ACP, JsonGenerator)}. 044 * </p> 045 * <p> 046 * Format is: 047 * 048 * <pre> 049 * {@code 050 * { 051 * "entity-type":"acls", 052 * "acl": [ 053 * { 054 * "name":"inherited", 055 * "ace":[ 056 * { 057 * "username":"administrators", 058 * "permission":"Everything", 059 * "granted":true 060 * }, 061 * ... 062 * ] 063 * }, 064 * ... 065 * ] 066 * <-- contextParameters if there are enrichers activated 067 * <-- additional property provided by extend() method 068 * } 069 * </pre> 070 * 071 * </p> 072 * 073 * @since 7.2 074 */ 075@Setup(mode = SINGLETON, priority = REFERENCE) 076public class ACPJsonWriter extends ExtensibleEntityJsonWriter<ACP> { 077 078 public static final String ENTITY_TYPE = "acls"; 079 080 public ACPJsonWriter() { 081 super(ENTITY_TYPE, ACP.class); 082 } 083 084 @Override 085 protected void writeEntityBody(ACP acp, JsonGenerator jg) throws IOException { 086 jg.writeArrayFieldStart("acl"); 087 for (ACL acl : acp.getACLs()) { 088 jg.writeStartObject(); 089 jg.writeStringField("name", acl.getName()); 090 jg.writeArrayFieldStart("ace"); 091 for (ACE ace : acl.getACEs()) { 092 jg.writeStartObject(); 093 jg.writeStringField("id", ace.getId()); 094 jg.writeStringField("username", ace.getUsername()); 095 jg.writeStringField("permission", ace.getPermission()); 096 jg.writeBooleanField("granted", ace.isGranted()); 097 jg.writeStringField("creator", ace.getCreator()); 098 jg.writeStringField("begin", 099 ace.getBegin() != null ? DateParser.formatW3CDateTime(ace.getBegin().getTime()) : null); 100 jg.writeStringField("end", ace.getEnd() != null ? DateParser.formatW3CDateTime(ace.getEnd().getTime()) 101 : null); 102 jg.writeStringField("status", ace.getStatus().toString().toLowerCase()); 103 jg.writeEndObject(); 104 } 105 106 jg.writeEndArray(); 107 jg.writeEndObject(); 108 } 109 jg.writeEndArray(); 110 } 111 112}