26
26
import java .io .IOException ;
27
27
import java .io .StringReader ;
28
28
import java .io .StringWriter ;
29
- import java .util .Collections ;
30
29
import java .net .URLEncoder ;
30
+ import java .util .Collections ;
31
31
32
32
import javax .xml .transform .OutputKeys ;
33
33
import javax .xml .transform .Source ;
34
34
import javax .xml .transform .Transformer ;
35
+ import javax .xml .transform .TransformerException ;
35
36
import javax .xml .transform .TransformerFactory ;
36
37
import javax .xml .transform .stream .StreamResult ;
37
38
import javax .xml .transform .stream .StreamSource ;
39
+ import java .security .GeneralSecurityException ;
38
40
39
41
/**
40
42
* Sample code used in the Cloud Storage Java documentation.
@@ -49,43 +51,53 @@ private StorageSample() { }
49
51
private static final String STORAGE_SCOPE =
50
52
"https://www.googleapis.com/auth/devstorage.read_write" ;
51
53
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
+ }
54
86
55
87
/**
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 .
57
89
*
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.
59
92
*/
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 ());
67
98
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 {
89
101
Transformer transformer = TransformerFactory .newInstance ()
90
102
.newTransformer (); // An identity transformer
91
103
transformer .setOutputProperty (OutputKeys .DOCTYPE_SYSTEM , "testing.dtd" );
@@ -98,6 +110,27 @@ public static void main(final String[] args) {
98
110
// Pretty print the output XML.
99
111
System .out .println ("\n Bucket listing for " + bucketName + ":\n " );
100
112
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 );
101
134
System .exit (0 );
102
135
103
136
} catch (IOException e ) {
0 commit comments