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 javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; 022import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession; 023 024import java.util.List; 025 026import javax.ws.rs.DELETE; 027import javax.ws.rs.GET; 028import javax.ws.rs.PUT; 029import javax.ws.rs.core.Response; 030import javax.ws.rs.core.Response.Status; 031 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.directory.Directory; 035import org.nuxeo.ecm.directory.DirectoryException; 036import org.nuxeo.ecm.directory.Session; 037import org.nuxeo.ecm.directory.api.DirectoryDeleteConstraint; 038import org.nuxeo.ecm.directory.api.DirectoryEntry; 039import org.nuxeo.ecm.directory.api.DirectoryService; 040import org.nuxeo.ecm.webengine.model.WebObject; 041import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 042import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * @since 5.7.3 047 */ 048@WebObject(type = "directoryEntry") 049public class DirectoryEntryObject extends DefaultObject { 050 051 protected DirectoryEntry entry; 052 053 protected Directory directory; 054 055 protected String entryId; 056 057 @Override 058 protected void initialize(Object... args) { 059 if (args.length < 1) { 060 throw new IllegalArgumentException("Directory Entry object takes one parameter"); 061 } 062 063 entry = (DirectoryEntry) args[0]; 064 directory = getDirectoryFromEntry(entry); 065 entryId = (String) args[1]; 066 } 067 068 @GET 069 public DirectoryEntry doGet() { 070 return entry; 071 } 072 073 @PUT 074 public DirectoryEntry doUpdateEntry(final DirectoryEntry entry) { 075 checkEditGuards(); 076 return withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() { 077 078 @Override 079 DirectoryEntry run(Session session) { 080 try { 081 DocumentModel docEntry = entry.getDocumentModel(); 082 session.updateEntry(docEntry); 083 return new DirectoryEntry(directory.getName(), session.getEntry(docEntry.getId())); 084 } catch (DirectoryException e) { 085 throw new NuxeoException(e.getMessage(), SC_BAD_REQUEST); 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.getService(DirectoryService.class); 101 if (deleteConstraints != null && !deleteConstraints.isEmpty()) { 102 for (DirectoryDeleteConstraint deleteConstraint : deleteConstraints) { 103 if (!deleteConstraint.canDelete(directoryService, entryId)) { 104 throw new NuxeoException("This entry is referenced in another vocabulary.", SC_BAD_REQUEST); 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.getService(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}