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