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