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.data.CharacterSet;
037import org.restlet.data.Form;
038import org.restlet.data.MediaType;
039import org.restlet.data.Request;
040import org.restlet.data.Response;
041import org.restlet.resource.Representation;
042import org.restlet.resource.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
061        DOMDocumentFactory domFactory = new DOMDocumentFactory();
062        DOMDocument result = (DOMDocument) domFactory.createDocument();
063
064        try {
065            DirectoryService service = Framework.getLocalService(DirectoryService.class);
066            List<Directory> directories = new LinkedList<Directory>();
067            Form form = req.getResourceRef().getQueryAsForm();
068
069            if (form.getNames().contains(DIRECTORY_NAME_QUERY_PARAM)) {
070                // only invalidate the caches of the requested directories
071                String[] directoryNames = form.getValues(DIRECTORY_NAME_QUERY_PARAM).split(",");
072                for (String directoryName : directoryNames) {
073                    Directory directory = service.getDirectory(directoryName);
074                    if (directory == null) {
075                        log.error("no such directory: " + directoryName);
076                    } else {
077                        directories.add(directory);
078                    }
079                }
080            } else {
081                // invalidate all directory caches
082                directories = service.getDirectories();
083            }
084
085            Element invalidatedCachesElement = result.addElement("invalidatedCaches");
086            for (Directory directory : directories) {
087                directory.getCache().invalidateAll();
088                invalidatedCachesElement.addElement("directory").addText(directory.getName());
089            }
090
091        } catch (DirectoryException e) {
092            handleError(res, e);
093            return;
094        }
095        Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML);
096        rep.setCharacterSet(CharacterSet.UTF_8);
097        res.setEntity(rep);
098    }
099
100}