001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (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
016 */
017
018package org.nuxeo.ecm.platform.publisher.remoting.server;
019
020import org.nuxeo.ecm.core.api.DocumentLocation;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.NuxeoException;
023import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
024import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
025import org.nuxeo.ecm.platform.publisher.api.RemotePublicationTreeManager;
026import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.RemotePublisherMarshaler;
027import org.nuxeo.runtime.api.Framework;
028
029import java.util.List;
030import java.util.Map;
031
032/**
033 * {@link PublicationInvokationHandler} implementation. Could be called by a Restlet, a WebEngine module or a
034 * TestInvoker
035 *
036 * @author tiry
037 */
038public class TestInvokationHandler implements PublicationInvokationHandler {
039
040    protected RemotePublisherMarshaler marshaler;
041
042    public TestInvokationHandler(RemotePublisherMarshaler marshaler) {
043        this.marshaler = marshaler;
044    }
045
046    public void init(RemotePublisherMarshaler marshaler) {
047        this.marshaler = marshaler;
048    }
049
050    public String invoke(String methodName, String data) {
051
052        // XXX Err management !
053
054        RemotePublicationTreeManager tm = Framework.getLocalService(RemotePublicationTreeManager.class);
055
056        List<Object> params = marshaler.unMarshallParameters(data);
057
058        if ("getChildrenDocuments".equals(methodName)) {
059            return marshaler.marshallResult(tm.getChildrenDocuments((PublicationNode) params.get(0)));
060        } else if ("getChildrenNodes".equals(methodName)) {
061            return marshaler.marshallResult(tm.getChildrenNodes((PublicationNode) params.get(0)));
062        } else if ("getExistingPublishedDocument".equals(methodName)) {
063            return marshaler.marshallResult(
064                    tm.getExistingPublishedDocument((String) params.get(0), (DocumentLocation) params.get(1)));
065        } else if ("getNodeByPath".equals(methodName)) {
066            return marshaler.marshallResult(tm.getNodeByPath((String) params.get(0), (String) params.get(1)));
067        } else if ("getParent".equals(methodName)) {
068            return marshaler.marshallResult(tm.getParent((PublicationNode) params.get(0)));
069        } else if ("getPublishedDocumentInNode".equals(methodName)) {
070            return marshaler.marshallResult(tm.getPublishedDocumentInNode((PublicationNode) params.get(0)));
071        } else if ("initRemoteSession".equals(methodName)) {
072            return marshaler.marshallResult(
073                    tm.initRemoteSession((String) params.get(0), (Map<String, String>) params.get(1)));
074        } else if ("release".equals(methodName)) {
075            tm.release((String) params.get(0));
076            return null;
077        } else if ("publish".equals(methodName)) {
078            if (params.size() == 2 || params.get(2) == null) {
079                return marshaler.marshallResult(
080                        tm.publish((DocumentModel) params.get(0), (PublicationNode) params.get(1)));
081            } else {
082                return marshaler.marshallResult(tm.publish((DocumentModel) params.get(0),
083                        (PublicationNode) params.get(1), (Map<String, String>) params.get(2)));
084            }
085        } else if ("unpublish".equals(methodName)) {
086            if (params.get(0) instanceof DocumentModel) {
087                tm.unpublish((DocumentModel) params.get(0), (PublicationNode) params.get(1));
088            } else if (params.get(1) instanceof PublishedDocument) {
089                tm.unpublish((String) params.get(0), (PublishedDocument) params.get(1));
090            }
091            return null;
092        } else {
093            throw new NuxeoException("Unable to handle unknown method " + methodName);
094        }
095    }
096
097}