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.server.jaxrs.directory; 020 021import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession; 022 023import java.util.List; 024 025import javax.ws.rs.DELETE; 026import javax.ws.rs.GET; 027import javax.ws.rs.PUT; 028import javax.ws.rs.core.Response; 029import javax.ws.rs.core.Response.Status; 030 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.directory.Directory; 033import org.nuxeo.ecm.directory.DirectoryException; 034import org.nuxeo.ecm.directory.Session; 035import org.nuxeo.ecm.directory.api.DirectoryDeleteConstraint; 036import org.nuxeo.ecm.directory.api.DirectoryEntry; 037import org.nuxeo.ecm.directory.api.DirectoryService; 038import org.nuxeo.ecm.webengine.model.WebObject; 039import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 040import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException; 041import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * @since 5.7.3 046 */ 047@WebObject(type = "directoryEntry") 048public class DirectoryEntryObject extends DefaultObject { 049 050 protected DirectoryEntry entry; 051 052 protected Directory directory; 053 054 protected String entryId; 055 056 @Override 057 protected void initialize(Object... args) { 058 if (args.length < 1) { 059 throw new IllegalArgumentException("Directory Entry obhect takes one parameter"); 060 } 061 062 entry = (DirectoryEntry) args[0]; 063 directory = getDirectoryFromEntry(entry); 064 entryId = (String) args[1];; 065 } 066 067 @GET 068 public DirectoryEntry doGet() { 069 return entry; 070 } 071 072 @PUT 073 public DirectoryEntry doUpdateEntry(final DirectoryEntry entry) { 074 checkEditGuards(); 075 return withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() { 076 077 @Override 078 DirectoryEntry run(Session session) { 079 DocumentModel docEntry = entry.getDocumentModel(); 080 session.updateEntry(docEntry); 081 082 String id = (String) docEntry.getPropertyValue(directory.getSchema() + ":" + directory.getIdField()); 083 084 return new DirectoryEntry(directory.getName(), session.getEntry(id)); 085 086 } 087 }); 088 089 } 090 091 private void checkEditGuards() { 092 ((DirectoryObject) prev).checkEditGuards(); 093 } 094 095 /** 096 * @since 8.4 097 */ 098 private void checkDeleteGuards() { 099 List<DirectoryDeleteConstraint> deleteConstraints = directory.getDirectoryDeleteConstraints(); 100 DirectoryService directoryService = Framework.getLocalService(DirectoryService.class); 101 if (deleteConstraints != null && !deleteConstraints.isEmpty()) { 102 for (DirectoryDeleteConstraint deleteConstraint : deleteConstraints) { 103 if (!deleteConstraint.canDelete(directoryService, entryId)) { 104 throw new WebSecurityException("This entry is referenced in another vocabulary."); 105 } 106 } 107 } 108 } 109 110 @DELETE 111 public Response doDeleteEntry() { 112 checkEditGuards(); 113 checkDeleteGuards(); 114 withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() { 115 116 @Override 117 DirectoryEntry run(Session session) { 118 session.deleteEntry(entry.getDocumentModel()); 119 return null; 120 } 121 }); 122 123 return Response.ok().status(Status.NO_CONTENT).build(); 124 125 } 126 127 private Directory getDirectoryFromEntry(final DirectoryEntry entry) { 128 DirectoryService ds = Framework.getLocalService(DirectoryService.class); 129 Directory directory; 130 try { 131 directory = ds.getDirectory(entry.getDirectoryName()); 132 } catch (DirectoryException e) { 133 throw new WebResourceNotFoundException("directory not found"); 134 } 135 return directory; 136 } 137 138}