All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.amazon.s3.LocationResponse Maven / Gradle / Ivy

//  This software code is made available "AS IS" without warranties of any
//  kind.  You may copy, display, modify and redistribute the software
//  code either by itself or as incorporated into your code; provided that
//  you do not remove any proprietary notices.  Your use of this software
//  code is at your own risk and you waive any claim against Amazon
//  Digital Services, Inc. or its affiliates with respect to your use of
//  this software code. (c) 2006-2007 Amazon Digital Services, Inc. or its
//  affiliates.

package com.amazon.s3;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
 * A Response object returned from AWSAuthConnection.getBucketLocation().
 * Parses the response XML and exposes the location constraint
 * via the geteLocation() method.
 */
public class LocationResponse extends Response {
    String location;
    
    /**
     * Parse the response to a ?location query.
     */
    public LocationResponse(HttpURLConnection connection) throws 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.location = handler.location;
            } catch (SAXException e) {
                throw new RuntimeException("Unexpected error parsing ListAllMyBuckets xml", e);
            }
        } else {
            this.location = "";
        }
    }

    /**
     * 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 location;
    }

    /**
     * Helper class to parse LocationConstraint response XML
     */
    static class LocationResponseHandler extends DefaultHandler {
        String location = null;
        private StringBuffer currText = null;
        
        public void startDocument() {
        }

        public void startElement(String uri, String name, String qName, Attributes attrs) {
            if (name.equals("LocationConstraint")) {
                this.currText = new StringBuffer();
            }
        }

        public void endElement(String uri, String name, String qName) {
            if (name.equals("LocationConstraint")) {
                location = this.currText.toString();
                this.currText = null;
            }
        }
        
        public void characters(char ch[], int start, int length) {
            if (currText != null)
                this.currText.append(ch, start, length);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy