2626import java .io .IOException ;
2727import java .io .StringReader ;
2828import java .io .StringWriter ;
29- import java .util .Collections ;
3029import java .net .URLEncoder ;
30+ import java .util .Collections ;
3131
3232import javax .xml .transform .OutputKeys ;
3333import javax .xml .transform .Source ;
3434import javax .xml .transform .Transformer ;
35+ import javax .xml .transform .TransformerException ;
3536import javax .xml .transform .TransformerFactory ;
3637import javax .xml .transform .stream .StreamResult ;
3738import javax .xml .transform .stream .StreamSource ;
39+ import java .security .GeneralSecurityException ;
3840
3941/**
4042 * Sample code used in the Cloud Storage Java documentation.
@@ -49,43 +51,53 @@ private StorageSample() { }
4951 private static final String STORAGE_SCOPE =
5052 "https://www.googleapis.com/auth/devstorage.read_write" ;
5153
52- /** Global instance of the HTTP transport. */
53- private static HttpTransport httpTransport ;
54+ /**
55+ * Fetches the listing of the given bucket.
56+ *
57+ * @param bucketName the name of the bucket to list.
58+ *
59+ * @return the raw XML containing the listing of the bucket.
60+ * @throws IOException if there's an error communicating with Cloud Storage.
61+ * @throws GeneralSecurityException for errors creating https connection.
62+ */
63+ public static String listBucket (final String bucketName )
64+ throws IOException , GeneralSecurityException {
65+ //[START snippet]
66+ // Build an account credential.
67+ GoogleCredential credential = GoogleCredential .getApplicationDefault ()
68+ .createScoped (Collections .singleton (STORAGE_SCOPE ));
69+
70+ // Set up and execute a Google Cloud Storage request.
71+ String uri = "https://storage.googleapis.com/"
72+ + URLEncoder .encode (bucketName , "UTF-8" );
73+
74+ HttpTransport httpTransport = GoogleNetHttpTransport .newTrustedTransport ();
75+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (
76+ credential );
77+ GenericUrl url = new GenericUrl (uri );
78+
79+ HttpRequest request = requestFactory .buildGetRequest (url );
80+ HttpResponse response = request .execute ();
81+ String content = response .parseAsString ();
82+ //[END snippet]
83+
84+ return content ;
85+ }
5486
5587 /**
56- * A command-line handler to display the bucket passed in as an argument .
88+ * Prints out the contents of the given xml, in a more readable form .
5789 *
58- * @param args the array of command-line arguments.
90+ * @param bucketName the name of the bucket you're listing.
91+ * @param content the raw XML string.
5992 */
60- public static void main (final String [] args ) {
61- try {
62- httpTransport = GoogleNetHttpTransport .newTrustedTransport ();
63- // Check for valid setup.
64- Preconditions .checkArgument (args .length == 1 ,
65- "Please pass in the Google Cloud Storage bucket name to display" );
66- String bucketName = args [0 ];
93+ private static void prettyPrintXml (
94+ final String bucketName , final String content ) {
95+ // Instantiate transformer input.
96+ Source xmlInput = new StreamSource (new StringReader (content ));
97+ StreamResult xmlOutput = new StreamResult (new StringWriter ());
6798
68- //[START snippet]
69- // Build an account credential.
70- GoogleCredential credential = GoogleCredential .getApplicationDefault ()
71- .createScoped (Collections .singleton (STORAGE_SCOPE ));
72-
73- // Set up and execute a Google Cloud Storage request.
74- String uri = "https://storage.googleapis.com/"
75- + URLEncoder .encode (bucketName , "UTF-8" );
76- HttpRequestFactory requestFactory = httpTransport .createRequestFactory (
77- credential );
78- GenericUrl url = new GenericUrl (uri );
79- HttpRequest request = requestFactory .buildGetRequest (url );
80- HttpResponse response = request .execute ();
81- String content = response .parseAsString ();
82- //[END snippet]
83-
84- // Instantiate transformer input.
85- Source xmlInput = new StreamSource (new StringReader (content ));
86- StreamResult xmlOutput = new StreamResult (new StringWriter ());
87-
88- // Configure transformer.
99+ // Configure transformer.
100+ try {
89101 Transformer transformer = TransformerFactory .newInstance ()
90102 .newTransformer (); // An identity transformer
91103 transformer .setOutputProperty (OutputKeys .DOCTYPE_SYSTEM , "testing.dtd" );
@@ -98,6 +110,27 @@ public static void main(final String[] args) {
98110 // Pretty print the output XML.
99111 System .out .println ("\n Bucket listing for " + bucketName + ":\n " );
100112 System .out .println (xmlOutput .getWriter ().toString ());
113+ } catch (TransformerException e ) {
114+ e .printStackTrace ();
115+ }
116+ }
117+
118+ /**
119+ * A command-line handler to display the bucket passed in as an argument.
120+ *
121+ * @param args the array of command-line arguments.
122+ */
123+ public static void main (final String [] args ) {
124+ try {
125+ // Check for valid setup.
126+ Preconditions .checkArgument (
127+ args .length == 1 ,
128+ "Please pass in the Google Cloud Storage bucket name to display" );
129+ String bucketName = args [0 ];
130+
131+ String content = listBucket (bucketName );
132+
133+ prettyPrintXml (bucketName , content );
101134 System .exit (0 );
102135
103136 } catch (IOException e ) {
0 commit comments