001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (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 * bstefanescu 016 */ 017package org.nuxeo.shell.automation; 018 019import java.net.URL; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.nuxeo.ecm.automation.client.AutomationClient; 024import org.nuxeo.ecm.automation.client.Session; 025import org.nuxeo.ecm.automation.client.adapters.DocumentService; 026import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient; 027import org.nuxeo.ecm.automation.client.model.DocRef; 028import org.nuxeo.ecm.automation.client.model.Document; 029import org.nuxeo.ecm.automation.client.model.PathRef; 030import org.nuxeo.shell.Shell; 031import org.nuxeo.shell.ShellException; 032import org.nuxeo.shell.utils.Path; 033 034/** 035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 036 */ 037public class RemoteContext { 038 039 protected Shell shell; 040 041 protected AutomationFeature feature; 042 043 protected HttpAutomationClient client; 044 045 protected Session session; 046 047 protected DocumentService ds; 048 049 protected Document doc; 050 051 protected List<Document> stack; 052 053 protected String userName; 054 055 protected String host; 056 057 public RemoteContext(AutomationFeature feature, HttpAutomationClient client, Session session, String dir) 058 throws Exception { 059 this.shell = Shell.get(); 060 this.client = client; 061 this.session = session; 062 ds = session.getAdapter(DocumentService.class); 063 stack = new ArrayList<Document>(); 064 try { 065 doc = ds.getDocument(dir); 066 } catch (Throwable e) { 067 throw new ShellException("Cannot access to " + dir + " (use another directory using the -d parameter)", e); 068 } 069 userName = session.getLogin().getUsername(); 070 host = new URL(client.getBaseUrl()).getHost(); 071 shell.putContextObject(RemoteContext.class, this); 072 shell.putContextObject(AutomationClient.class, client); 073 shell.putContextObject(Session.class, session); 074 shell.putContextObject(DocumentService.class, ds); 075 } 076 077 public String getUserName() { 078 return userName; 079 } 080 081 public String getHost() { 082 return host; 083 } 084 085 public void dispose() { 086 shell.removeContextObject(RemoteContext.class); 087 shell.removeContextObject(AutomationClient.class); 088 shell.removeContextObject(Session.class); 089 shell.removeContextObject(DocumentService.class); 090 } 091 092 public Shell getShell() { 093 return shell; 094 } 095 096 public AutomationFeature getFeature() { 097 return feature; 098 } 099 100 public HttpAutomationClient getClient() { 101 return client; 102 } 103 104 public Session getSession() { 105 return session; 106 } 107 108 public Document getDocument() { 109 return doc; 110 } 111 112 public Path resolvePath(String path) { 113 if (path == null) { 114 return new Path(getDocument().getPath()); 115 } 116 if (path.startsWith("/")) { 117 return new Path(path); 118 } else { 119 return new Path(doc.getPath()).append(path); 120 } 121 } 122 123 public DocRef resolveRef(String path) { 124 if (path == null) { 125 return getDocument(); 126 } 127 if (path.startsWith("doc:")) { 128 return DocRef.newRef(path.substring("doc:".length())); 129 } 130 return new PathRef(resolvePath(path).toString()); 131 } 132 133 public Document resolveDocument(String path) { 134 return resolveDocument(path, null); 135 } 136 137 public Document resolveDocument(String path, String schemas) { 138 DocRef ref = null; 139 try { 140 ref = resolveRef(path); 141 return ds.getDocument(ref, schemas); 142 } catch (Exception e) { 143 throw new ShellException("Failed to fetch document: " + ref); 144 } 145 } 146 147 public void setDocument(Document doc) { 148 this.doc = doc; 149 } 150 151 public Document peekDocument() { 152 if (!stack.isEmpty()) { 153 return stack.get(stack.size() - 1); 154 } 155 return null; 156 } 157 158 public Document pushDocument(Document doc) { 159 setDocument(doc); 160 Document old = peekDocument(); 161 stack.add(doc); 162 return old; 163 } 164 165 public Document popDocument() { 166 if (!stack.isEmpty()) { 167 Document doc = stack.remove(stack.size() - 1); 168 setDocument(peekDocument()); 169 return doc; 170 } 171 return null; 172 } 173 174 public DocumentService getDocumentService() { 175 return ds; 176 } 177 178 public List<Document> getStack() { 179 return stack; 180 } 181}