1 23 package org.archive.net.s3; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.HttpURLConnection ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 import java.net.URLStreamHandler ; 31 import java.text.SimpleDateFormat ; 32 import java.util.Date ; 33 import java.util.Locale ; 34 import java.util.TimeZone ; 35 36 51 public class Handler extends URLStreamHandler { 52 private static final String AWS_ACCESS_KEY_ID = "aws.access.key.id"; 53 private static final String AWS_ACCESS_KEY_SECRET = "aws.access.key.secret"; 54 private static final SimpleDateFormat format = 56 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss ", Locale.US); 57 static { 58 format.setTimeZone(TimeZone.getTimeZone("GMT")); 59 } 60 protected URLConnection openConnection(URL u) 61 throws IOException { 62 String resource = u.getFile(); 64 if (resource.charAt(0) == '/') { 65 resource = resource.substring(1); 66 } 67 URL h = new URL ("http", u.getHost(), u.getPort(), u.getFile()); 68 HttpURLConnection connection = (HttpURLConnection )h.openConnection(); 69 addAuthHeader(connection, "GET", resource); 70 71 return connection; 72 } 73 74 75 83 private void addAuthHeader(HttpURLConnection connection, String method, 84 String resource) { 85 if (connection.getRequestProperty("Date") == null) { 86 connection.setRequestProperty("Date", 87 format.format(new Date ()) + "GMT"); 88 } 89 if (connection.getRequestProperty("Content-Type") == null) { 90 connection.setRequestProperty("Content-Type", ""); 91 } 92 93 String id = System.getProperty(AWS_ACCESS_KEY_ID); 94 if (id == null || id.length() <= 0) { 95 throw new NullPointerException (AWS_ACCESS_KEY_ID + 96 " system property is empty"); 97 } 98 String secret = System.getProperty(AWS_ACCESS_KEY_SECRET); 99 if (secret == null || secret.length() <= 0) { 100 throw new NullPointerException (AWS_ACCESS_KEY_SECRET + 101 " system property is empty"); 102 } 103 String canonicalString = com.amazon.s3.Utils.makeCanonicalString(method, 104 resource, connection.getRequestProperties()); 105 String encodedCanonical = com.amazon.s3.Utils.encode(secret, 106 canonicalString, false); 107 connection.setRequestProperty("Authorization", 108 "AWS " + id + ":" + encodedCanonical); 109 } 110 111 116 public static void main(String [] args) 117 throws IOException { 118 if (args.length != 1) { 119 System.out.println("Usage: java " + 120 "-D" + AWS_ACCESS_KEY_ID + "=AWS_ACCESS_KEY_ID " + 121 "-D" + AWS_ACCESS_KEY_SECRET + "=AWS_ACCESS_KEY_SECRET " + 122 "org.archive.net.s3.Handler s3://AWS_HOST/bucket/key"); 123 System.exit(1); 124 } 125 URL u = new URL (args[0]); 126 URLConnection connect = u.openConnection(); 127 final int bufferlength = 4096; 129 byte [] buffer = new byte [bufferlength]; 130 InputStream is = connect.getInputStream(); 131 try { 132 for (int count = is.read(buffer, 0, bufferlength); 133 (count = is.read(buffer, 0, bufferlength)) != -1;) { 134 System.out.write(buffer, 0, count); 135 } 136 System.out.flush(); 137 } finally { 138 is.close(); 139 } 140 } 141 } 142 | Popular Tags |