001/*
002 * (C) Copyright 2008 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: CreationContainerListRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI;
023
024import java.io.Serializable;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.dom4j.Element;
029import org.dom4j.dom.DOMDocument;
030import org.dom4j.dom.DOMDocumentFactory;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.core.api.local.ClientLoginModule;
034import org.nuxeo.ecm.platform.filemanager.api.FileManager;
035import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
036import org.nuxeo.runtime.api.Framework;
037import org.restlet.Request;
038import org.restlet.Response;
039import org.restlet.data.CharacterSet;
040import org.restlet.data.MediaType;
041
042/**
043 * This restlet gets the list of containers that are suitable for new document creation.
044 * <p>
045 * The actual logic is delegated to the FileManagerService.
046 *
047 * @author Olivier Grisel <ogrisel@nuxeo.com>
048 */
049public class CreationContainerListRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable {
050
051    private static final Log log = LogFactory.getLog(CreationContainerListRestlet.class);
052
053    private static final long serialVersionUID = 5403775170948512675L;
054
055    @Override
056    public void handle(Request req, Response res) {
057        logDeprecation();
058
059        DocumentModelList containers = null;
060        String docType = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE);
061        FileManager fileManager = Framework.getService(FileManager.class);
062        containers = fileManager.getCreationContainers(ClientLoginModule.getCurrentPrincipal(), docType);
063
064        // build the XML response document holding the containers info
065        DOMDocumentFactory domFactory = new DOMDocumentFactory();
066        DOMDocument resultDocument = (DOMDocument) domFactory.createDocument();
067        Element containersElement = resultDocument.addElement("containers");
068        for (DocumentModel parent : containers) {
069            Element docElement = containersElement.addElement(documentTag);
070            docElement.addElement(docRepositoryTag).setText(parent.getRepositoryName());
071            docElement.addElement(docRefTag).setText(parent.getRef().toString());
072            docElement.addElement(docTitleTag).setText(parent.getTitle());
073            docElement.addElement(docPathTag).setText(parent.getPathAsString());
074        }
075        res.setEntity(resultDocument.asXML(), MediaType.APPLICATION_XML);
076        res.getEntity().setCharacterSet(CharacterSet.UTF_8);
077    }
078
079}