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