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 static java.nio.charset.StandardCharsets.UTF_8;
022
023import java.io.File;
024import java.io.InputStream;
025import java.util.Base64;
026
027import org.apache.commons.io.FileUtils;
028import org.apache.commons.io.IOUtils;
029import org.apache.http.HttpResponse;
030import org.apache.http.client.methods.HttpGet;
031import org.nuxeo.shell.Argument;
032import org.nuxeo.shell.Command;
033import org.nuxeo.shell.Context;
034import org.nuxeo.shell.Parameter;
035import org.nuxeo.shell.ShellException;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@Command(name = "print", help = "Print operation(s) definition")
041public class PrintOperation implements Runnable {
042
043    @Context
044    protected RemoteContext ctx;
045
046    @Parameter(name = "-u", hasValue = true, help = "The username if any.")
047    protected String u;
048
049    @Parameter(name = "-p", hasValue = true, help = "The password if any.")
050    protected String p;
051
052    @Parameter(name = "-out", hasValue = true, help = "An optional file to save the operation definition into. If not used the definition will be printed on stdout.")
053    protected File out;
054
055    @Argument(name = "operation", index = 0, required = false, completor = OperationNameCompletor.class, help = "The opertation to print.")
056    protected String name;
057
058    @Override
059    public void run() {
060        try {
061            String url = ctx.getClient().getBaseUrl();
062            HttpGet get = new HttpGet(url + (name == null ? "" : name));
063            if (u != null && p != null) {
064                // TODO be able to reuse the context of the automation client
065                String base64 = Base64.getEncoder().encodeToString((u + ":" + p).getBytes(UTF_8));
066                get.setHeader("Authorization", "Basic " + base64);
067            }
068            HttpResponse r = ctx.getClient().http().execute(get);
069            try (InputStream in = r.getEntity().getContent()) {
070                String content = IOUtils.toString(in, UTF_8);
071                if (out == null) {
072                    ctx.getShell().getConsole().println(content);
073                } else {
074                    FileUtils.writeStringToFile(out, content, UTF_8);
075                }
076            }
077        } catch (Exception e) {
078            throw new ShellException(e);
079        }
080    }
081
082}