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.client;
019
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentLocation;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
024import org.nuxeo.ecm.platform.publisher.api.*;
025import org.nuxeo.ecm.platform.publisher.impl.service.AbstractRemotableTree;
026import org.nuxeo.ecm.platform.publisher.impl.service.ProxyNode;
027import org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler;
028import org.nuxeo.ecm.platform.publisher.remoting.marshaling.basic.BasicPublicationNode;
029import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.RemotePublisherMarshaler;
030import org.nuxeo.ecm.platform.publisher.remoting.restProxies.RemotePublicationTreeManagerRestProxy;
031import org.nuxeo.runtime.api.Framework;
032
033import java.util.ArrayList;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037
038/**
039 * {@link PublicationTree} implementation that points to a remote tree on a remote server.
040 *
041 * @author tiry
042 */
043public class ClientRemotePublicationTree extends AbstractRemotableTree implements PublicationTree {
044
045    private static final long serialVersionUID = 1L;
046
047    protected static final String ORGSERVER_KEY = "originalServer";
048
049    protected static final String USERNAME_KEY = "userName";
050
051    protected static final String PASSWORD_KEY = "password";
052
053    protected static final String BASEURL_KEY = "baseURL";
054
055    protected static final String MARSHALER_KEY = "marshaler";
056
057    protected static final String TARGETTREENAME_KEY = "targetTree";
058
059    public static final String ICON_EXPANDED_KEY = "iconExpanded";
060
061    public static final String ICON_COLLAPSED_KEY = "iconCollapsed";
062
063    public static final String TITLE_KEY = "title";
064
065    protected String targetTreeName;
066
067    protected String name;
068
069    protected String serverSessionId;
070
071    protected String title;
072
073    protected String treeTitle;
074
075    protected String rootPath;
076
077    protected String nodeType;
078
079    protected String iconCollapsed = "/icons/folder.gif";
080
081    protected String iconExpanded = "/icons/folder_open.gif";
082
083    protected PublishedDocumentFactory factory;
084
085    protected ClientRemotePublicationNode rootNode;
086
087    protected CoreSession coreSession;
088
089    public ClientRemotePublicationTree() {
090        // empty
091    }
092
093    protected String getTargetTreeName() {
094        return targetTreeName;
095    }
096
097    @Override
098    protected PublicationNode switchToClientNode(PublicationNode node) {
099        return new ClientRemotePublicationNode(configName, sessionId, node, serverSessionId, treeService,
100                getTargetTreeName());
101    }
102
103    @Override
104    protected PublicationNode switchToServerNode(PublicationNode node) {
105        if (node instanceof ClientRemotePublicationNode) {
106            ClientRemotePublicationNode cNode = (ClientRemotePublicationNode) node;
107            return new BasicPublicationNode(cNode.getNodeType(), cNode.getPath(), cNode.getTitle(),
108                    cNode.getUnwrappedTreeName(), serverSessionId);
109        }
110        if (node instanceof ProxyNode) {
111            ProxyNode rNode = (ProxyNode) node;
112            return new BasicPublicationNode(rNode.getNodeType(), rNode.getPath(), rNode.getTitle(),
113                    getTargetTreeName(), serverSessionId);
114        } else {
115            return node;
116        }
117    }
118
119    public void initTree(String sid, CoreSession coreSession, Map<String, String> parameters,
120            PublishedDocumentFactory factory, String configName, String title) {
121
122        this.sessionId = sid;
123        this.name = "Remote";
124        this.configName = configName;
125        this.factory = factory;
126        this.coreSession = coreSession;
127        treeTitle = title != null ? title : configName;
128
129        String userName = parameters.get(USERNAME_KEY);
130        String password = parameters.get(PASSWORD_KEY);
131        String baseURL = Framework.expandVars(parameters.get(BASEURL_KEY));
132        String marshalerClassName = parameters.get(MARSHALER_KEY);
133
134        targetTreeName = parameters.get(TARGETTREENAME_KEY);
135        if (targetTreeName == null)
136            targetTreeName = name;
137
138        if (parameters.containsKey(ICON_COLLAPSED_KEY)) {
139            iconCollapsed = parameters.get(ICON_COLLAPSED_KEY);
140        }
141        if (parameters.containsKey(ICON_EXPANDED_KEY)) {
142            iconExpanded = parameters.get(ICON_EXPANDED_KEY);
143        }
144
145        RemotePublisherMarshaler marshaler;
146
147        if (marshalerClassName == null) {
148            marshaler = new DefaultMarshaler();
149        } else {
150            try {
151                marshaler = (RemotePublisherMarshaler) Class.forName(marshalerClassName).newInstance();
152            } catch (ReflectiveOperationException e) {
153                marshaler = new DefaultMarshaler();
154            }
155        }
156
157        marshaler.setAssociatedCoreSession(coreSession);
158        marshaler.setParameters(parameters);
159
160        treeService = new RemotePublicationTreeManagerRestProxy(baseURL, userName, password, marshaler);
161
162        Map<String, String> remoteParameters = new HashMap<String, String>();
163        Map<String, String> rTree = treeService.initRemoteSession(targetTreeName, remoteParameters);
164
165        serverSessionId = rTree.get("sessionId");
166        this.title = rTree.get("title");
167        rootPath = rTree.get("path");
168        nodeType = rTree.get("nodeType");
169
170        PublicationNode basicRootNode = new BasicPublicationNode(nodeType, rootPath, this.title, configName, sessionId);
171        rootNode = new ClientRemotePublicationNode(configName, sessionId, basicRootNode, serverSessionId, treeService,
172                getTargetTreeName());
173
174    }
175
176    @Override
177    protected RemotePublicationTreeManager getTreeService() {
178        return treeService;
179    }
180
181    /*
182     * public List<PublicationNode> getTree() { return rootNode.getChildrenNodes(); }
183     */
184
185    public List<PublishedDocument> getChildrenDocuments() {
186        return rootNode.getChildrenDocuments();
187    }
188
189    public String getPath() {
190        return rootPath;
191    }
192
193    public String getTitle() {
194        return title;
195    }
196
197    public String getTreeTitle() {
198        return treeTitle;
199    }
200
201    public String getName() {
202        return name;
203    }
204
205    public String getTreeType() {
206        return "RemoteTree";
207    }
208
209    public String getNodeType() {
210        return nodeType;
211    }
212
213    public String getTreeConfigName() {
214        return configName;
215    }
216
217    public String getSessionId() {
218        return sessionId;
219    }
220
221    public String getRemoteSessionId() {
222        return getServerTreeSessionId();
223    }
224
225    @Override
226    protected String getServerTreeSessionId() {
227        return serverSessionId;
228    }
229
230    public PublishedDocument publish(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
231            {
232
233        doc = factory.snapshotDocumentBeforePublish(doc);
234        return super.publish(doc, targetNode, params);
235    }
236
237    public List<PublicationNode> getChildrenNodes() {
238        return rootNode.getChildrenNodes();
239    }
240
241    public String getType() {
242        return this.getClass().getSimpleName();
243    }
244
245    @Override
246    public List<PublishedDocument> getExistingPublishedDocument(DocumentLocation docLoc) {
247
248        List<PublishedDocument> allPubDocs = new ArrayList<PublishedDocument>();
249
250        List<DocumentModel> possibleDocsToCheck = new ArrayList<DocumentModel>();
251
252        DocumentModel livedoc = coreSession.getDocument(docLoc.getDocRef());
253        if (!livedoc.isVersion()) {
254            possibleDocsToCheck = coreSession.getVersions(docLoc.getDocRef());
255        }
256
257        possibleDocsToCheck.add(0, livedoc);
258
259        for (DocumentModel doc : possibleDocsToCheck) {
260            List<PublishedDocument> pubDocs = getTreeService().getExistingPublishedDocument(getServerTreeSessionId(),
261                    new DocumentLocationImpl(doc));
262            allPubDocs.addAll(pubDocs);
263        }
264
265        return allPubDocs;
266    }
267
268    public String getIconExpanded() {
269        return iconExpanded;
270    }
271
272    public String getIconCollapsed() {
273        return iconCollapsed;
274    }
275
276}