KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > net > s3 > Handler


1 /* $Id: Handler.java,v 1.1.2.1 2007/01/13 01:31:39 stack-sf Exp $
2  *
3  * Created October 28th, 2006
4  *
5  * Copyright (C) 2006 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.net.s3;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30 import java.net.URLStreamHandler JavaDoc;
31 import java.text.SimpleDateFormat JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.TimeZone JavaDoc;
35
36 /**
37  * A protocol handler for an s3 scheme. To add this handler to the
38  * java.net.URL set, you must define the system property
39  * <code>-Djava.protocol.handler.pkgs=org.archive.net</code>. So this handler
40  * gets triggered, your S3 URLs must have a scheme of 's3' as in:
41  * <code>s3://s3.amazonaws.com/org.archive-scratch/diff.txt</code> (TODO: An
42  * s3s handler for secure access). When run, this code will look for values in
43  * system properties, <code>aws.access.key.id</code> and
44  * <code>aws.access.key.secret</code>, to use authenticating w/ s3. This
45  * handler has dependency on a couple of the classes from s3-library-examples.
46  *
47  * @see http://developer.amazonwebservices.com/connect/entry.jspa?externalID=132&categoryID=47
48  *
49  * @author stack
50  */

51 public class Handler extends URLStreamHandler JavaDoc {
52     private static final String JavaDoc AWS_ACCESS_KEY_ID = "aws.access.key.id";
53     private static final String JavaDoc AWS_ACCESS_KEY_SECRET = "aws.access.key.secret";
54     // TODO: Use JVM Locale.
55
private static final SimpleDateFormat JavaDoc format =
56         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss ", Locale.US);
57     static {
58         format.setTimeZone(TimeZone.getTimeZone("GMT"));
59     }
60     protected URLConnection JavaDoc openConnection(URL JavaDoc u)
61     throws IOException JavaDoc {
62         // Do I need to strip any leading '/'?
63
String JavaDoc resource = u.getFile();
64         if (resource.charAt(0) == '/') {
65             resource = resource.substring(1);
66         }
67         URL JavaDoc h = new URL JavaDoc("http", u.getHost(), u.getPort(), u.getFile());
68         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)h.openConnection();
69         addAuthHeader(connection, "GET", resource);
70
71         return connection;
72     }
73     
74     
75     /**
76      * Add the appropriate Authorization header to the HttpURLConnection.
77      * Below is based on code from s3-example-library from the AWSAuthConnection
78      * class.
79      * @param connection The HttpURLConnection to which the header will be added.
80      * @param method The HTTP method to use (GET, PUT, DELETE)
81      * @param resource The resource name (bucketName + "/" + key).
82      */

83     private void addAuthHeader(HttpURLConnection JavaDoc connection, String JavaDoc method,
84             String JavaDoc resource) {
85         if (connection.getRequestProperty("Date") == null) {
86             connection.setRequestProperty("Date",
87                 format.format(new Date JavaDoc()) + "GMT");
88         }
89         if (connection.getRequestProperty("Content-Type") == null) {
90             connection.setRequestProperty("Content-Type", "");
91         }
92
93         String JavaDoc id = System.getProperty(AWS_ACCESS_KEY_ID);
94         if (id == null || id.length() <= 0) {
95             throw new NullPointerException JavaDoc(AWS_ACCESS_KEY_ID +
96                 " system property is empty");
97         }
98         String JavaDoc secret = System.getProperty(AWS_ACCESS_KEY_SECRET);
99         if (secret == null || secret.length() <= 0) {
100             throw new NullPointerException JavaDoc(AWS_ACCESS_KEY_SECRET +
101                 " system property is empty");
102         }
103         String JavaDoc canonicalString = com.amazon.s3.Utils.makeCanonicalString(method,
104             resource, connection.getRequestProperties());
105         String JavaDoc encodedCanonical = com.amazon.s3.Utils.encode(secret,
106             canonicalString, false);
107         connection.setRequestProperty("Authorization",
108             "AWS " + id + ":" + encodedCanonical);
109     }
110
111     /**
112      * Main dumps rsync file to STDOUT.
113      * @param args
114      * @throws IOException
115      */

116     public static void main(String JavaDoc[] args)
117     throws IOException JavaDoc {
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 JavaDoc u = new URL JavaDoc(args[0]);
126         URLConnection JavaDoc connect = u.openConnection();
127         // Write download to stdout.
128
final int bufferlength = 4096;
129         byte [] buffer = new byte [bufferlength];
130         InputStream JavaDoc 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