Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2013, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.as.host.controller.discovery;
  
  import static org.jboss.as.host.controller.HostControllerMessages.MESSAGES;
  
  import java.io.DataInput;
  import java.net.URL;
  import java.util.Arrays;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Locale;
  import java.util.List;
  import java.util.Map;
  import java.util.TreeMap;
  import javax.crypto.Mac;
Collection of utility methods required for S3 discovery. Some methods here are based on similar ones from JGroups' S3_PING.java. The S3 access code reuses the example shipped by Amazon, like S3_PING.java does.

Author(s):
Farah Juma
  
  public class S3Util {

    
Get the domain controller data from the given byte buffer.

Parameters:
buffer the byte buffer
Returns:
the domain controller data
Throws:
java.lang.Exception
  
      public static DomainControllerData domainControllerDataFromByteBuffer(byte[] bufferthrows Exception {
          if(buffer == null) {
              return null;
          }
          DomainControllerData retval = null;
          ByteArrayInputStream in_stream = new ByteArrayInputStream(buffer);
          DataInputStream in = new DataInputStream(in_stream);
          retval = new DomainControllerData();
          retval.readFrom(in);
          in.close();
          return retval;
      }

    
Write the domain controller data to a byte buffer.

Parameters:
dcData the domain controller data
Returns:
the byte buffer
Throws:
java.lang.Exception
 
     public static byte[] domainControllerDataToByteBuffer(DomainControllerData dcDatathrows Exception {
         byte[] result=null;
         final ByteArrayOutputStream out_stream=new ByteArrayOutputStream(512);
         DataOutputStream out=new DataOutputStream(out_stream);
         dcData.writeTo(out);
         result=out_stream.toByteArray();
         out.close();
         return result;
     }

    
Sanitize bucket and folder names according to AWS guidelines.
 
     protected static String sanitize(final String name) {
         String retval=name;
         retval=retval.replace('/''-');
         retval=retval.replace('\\''-');
         return retval;
     }

    
Use this helper method to generate pre-signed S3 urls. You'll need to generate urls for both the put and delete http methods. Example: Your AWS Access Key is "abcd". Your AWS Secret Access Key is "efgh". You want this node to write its information to "/S3/master/jboss-domain-master-data". So, your bucket is "S3" and your key is "master/jboss-domain-master-data". You want this to expire one year from now, or (System.currentTimeMillis / 1000) + (60 * 60 * 24 * 365) Let's assume that this equals 1316286684 Here's how to generate the value for the pre_signed_put_url property: String putUrl = S3Util.generatePreSignedUrl("abcd", "efgh", "put", "S3", "master/jboss-domain-master-data", 1316286684); Here's how to generate the value for the pre_signed_delete_url property: String deleteUrl = S3Util.generatePreSignedUrl("abcd", "efgh", "delete", "S3", "master/jboss-domain-master-data", 1316286684);

Parameters:
awsAccessKey Your AWS Access Key
awsSecretAccessKey Your AWS Secret Access Key
method The HTTP method - use "put" or "delete" for use with S3_PING
bucket The S3 bucket you want to write to
key The key within the bucket to write to
expirationDate The date this pre-signed url should expire, in seconds since epoch
Returns:
The pre-signed url to be used in pre_signed_put_url or pre_signed_delete_url properties
 
     public static String generatePreSignedUrl(String awsAccessKeyString awsSecretAccessKeyString method,
                                        String bucketString keylong expirationDate) {
         Map headers = new HashMap();
         if (method.equalsIgnoreCase("PUT")) {
             headers.put("x-amz-acl", Arrays.asList("public-read"));
         }
         return Utils.generateQueryStringAuthentication(awsAccessKeyawsSecretAccessKeymethod,
                                                        bucketkeynew HashMap(), headers,
                                                        expirationDate);
     }
 
     public static String readString(DataInput inthrows Exception {
         int b=in.readByte();
         if(b == 1)
             return in.readUTF();
         return null;
     }
 
     public static void writeString(String sDataOutput outthrows Exception {
         if(s != null) {
             out.write(1);
             out.writeUTF(s);
         }
         else {
             out.write(0);
         }
     }

    
Class that manipulates pre-signed urls. This has been copied from S3_PING.java since it is not declared with public access in S3_PING.java.
 
     static class PreSignedUrlParser {
         String bucket = "";
         String prefix = "";
 
         public PreSignedUrlParser(String preSignedUrl) {
             try {
                 URL url = new URL(preSignedUrl);
                 String path = url.getPath();
                 String[] pathParts = path.split("/");
 
                 if (pathParts.length < 3) {
                     throw .preSignedUrlMustPointToFile(preSignedUrl);
                 }
                 if (pathParts.length > 4) {
                     throw .invalidPreSignedUrlLength(preSignedUrl);
                 }
                 this. = pathParts[1];
                 if (pathParts.length > 3) {
                     this. = pathParts[2];
                 }
             } catch (MalformedURLException ex) {
                 throw .invalidPreSignedUrl(preSignedUrl);
             }
         }
 
         public String getBucket() {
             return ;
         }
 
         public String getPrefix() {
             return ;
         }
     }

    
The remaining classes have been copied from Amazon's sample code. Note: These nested classes are also defined in S3_PING.java. However, they are not declared with public access in S3_PING.java and so we have copied them here and added i18n for the error messages. /
 
     static class AWSAuthConnection {
         public static final String LOCATION_DEFAULT=null;
         public static final String LOCATION_EU="EU";
 
         private String awsAccessKeyId;
         private String awsSecretAccessKey;
         private boolean isSecure;
         private String server;
         private int port;
         private CallingFormat callingFormat;
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKey) {
             this(awsAccessKeyIdawsSecretAccessKeytrue);
         }
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKeyboolean isSecure) {
             this(awsAccessKeyIdawsSecretAccessKeyisSecure.);
         }
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKeyboolean isSecure,
                                  String server) {
             this(awsAccessKeyIdawsSecretAccessKeyisSecureserver,
                  isSecure. : .);
         }
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKeyboolean isSecure,
                                  String serverint port) {
             this(awsAccessKeyIdawsSecretAccessKeyisSecureserverport, CallingFormat.getSubdomainCallingFormat());
 
         }
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKeyboolean isSecure,
                                  String serverCallingFormat format) {
             this(awsAccessKeyIdawsSecretAccessKeyisSecureserver,
                  isSecure. : .,
                  format);
         }

        
Create a new interface to interact with S3 with the given credential and connection parameters

Parameters:
awsAccessKeyId Your user key into AWS
awsSecretAccessKey The secret string used to generate signatures for authentication.
isSecure use SSL encryption
server Which host to connect to. Usually, this will be s3.amazonaws.com
port Which port to use.
format Type of request Regular/Vanity or Pure Vanity domain
 
         public AWSAuthConnection(String awsAccessKeyIdString awsSecretAccessKeyboolean isSecure,
                                  String serverint portCallingFormat format) {
             this.=awsAccessKeyId;
             this.=awsSecretAccessKey;
             this.=isSecure;
             this.=server;
             this.=port;
             this.=format;
         }

        
Creates a new bucket.

Parameters:
bucket The name of the bucket to create.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response createBucket(String bucketMap headersthrows IOException {
             return createBucket(bucketnullheaders);
         }

        
Creates a new bucket.

Parameters:
bucket The name of the bucket to create.
location Desired location ("EU") (or null for default).
headers A Map of String to List of Strings representing the http headers to pass (can be null).
Throws:
java.lang.IllegalArgumentException on invalid location
 
         public Response createBucket(String bucketString locationMap headersthrows IOException {
             String body;
             if(location == null) {
                 body=null;
             }
             else if(.equals(location)) {
                 if(!.supportsLocatedBuckets())
                     throw .creatingBucketWithUnsupportedCallingFormat();
                 body="<CreateBucketConstraint><LocationConstraint>" + location + "</LocationConstraint></CreateBucketConstraint>";
             }
             else
                 throw .invalidS3Location(location);
 
             // validate bucket name
             if(!Utils.validateBucketName(bucket))
                 throw .invalidS3Bucket(bucket);
 
             HttpURLConnection request=makeRequest("PUT"bucket""nullheaders);
             if(body != null) {
                 request.setDoOutput(true);
                 request.getOutputStream().write(body.getBytes(.));
             }
             return new Response(request);
         }

        
Check if the specified bucket exists (via a HEAD request)

Parameters:
bucket The name of the bucket to check
Returns:
true if HEAD access returned success
 
         public boolean checkBucketExists(String bucketthrows IOException {
             HttpURLConnection response=makeRequest("HEAD"bucket""nullnull);
             int httpCode=response.getResponseCode();
 
             if(httpCode >= 200 && httpCode < 300)
                 return true;
             if(httpCode == .// bucket doesn't exist
                 return false;
             throw .bucketAuthenticationFailure(buckethttpCoderesponse.getResponseMessage());
         }

        
Lists the contents of a bucket.

Parameters:
bucket The name of the bucket to create.
prefix All returned keys will start with this string (can be null).
marker All returned keys will be lexographically greater than this string (can be null).
maxKeys The maximum number of keys to return (can be null).
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public ListBucketResponse listBucket(String bucketString prefixString marker,
                                              Integer maxKeysMap headersthrows IOException {
             return listBucket(bucketprefixmarkermaxKeysnullheaders);
         }

        
Lists the contents of a bucket.

Parameters:
bucket The name of the bucket to list.
prefix All returned keys will start with this string (can be null).
marker All returned keys will be lexographically greater than this string (can be null).
maxKeys The maximum number of keys to return (can be null).
delimiter Keys that contain a string between the prefix and the first occurrence of the delimiter will be rolled up into a single element.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public ListBucketResponse listBucket(String bucketString prefixString marker,
                                              Integer maxKeysString delimiterMap headersthrows IOException {
 
             Map pathArgs=Utils.paramsForListOptions(prefixmarkermaxKeysdelimiter);
             return new ListBucketResponse(makeRequest("GET"bucket""pathArgsheaders));
         }

        
Deletes a bucket.

Parameters:
bucket The name of the bucket to delete.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response deleteBucket(String bucketMap headersthrows IOException {
             return new Response(makeRequest("DELETE"bucket""nullheaders));
         }

        
Writes an object to S3.

Parameters:
bucket The name of the bucket to which the object will be added.
key The name of the key to use.
object An S3Object containing the data to write.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response put(String bucketString keyS3Object objectMap headersthrows IOException {
             HttpURLConnection request=
                     makeRequest("PUT"bucket, Utils.urlencode(key), nullheadersobject);
 
             request.setDoOutput(true);
             request.getOutputStream().write(object.data == nullnew byte[]{} : object.data);
 
             return new Response(request);
         }
 
         public Response put(String preSignedUrlS3Object objectMap headersthrows IOException {
             HttpURLConnection request = makePreSignedRequest("PUT"preSignedUrlheaders);
             request.setDoOutput(true);
             request.getOutputStream().write(object.data == nullnew byte[]{} : object.data);
 
             return new Response(request);
         }

        
Creates a copy of an existing S3 Object. In this signature, we will copy the existing metadata. The default access control policy is private; if you want to override it, please use x-amz-acl in the headers.

Parameters:
sourceBucket The name of the bucket where the source object lives.
sourceKey The name of the key to copy.
destinationBucket The name of the bucket to which the object will be added.
destinationKey The name of the key to use.
headers A Map of String to List of Strings representing the http headers to pass (can be null). You may wish to set the x-amz-acl header appropriately.
 
         public Response copy(String sourceBucketString sourceKeyString destinationBucketString destinationKeyMap headers)
                 throws IOException {
             S3Object object=new S3Object(new byte[]{}, new HashMap());
             headers=headers == nullnew HashMap() : new HashMap(headers);
             headers.put("x-amz-copy-source", Arrays.asList(sourceBucket + "/" + sourceKey));
             headers.put("x-amz-metadata-directive", Arrays.asList("COPY"));
             return verifyCopy(put(destinationBucketdestinationKeyobjectheaders));
         }

        
Creates a copy of an existing S3 Object. In this signature, we will replace the existing metadata. The default access control policy is private; if you want to override it, please use x-amz-acl in the headers.

Parameters:
sourceBucket The name of the bucket where the source object lives.
sourceKey The name of the key to copy.
destinationBucket The name of the bucket to which the object will be added.
destinationKey The name of the key to use.
metadata A Map of String to List of Strings representing the S3 metadata for the new object.
headers A Map of String to List of Strings representing the http headers to pass (can be null). You may wish to set the x-amz-acl header appropriately.
 
         public Response copy(String sourceBucketString sourceKeyString destinationBucketString destinationKeyMap metadataMap headers)
                 throws IOException {
             S3Object object=new S3Object(new byte[]{}, metadata);
             headers=headers == nullnew HashMap() : new HashMap(headers);
             headers.put("x-amz-copy-source", Arrays.asList(sourceBucket + "/" + sourceKey));
             headers.put("x-amz-metadata-directive", Arrays.asList("REPLACE"));
             return verifyCopy(put(destinationBucketdestinationKeyobjectheaders));
         }

        
Copy sometimes returns a successful response and starts to send whitespace characters to us. This method processes those whitespace characters and will throw an exception if the response is either unknown or an error.

Parameters:
response Response object from the PUT request.
Returns:
The response with the input stream drained.
Throws:
java.io.IOException If anything goes wrong.
 
         private static Response verifyCopy(Response responsethrows IOException {
             if(response.connection.getResponseCode() < 400) {
                 byte[] body=GetResponse.slurpInputStream(response.connection.getInputStream());
                 String message=new String(body);
                 if(message.contains("<Error")) {
                     throw new IOException(message.substring(message.indexOf("<Error")));
                 }
                 else if(message.contains("</CopyObjectResult>")) {
                     // It worked!
                 }
                 else {
                     throw .unexpectedResponse(message);
                 }
             }
             return response;
         }

        
Reads an object from S3.

Parameters:
bucket The name of the bucket where the object lives.
key The name of the key to use.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public GetResponse get(String bucketString keyMap headersthrows IOException {
             return new GetResponse(makeRequest("GET"bucket, Utils.urlencode(key), nullheaders));
         }

        
Deletes an object from S3.

Parameters:
bucket The name of the bucket where the object lives.
key The name of the key to use.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response delete(String bucketString keyMap headersthrows IOException {
             return new Response(makeRequest("DELETE"bucket, Utils.urlencode(key), nullheaders));
         }
 
         public Response delete(String preSignedUrlthrows IOException {
             return new Response(makePreSignedRequest("DELETE"preSignedUrlnull));
         }

        
Get the requestPayment xml document for a given bucket

Parameters:
bucket The name of the bucket
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public GetResponse getBucketRequestPayment(String bucketMap headersthrows IOException {
             Map pathArgs=new HashMap();
             pathArgs.put("requestPayment"null);
             return new GetResponse(makeRequest("GET"bucket""pathArgsheaders));
         }

        
Write a new requestPayment xml document for a given bucket

Parameters:
bucket The name of the bucket
requestPaymentXMLDoc
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response putBucketRequestPayment(String bucketString requestPaymentXMLDocMap headers)
                 throws IOException {
             Map pathArgs=new HashMap();
             pathArgs.put("requestPayment"null);
             S3Object object=new S3Object(requestPaymentXMLDoc.getBytes(), null);
             HttpURLConnection request=makeRequest("PUT"bucket""pathArgsheadersobject);
 
             request.setDoOutput(true);
             request.getOutputStream().write(object.data == nullnew byte[]{} : object.data);
 
             return new Response(request);
         }

        
Get the logging xml document for a given bucket

Parameters:
bucket The name of the bucket
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public GetResponse getBucketLogging(String bucketMap headersthrows IOException {
             Map pathArgs=new HashMap();
             pathArgs.put("logging"null);
             return new GetResponse(makeRequest("GET"bucket""pathArgsheaders));
         }

        
Write a new logging xml document for a given bucket

Parameters:
loggingXMLDoc The xml representation of the logging configuration as a String
bucket The name of the bucket
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response putBucketLogging(String bucketString loggingXMLDocMap headersthrows IOException {
             Map pathArgs=new HashMap();
             pathArgs.put("logging"null);
             S3Object object=new S3Object(loggingXMLDoc.getBytes(), null);
             HttpURLConnection request=makeRequest("PUT"bucket""pathArgsheadersobject);
 
             request.setDoOutput(true);
             request.getOutputStream().write(object.data == nullnew byte[]{} : object.data);
 
             return new Response(request);
         }

        
Get the ACL for a given bucket

Parameters:
bucket The name of the bucket where the object lives.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public GetResponse getBucketACL(String bucketMap headersthrows IOException {
             return getACL(bucket""headers);
         }

        
Get the ACL for a given object (or bucket, if key is null).

Parameters:
bucket The name of the bucket where the object lives.
key The name of the key to use.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public GetResponse getACL(String bucketString keyMap headersthrows IOException {
             if(key == nullkey="";
 
             Map pathArgs=new HashMap();
             pathArgs.put("acl"null);
 
             return new GetResponse(
                     makeRequest("GET"bucket, Utils.urlencode(key), pathArgsheaders)
             );
         }

        
Write a new ACL for a given bucket

Parameters:
aclXMLDoc The xml representation of the ACL as a String
bucket The name of the bucket where the object lives.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response putBucketACL(String bucketString aclXMLDocMap headersthrows IOException {
             return putACL(bucket""aclXMLDocheaders);
         }

        
Write a new ACL for a given object

Parameters:
aclXMLDoc The xml representation of the ACL as a String
bucket The name of the bucket where the object lives.
key The name of the key to use.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public Response putACL(String bucketString keyString aclXMLDocMap headers)
                 throws IOException {
             S3Object object=new S3Object(aclXMLDoc.getBytes(), null);
 
             Map pathArgs=new HashMap();
             pathArgs.put("acl"null);
 
             HttpURLConnection request=
                     makeRequest("PUT"bucket, Utils.urlencode(key), pathArgsheadersobject);
 
             request.setDoOutput(true);
             request.getOutputStream().write(object.data == nullnew byte[]{} : object.data);
 
             return new Response(request);
         }
 
         public LocationResponse getBucketLocation(String bucket)
                 throws IOException {
             Map pathArgs=new HashMap();
             pathArgs.put("location"null);
             return new LocationResponse(makeRequest("GET"bucket""pathArgsnull));
         }


        
List all the buckets created by this account.

Parameters:
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         public ListAllMyBucketsResponse listAllMyBuckets(Map headers)
                 throws IOException {
             return new ListAllMyBucketsResponse(makeRequest("GET"""""nullheaders));
         }


        
Make a new HttpURLConnection without passing an S3Object parameter. Use this method for key operations that do require arguments

Parameters:
method The method to invoke
bucketName the bucket this request is for
key the key this request is for
pathArgs the
headers
Returns:
Throws:
java.net.MalformedURLException
java.io.IOException
 
         private HttpURLConnection makeRequest(String methodString bucketNameString keyMap pathArgsMap headers)
                 throws IOException {
             return makeRequest(methodbucketNamekeypathArgsheadersnull);
         }


        
Make a new HttpURLConnection.

Parameters:
method The HTTP method to use (GET, PUT, DELETE)
bucket The bucket name this request affects
key The key this request is for
pathArgs parameters if any to be sent along this request
headers A Map of String to List of Strings representing the http headers to pass (can be null).
object The S3Object that is to be written (can be null).
 
         private HttpURLConnection makeRequest(String methodString bucketString keyMap pathArgsMap headers,
                                               S3Object object)
                 throws IOException {
             CallingFormat format=Utils.getCallingFormatForBucket(this.bucket);
             if( && format != CallingFormat.getPathCallingFormat() && bucket.contains(".")) {
                 ..println("You are making an SSL connection, however, the bucket contains periods and the wildcard certificate will not match by default.  Please consider using HTTP.");
             }
 
             // build the domain based on the calling format
             URL url=format.getURL(this.bucketkeypathArgs);
 
             HttpURLConnection connection=(HttpURLConnection)url.openConnection();
             connection.setRequestMethod(method);
 
             // subdomain-style urls may encounter http redirects.
             // Ensure that redirects are supported.
             if(!connection.getInstanceFollowRedirects()
                     && format.supportsLocatedBuckets())
                 throw .httpRedirectSupportRequired();
 
             addHeaders(connectionheaders);
             if(object != nulladdMetadataHeaders(connectionobject.metadata);
             addAuthHeader(connectionmethodbucketkeypathArgs);
 
             return connection;
         }
 
         private HttpURLConnection makePreSignedRequest(String methodString preSignedUrlMap headersthrows IOException {
             URL url = new URL(preSignedUrl);
             HttpURLConnection connection = (HttpURLConnectionurl.openConnection();
             connection.setRequestMethod(method);
 
             addHeaders(connectionheaders);
 
             return connection;
         }

        
Add the given headers to the HttpURLConnection.

Parameters:
connection The HttpURLConnection to which the headers will be added.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
 
         private static void addHeaders(HttpURLConnection connectionMap headers) {
             addHeaders(connectionheaders"");
         }

        
Add the given metadata fields to the HttpURLConnection.

Parameters:
connection The HttpURLConnection to which the headers will be added.
metadata A Map of String to List of Strings representing the s3 metadata for this resource.
 
         private static void addMetadataHeaders(HttpURLConnection connectionMap metadata) {
             addHeaders(connectionmetadata.);
         }

        
Add the given headers to the HttpURLConnection with a prefix before the keys.

Parameters:
connection The HttpURLConnection to which the headers will be added.
headers A Map of String to List of Strings representing the http headers to pass (can be null).
prefix The string to prepend to each key before adding it to the connection.
 
         private static void addHeaders(HttpURLConnection connectionMap headersString prefix) {
             if(headers != null) {
                 for(Iterator i=headers.keySet().iterator(); i.hasNext();) {
                     String key=(String)i.next();
                     for(Iterator j=((List)headers.get(key)).iterator(); j.hasNext();) {
                         String value=(String)j.next();
                         connection.addRequestProperty(prefix + keyvalue);
                     }
                 }
             }
         }

        
Add the appropriate Authorization header to the HttpURLConnection.

Parameters:
connection The HttpURLConnection to which the header will be added.
method The HTTP method to use (GET, PUT, DELETE)
bucket the bucket name this request is for
key the key this request is for
pathArgs path arguments which are part of this request
 
         private void addAuthHeader(HttpURLConnection connectionString methodString bucketString keyMap pathArgs) {
             if(connection.getRequestProperty("Date") == null) {
                 connection.setRequestProperty("Date"httpDate());
             }
             if(connection.getRequestProperty("Content-Type") == null) {
                 connection.setRequestProperty("Content-Type""");
             }
 
             if(this. != null && this. != null) {
                 String canonicalString=
                         Utils.makeCanonicalString(methodbucketkeypathArgsconnection.getRequestProperties());
                 String encodedCanonical=Utils.encode(this.canonicalStringfalse);
                 connection.setRequestProperty("Authorization",
                                               "AWS " + this. + ":" + encodedCanonical);
             }
         }


        
Generate an rfc822 date for use in the Date HTTP header.
 
         public static String httpDate() {
             final String DateFormat="EEE, dd MMM yyyy HH:mm:ss ";
             SimpleDateFormat format=new SimpleDateFormat(DateFormat.);
             format.setTimeZone(TimeZone.getTimeZone("GMT"));
             return format.format(new Date()) + "GMT";
         }
     }
 
     static class ListEntry {
        
The name of the object
 
         public String key;

        
The date at which the object was last modified.
 
         public Date lastModified;

        
The object's ETag, which can be used for conditional GETs.
 
         public String eTag;

        
The size of the object in bytes.
 
         public long size;

        
The object's storage class
 
         public String storageClass;

        
The object's owner
 
         public Owner owner;
 
         public String toString() {
             return ;
         }
     }
 
     static class Owner {
         public String id;
         public String displayName;
     }
 
 
     static class Response {
         public HttpURLConnection connection;
 
         public Response(HttpURLConnection connectionthrows IOException {
             this.=connection;
         }
     }
 
 
     static class GetResponse extends Response {
         public S3Object object;

        
Pulls a representation of an S3Object out of the HttpURLConnection response.
 
         public GetResponse(HttpURLConnection connectionthrows IOException {
             super(connection);
             if(connection.getResponseCode() < 400) {
                 Map metadata=extractMetadata(connection);
                 byte[] body=slurpInputStream(connection.getInputStream());
                 this.=new S3Object(bodymetadata);
             }
         }

        
Examines the response's header fields and returns a Map from String to List of Strings representing the object's metadata.
 
         private static Map extractMetadata(HttpURLConnection connection) {
             TreeMap metadata=new TreeMap();
             Map headers=connection.getHeaderFields();
             for(Iterator i=headers.keySet().iterator(); i.hasNext();) {
                 String key=(String)i.next();
                 if(key == nullcontinue;
                 if(key.startsWith(.)) {
                     metadata.put(key.substring(..length()), headers.get(key));
                 }
             }
 
             return metadata;
         }

        
Read the input stream and dump it all into a big byte array
 
         static byte[] slurpInputStream(InputStream streamthrows IOException {
             final int chunkSize=2048;
             byte[] buf=new byte[chunkSize];
             ByteArrayOutputStream byteStream=new ByteArrayOutputStream(chunkSize);
             int count;
 
             while((count=stream.read(buf)) != -1) byteStream.write(buf, 0, count);
 
             return byteStream.toByteArray();
         }
     }
 
     static class LocationResponse extends Response {
         String location;

        
Parse the response to a ?location query.
 
         public LocationResponse(HttpURLConnection connectionthrows IOException {
             super(connection);
             if(connection.getResponseCode() < 400) {
                 try {
                     XMLReader xr=Utils.createXMLReader();
                     LocationResponseHandler handler=new LocationResponseHandler();
                     xr.setContentHandler(handler);
                     xr.setErrorHandler(handler);
 
                     xr.parse(new InputSource(connection.getInputStream()));
                     this.=handler.loc;
                 }
                 catch(SAXException e) {
                     throw .errorParsingBucketListings(e);
                 }
             }
             else {
                 this.="<error>";
             }
         }

        
Report the location-constraint for a bucket. A value of null indicates an error; the empty string indicates no constraint; and any other value is an actual location constraint value.
 
         public String getLocation() {
             return ;
         }

        
Helper class to parse LocationConstraint response XML
 
         static class LocationResponseHandler extends DefaultHandler {
             String loc=null;
             private StringBuffer currText=null;
 
             public void startDocument() {
             }
 
             public void startElement(String uriString nameString qNameAttributes attrs) {
                 if(name.equals("LocationConstraint")) {
                     this.=new StringBuffer();
                 }
             }
 
             public void endElement(String uriString nameString qName) {
                 if(name.equals("LocationConstraint")) {
                     =this..toString();
                     this.=null;
                 }
             }
 
             public void characters(char[] chint startint length) {
                 if( != null)
                     this..append(chstartlength);
             }
         }
     }
 
 
     static class Bucket {
        
The name of the bucket.
 
         public String name;

        
The bucket's creation date.
 
         public Date creationDate;
 
         public Bucket() {
             this.=null;
             this.=null;
         }
 
         public Bucket(String nameDate creationDate) {
             this.=name;
             this.=creationDate;
         }
 
         public String toString() {
             return this.;
         }
     }
 
     static class ListBucketResponse extends Response {

        
The name of the bucket being listed. Null if request fails.
 
         public String name=null;

        
The prefix echoed back from the request. Null if request fails.
 
         public String prefix=null;

        
The marker echoed back from the request. Null if request fails.
 
         public String marker=null;

        
The delimiter echoed back from the request. Null if not specified in the request, or if it fails.
        public String delimiter=null;

        
The maxKeys echoed back from the request if specified. 0 if request fails.
        public int maxKeys=0;

        
Indicates if there are more results to the list. True if the current list results have been truncated. false if request fails.
        public boolean isTruncated=false;

        
Indicates what to use as a marker for subsequent list requests in the event that the results are truncated. Present only when a delimiter is specified. Null if request fails.
        public String nextMarker=null;

        
A List of ListEntry objects representing the objects in the given bucket. Null if the request fails.
        public List entries=null;

        
A List of CommonPrefixEntry objects representing the common prefixes of the keys that matched up to the delimiter. Null if the request fails.
        public List commonPrefixEntries=null;
        public ListBucketResponse(HttpURLConnection connectionthrows IOException {
            super(connection);
            if(connection.getResponseCode() < 400) {
                try {
                    XMLReader xr=Utils.createXMLReader();
                    ListBucketHandler handler=new ListBucketHandler();
                    xr.setContentHandler(handler);
                    xr.setErrorHandler(handler);
                    xr.parse(new InputSource(connection.getInputStream()));
                    this.=handler.getName();
                    this.=handler.getPrefix();
                    this.=handler.getMarker();
                    this.=handler.getDelimiter();
                    this.=handler.getMaxKeys();
                    this.=handler.getIsTruncated();
                    this.=handler.getNextMarker();
                    this.=handler.getKeyEntries();
                    this.=handler.getCommonPrefixEntries();
                }
                catch(SAXException e) {
                    throw .errorParsingBucketListings(e);
                }
            }
        }
        static class ListBucketHandler extends DefaultHandler {
            private String name=null;
            private String prefix=null;
            private String marker=null;
            private String delimiter=null;
            private int maxKeys=0;
            private boolean isTruncated=false;
            private String nextMarker=null;
            private boolean isEchoedPrefix=false;
            private List keyEntries=null;
            private ListEntry keyEntry=null;
            private List commonPrefixEntries=null;
            private CommonPrefixEntry commonPrefixEntry=null;
            private StringBuffer currText=null;
            private SimpleDateFormat iso8601Parser=null;
            public ListBucketHandler() {
                super();
                =new ArrayList();
                =new ArrayList();
                this.=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                this..setTimeZone(new SimpleTimeZone(0, "GMT"));
                this.=new StringBuffer();
            }
            public void startDocument() {
                this.=true;
            }
            public void endDocument() {
                // ignore
            }
            public void startElement(String uriString nameString qNameAttributes attrs) {
                if(name.equals("Contents")) {
                    this.=new ListEntry();
                }
                else if(name.equals("Owner")) {
                    this..=new Owner();
                }
                else if(name.equals("CommonPrefixes")) {
                    this.=new CommonPrefixEntry();
                }
            }
            public void endElement(String uriString nameString qName) {
                if(name.equals("Name")) {
                    this.=this..toString();
                }
                // this prefix is the one we echo back from the request
                else if(name.equals("Prefix") && this.) {
                    this.=this..toString();
                    this.=false;
                }
                else if(name.equals("Marker")) {
                    this.=this..toString();
                }
                else if(name.equals("MaxKeys")) {
                    this.=Integer.parseInt(this..toString());
                }
                else if(name.equals("Delimiter")) {
                    this.=this..toString();
                }
                else if(name.equals("IsTruncated")) {
                    this.=Boolean.valueOf(this..toString());
                }
                else if(name.equals("NextMarker")) {
                    this.=this..toString();
                }
                else if(name.equals("Contents")) {
                    this..add(this.);
                }
                else if(name.equals("Key")) {
                    this..=this..toString();
                }
                else if(name.equals("LastModified")) {
                    try {
                        this..=this..parse(this..toString());
                    }
                    catch(ParseException e) {
                        throw .errorParsingBucketListings(e);
                    }
                }
                else if(name.equals("ETag")) {
                    this..=this..toString();
                }
                else if(name.equals("Size")) {
                    this..=Long.parseLong(this..toString());
                }
                else if(name.equals("StorageClass")) {
                    this..=this..toString();
                }
                else if(name.equals("ID")) {
                    this...=this..toString();
                }
                else if(name.equals("DisplayName")) {
                    this...=this..toString();
                }
                else if(name.equals("CommonPrefixes")) {
                    this..add(this.);
                }
                // this is the common prefix for keys that match up to the delimiter
                else if(name.equals("Prefix")) {
                    this..=this..toString();
                }
                if(this..length() != 0)
                    this.=new StringBuffer();
            }
            public void characters(char[] chint startint length) {
                this..append(chstartlength);
            }
            public String getName() {
                return this.;
            }
            public String getPrefix() {
                return this.;
            }
            public String getMarker() {
                return this.;
            }
            public String getDelimiter() {
                return this.;
            }
            public int getMaxKeys() {
                return this.;
            }
            public boolean getIsTruncated() {
                return this.;
            }
            public String getNextMarker() {
                return this.;
            }
            public List getKeyEntries() {
                return this.;
            }
            public List getCommonPrefixEntries() {
                return this.;
            }
        }
    }
    static class CommonPrefixEntry {
        
The prefix common to the delimited keys it represents
        public String prefix;
    }
    static class ListAllMyBucketsResponse extends Response {
        
A list of Bucket objects, one for each of this account's buckets. Will be null if the request fails.
        public List entries;
        public ListAllMyBucketsResponse(HttpURLConnection connectionthrows IOException {
            super(connection);
            if(connection.getResponseCode() < 400) {
                try {
                    XMLReader xr=Utils.createXMLReader();
                    ListAllMyBucketsHandler handler=new ListAllMyBucketsHandler();
                    xr.setContentHandler(handler);
                    xr.setErrorHandler(handler);
                    xr.parse(new InputSource(connection.getInputStream()));
                    this.=handler.getEntries();
                }
                catch(SAXException e) {
                    throw .errorParsingBucketListings(e);
                }
            }
        }
        static class ListAllMyBucketsHandler extends DefaultHandler {
            private List entries=null;
            private Bucket currBucket=null;
            private StringBuffer currText=null;
            private SimpleDateFormat iso8601Parser=null;
            public ListAllMyBucketsHandler() {
                super();
                =new ArrayList();
                this.=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                this..setTimeZone(new SimpleTimeZone(0, "GMT"));
                this.=new StringBuffer();
            }
            public void startDocument() {
                // ignore
            }
            public void endDocument() {
                // ignore
            }
            public void startElement(String uriString nameString qNameAttributes attrs) {
                if(name.equals("Bucket")) {
                    this.=new Bucket();
                }
            }
            public void endElement(String uriString nameString qName) {
                if(name.equals("Bucket")) {
                    this..add(this.);
                }
                else if(name.equals("Name")) {
                    this..=this..toString();
                }
                else if(name.equals("CreationDate")) {
                    try {
                        this..=this..parse(this..toString());