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