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