001/*
002 * (C) Copyright 2013 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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.jaxrs.io.documents;
018
019import java.io.IOException;
020
021import javax.ws.rs.Produces;
022import javax.ws.rs.core.MediaType;
023import javax.ws.rs.ext.Provider;
024
025import org.codehaus.jackson.JsonGenerator;
026import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
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.schema.utils.DateParser;
031
032/**
033 * Json writer for ACP.
034 *
035 * @since 5.7.3
036 */
037@Provider
038@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" })
039public class ACPWriter extends EntityWriter<ACP> {
040
041    public static final String ENTITY_TYPE = "acls";
042
043    @Override
044    protected void writeEntityBody(JsonGenerator jg, ACP item) throws IOException {
045
046        jg.writeArrayFieldStart("acl");
047        for (ACL acl : item.getACLs()) {
048            jg.writeStartObject();
049            jg.writeStringField("name", acl.getName());
050
051            jg.writeArrayFieldStart("ace");
052
053            for (ACE ace : acl.getACEs()) {
054                jg.writeStartObject();
055                jg.writeStringField("id", ace.getId());
056                jg.writeStringField("username", ace.getUsername());
057                jg.writeStringField("permission", ace.getPermission());
058                jg.writeBooleanField("granted", ace.isGranted());
059                jg.writeStringField("creator", ace.getCreator());
060                jg.writeStringField("begin",
061                        ace.getBegin() != null ? DateParser.formatW3CDateTime(ace.getBegin().getTime()) : null);
062                jg.writeStringField("end",
063                        ace.getEnd() != null ? DateParser.formatW3CDateTime(ace.getEnd().getTime()) : null);
064                jg.writeStringField("status", ace.getStatus().toString().toLowerCase());
065                jg.writeEndObject();
066            }
067
068            jg.writeEndArray();
069            jg.writeEndObject();
070        }
071        jg.writeEndArray();
072    }
073
074    @Override
075    protected String getEntityType() {
076        return ENTITY_TYPE;
077    }
078}