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