View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.pgm;
46  
47  import static java.lang.Integer.valueOf;
48  
49  import java.io.EOFException;
50  import java.io.File;
51  import java.io.FileInputStream;
52  import java.io.FileNotFoundException;
53  import java.io.IOException;
54  import java.io.InputStream;
55  import java.io.OutputStream;
56  import java.net.URLConnection;
57  import java.text.MessageFormat;
58  import java.util.Properties;
59  
60  import org.eclipse.jgit.pgm.internal.CLIText;
61  import org.eclipse.jgit.transport.AmazonS3;
62  import org.kohsuke.args4j.Argument;
63  
64  @Command(name = "amazon-s3-client", common = false, usage = "usage_CommandLineClientForamazonsS3Service")
65  class AmazonS3Client extends TextBuiltin {
66  	@Argument(index = 0, metaVar = "metaVar_connProp", required = true)
67  	private File propertyFile;
68  
69  	@Argument(index = 1, metaVar = "metaVar_op", required = true)
70  	private String op;
71  
72  	@Argument(index = 2, metaVar = "metaVar_bucket", required = true)
73  	private String bucket;
74  
75  	@Argument(index = 3, metaVar = "metaVar_KEY", required = true)
76  	private String key;
77  
78  	/** {@inheritDoc} */
79  	@Override
80  	protected final boolean requiresRepository() {
81  		return false;
82  	}
83  
84  	/** {@inheritDoc} */
85  	@Override
86  	protected void run() throws Exception {
87  		final AmazonS3 s3 = new AmazonS3(properties());
88  
89  		if ("get".equals(op)) { //$NON-NLS-1$
90  			final URLConnection c = s3.get(bucket, key);
91  			int len = c.getContentLength();
92  			try (InputStream in = c.getInputStream()) {
93  				outw.flush();
94  				final byte[] tmp = new byte[2048];
95  				while (len > 0) {
96  					final int n = in.read(tmp);
97  					if (n < 0)
98  						throw new EOFException(MessageFormat.format(
99  								CLIText.get().expectedNumberOfbytes,
100 								valueOf(len)));
101 					outs.write(tmp, 0, n);
102 					len -= n;
103 				}
104 				outs.flush();
105 			}
106 
107 		} else if ("ls".equals(op) || "list".equals(op)) { //$NON-NLS-1$//$NON-NLS-2$
108 			for (String k : s3.list(bucket, key))
109 				outw.println(k);
110 
111 		} else if ("rm".equals(op) || "delete".equals(op)) { //$NON-NLS-1$ //$NON-NLS-2$
112 			s3.delete(bucket, key);
113 
114 		} else if ("put".equals(op)) { //$NON-NLS-1$
115 			try (OutputStream os = s3.beginPut(bucket, key, null, null)) {
116 				final byte[] tmp = new byte[2048];
117 				int n;
118 				while ((n = ins.read(tmp)) > 0)
119 					os.write(tmp, 0, n);
120 			}
121 		} else {
122 			throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
123 		}
124 	}
125 
126 	private Properties properties() {
127 		try {
128 			try (InputStream in = new FileInputStream(propertyFile)) {
129 				final Properties p = new Properties();
130 				p.load(in);
131 				return p;
132 			}
133 		} catch (FileNotFoundException e) {
134 			throw die(MessageFormat.format(CLIText.get().noSuchFile, propertyFile), e);
135 		} catch (IOException e) {
136 			throw die(MessageFormat.format(CLIText.get().cannotReadBecause, propertyFile, e.getMessage()), e);
137 		}
138 	}
139 }