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