001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (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.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 * Nuxeo - initial API and implementation 016 * 017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.restAPI; 021 022import java.util.LinkedList; 023import java.util.List; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.dom4j.Element; 028import org.dom4j.dom.DOMDocument; 029import org.dom4j.dom.DOMDocumentFactory; 030import org.nuxeo.ecm.directory.Directory; 031import org.nuxeo.ecm.directory.DirectoryException; 032import org.nuxeo.ecm.directory.api.DirectoryService; 033import org.nuxeo.runtime.api.Framework; 034import org.restlet.data.CharacterSet; 035import org.restlet.data.Form; 036import org.restlet.data.MediaType; 037import org.restlet.data.Request; 038import org.restlet.data.Response; 039import org.restlet.resource.Representation; 040import org.restlet.resource.StringRepresentation; 041 042/** 043 * Restlet to invalidate the cache of directories from an external application. 044 * <p> 045 * Warning: this restlet can only be used on the JVM that hosts the DirectoryService instance since it is using 046 * non-remotable API. This means this restlet will not work with the multi machine setups of Nuxeo. 047 * 048 * @author ogrisel 049 */ 050public class DirectoryCacheRestlet extends BaseNuxeoRestlet { 051 052 private static final Log log = LogFactory.getLog(DirectoryCacheRestlet.class); 053 054 public static final String DIRECTORY_NAME_QUERY_PARAM = "directory"; 055 056 @Override 057 public void handle(Request req, Response res) { 058 059 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 060 DOMDocument result = (DOMDocument) domFactory.createDocument(); 061 062 try { 063 DirectoryService service = Framework.getLocalService(DirectoryService.class); 064 List<Directory> directories = new LinkedList<Directory>(); 065 Form form = req.getResourceRef().getQueryAsForm(); 066 067 if (form.getNames().contains(DIRECTORY_NAME_QUERY_PARAM)) { 068 // only invalidate the caches of the requested directories 069 String[] directoryNames = form.getValues(DIRECTORY_NAME_QUERY_PARAM).split(","); 070 for (String directoryName : directoryNames) { 071 Directory directory = service.getDirectory(directoryName); 072 if (directory == null) { 073 log.error("no such directory: " + directoryName); 074 } else { 075 directories.add(directory); 076 } 077 } 078 } else { 079 // invalidate all directory caches 080 directories = service.getDirectories(); 081 } 082 083 Element invalidatedCachesElement = result.addElement("invalidatedCaches"); 084 for (Directory directory : directories) { 085 directory.getCache().invalidateAll(); 086 invalidatedCachesElement.addElement("directory").addText(directory.getName()); 087 } 088 089 } catch (DirectoryException e) { 090 handleError(res, e); 091 return; 092 } 093 Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML); 094 rep.setCharacterSet(CharacterSet.UTF_8); 095 res.setEntity(rep); 096 } 097 098}