001/*
002 * (C) Copyright 2013 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 *     dmetzler
018 */
019package org.nuxeo.ecm.restapi.jaxrs.io.documents;
020
021import java.io.IOException;
022
023import javax.ws.rs.Produces;
024import javax.ws.rs.core.MediaType;
025import javax.ws.rs.ext.Provider;
026
027import org.codehaus.jackson.JsonGenerator;
028import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
029import org.nuxeo.ecm.core.api.security.ACE;
030import org.nuxeo.ecm.core.api.security.ACL;
031import org.nuxeo.ecm.core.api.security.ACP;
032import org.nuxeo.ecm.core.io.marshallers.json.document.ACPJsonWriter;
033import org.nuxeo.ecm.core.schema.utils.DateParser;
034import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
035
036/**
037 * Json writer for ACP.
038 *
039 * @since 5.7.3
040 * @deprecated since 7.10 The Nuxeo JSON marshalling was migrated to nuxeo-core-io. This class is replaced by
041 *             {@link ACPJsonWriter} which is registered by default and available to marshal {@link ACP} from the Nuxeo
042 *             Rest API thanks to the JAX-RS marshaller {@link JsonCoreIODelegate}.
043 */
044@Deprecated
045@Provider
046@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" })
047public class ACPWriter extends EntityWriter<ACP> {
048
049    public static final String ENTITY_TYPE = "acls";
050
051    @Override
052    protected void writeEntityBody(JsonGenerator jg, ACP item) throws IOException {
053
054        jg.writeArrayFieldStart("acl");
055        for (ACL acl : item.getACLs()) {
056            jg.writeStartObject();
057            jg.writeStringField("name", acl.getName());
058
059            jg.writeArrayFieldStart("ace");
060
061            for (ACE ace : acl.getACEs()) {
062                jg.writeStartObject();
063                jg.writeStringField("id", ace.getId());
064                jg.writeStringField("username", ace.getUsername());
065                jg.writeStringField("permission", ace.getPermission());
066                jg.writeBooleanField("granted", ace.isGranted());
067                jg.writeStringField("creator", ace.getCreator());
068                jg.writeStringField("begin",
069                        ace.getBegin() != null ? DateParser.formatW3CDateTime(ace.getBegin().getTime()) : null);
070                jg.writeStringField("end", ace.getEnd() != null ? DateParser.formatW3CDateTime(ace.getEnd().getTime())
071                        : null);
072                jg.writeStringField("status", ace.getStatus().toString().toLowerCase());
073                jg.writeEndObject();
074            }
075
076            jg.writeEndArray();
077            jg.writeEndObject();
078        }
079        jg.writeEndArray();
080    }
081
082    @Override
083    protected String getEntityType() {
084        return ENTITY_TYPE;
085    }
086}