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.directory;
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.automation.jaxrs.io.documents.JsonDocumentWriter;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.schema.SchemaManager;
032import org.nuxeo.ecm.core.schema.types.Field;
033import org.nuxeo.ecm.core.schema.types.QName;
034import org.nuxeo.ecm.core.schema.types.Schema;
035import org.nuxeo.ecm.directory.Directory;
036import org.nuxeo.ecm.directory.api.DirectoryEntry;
037import org.nuxeo.ecm.directory.api.DirectoryService;
038import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @since 5.7.3
043 * @deprecated since 7.10 The Nuxeo JSON marshalling was migrated to nuxeo-core-io. This class is replaced by
044 *             org.nuxeo.ecm.directory.io.DirectoryEntrysonWriter which is registered by default and available to
045 *             marshal {@link DirectoryEntry} from the Nuxeo Rest API thanks to the JAX-RS marshaller
046 *             {@link JsonCoreIODelegate}.
047 */
048@Deprecated
049@Provider
050@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" })
051public class DirectoryEntryWriter extends EntityWriter<DirectoryEntry> {
052
053    /**
054     *
055     */
056    public static final String ENTITY_TYPE = "directoryEntry";
057
058    @Override
059    protected void writeEntityBody(JsonGenerator jg, DirectoryEntry directoryEntry) throws IOException {
060        String directoryName = directoryEntry.getDirectoryName();
061
062        jg.writeStringField("directoryName", directoryName);
063        jg.writeObjectFieldStart("properties");
064        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
065        Directory directory = ds.getDirectory(directoryName);
066
067        SchemaManager sm = Framework.getLocalService(SchemaManager.class);
068        Schema schema = sm.getSchema(directory.getSchema());
069
070        DocumentModel entry = directoryEntry.getDocumentModel();
071
072        for (Field field : schema.getFields()) {
073            QName fieldName = field.getName();
074            String key = fieldName.getLocalName();
075
076            jg.writeFieldName(key);
077            if (key.equals(directory.getPasswordField())) {
078                jg.writeString("");
079            } else {
080                JsonDocumentWriter.writePropertyValue(jg, entry.getProperty(fieldName.getPrefixedName()), "");
081            }
082
083        }
084        jg.writeEndObject();
085
086    }
087
088    @Override
089    protected String getEntityType() {
090        return ENTITY_TYPE;
091    }
092
093}