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