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.directory; 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.automation.jaxrs.io.documents.JsonDocumentWriter; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.schema.SchemaManager; 030import org.nuxeo.ecm.core.schema.types.Field; 031import org.nuxeo.ecm.core.schema.types.QName; 032import org.nuxeo.ecm.core.schema.types.Schema; 033import org.nuxeo.ecm.directory.Directory; 034import org.nuxeo.ecm.directory.api.DirectoryEntry; 035import org.nuxeo.ecm.directory.api.DirectoryService; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * @since 5.7.3 040 */ 041@Provider 042@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" }) 043public class DirectoryEntryWriter extends EntityWriter<DirectoryEntry> { 044 045 /** 046 * 047 */ 048 public static final String ENTITY_TYPE = "directoryEntry"; 049 050 @Override 051 protected void writeEntityBody(JsonGenerator jg, DirectoryEntry directoryEntry) throws IOException { 052 String directoryName = directoryEntry.getDirectoryName(); 053 054 jg.writeStringField("directoryName", directoryName); 055 jg.writeObjectFieldStart("properties"); 056 DirectoryService ds = Framework.getLocalService(DirectoryService.class); 057 Directory directory = ds.getDirectory(directoryName); 058 059 SchemaManager sm = Framework.getLocalService(SchemaManager.class); 060 Schema schema = sm.getSchema(directory.getSchema()); 061 062 DocumentModel entry = directoryEntry.getDocumentModel(); 063 064 for (Field field : schema.getFields()) { 065 QName fieldName = field.getName(); 066 String key = fieldName.getLocalName(); 067 068 jg.writeFieldName(key); 069 if (key.equals(directory.getPasswordField())) { 070 jg.writeString(""); 071 } else { 072 JsonDocumentWriter.writePropertyValue(jg, entry.getProperty(fieldName.getPrefixedName()), ""); 073 } 074 075 } 076 jg.writeEndObject(); 077 078 } 079 080 @Override 081 protected String getEntityType() { 082 return ENTITY_TYPE; 083 } 084 085}