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