/*
 * Copyright 2013 EMC Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.emc.vipr.s3;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.List;

import com.emc.vipr.services.s3.ViPRS3Client;

import org.apache.commons.codec.binary.Base64;

import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.util.StringInputStream;

public class s3api {

// Create Client Instance
    public static ViPRS3Client getS3Client(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE ) {
        BasicAWSCredentials creds = new BasicAWSCredentials(S3_ACCESS_KEY_ID, S3_SECRET_KEY);
		ViPRS3Client client = new ViPRS3Client(S3_ENDPOINT, creds);

		if (S3_ViPR_NAMESPACE != null) {
		    client.setNamespace(S3_ViPR_NAMESPACE);
		}

		return client;
    }

/********************************* Bucket Operations******************************/
    
    
	public static void CreateBucket(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET ) throws Exception {
    	// create the ViPR S3 Client
    	ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);

    	// create the bucket - used for subsequent demo operations
        s3.createBucket(S3_BUCKET);

        
    }

    
	public static  ObjectListing ReadBucket(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET ) throws Exception {
    	// create the ViPR S3 Client
    	ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        
        // get bucket listing to retrieve the bucket name
        return s3.listObjects(S3_BUCKET);

	}
    
	public static  List<Bucket> ListBuckets(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE) throws Exception {
    	// create the ViPR S3 Client
    	ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        
        // get bucket listing to retrieve the bucket name
        return s3.listBuckets();

	}
    
	public static  void  DeleteBuckets(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET ) throws Exception {
    	// create the ViPR S3 Client
    	ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        
        // get bucket listing to retrieve the bucket name
        s3.deleteBucket(S3_BUCKET);

	}
    

	/********************************* Object Operations******************************/				
	public static void CreateObject(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET,String key,InputStream content ) throws Exception {

		ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        // create the object in the demo bucket
        s3.putObject(S3_BUCKET, key, content, null);


    }
    
	public static void UpdateObject(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET,String key,InputStream content ) throws Exception {

		ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        // create the object in the demo bucket
        s3.putObject(S3_BUCKET, key, content, null);


    }
	
	public static void DeleteObject(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET,String key) throws Exception {

		ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        // create the object in the demo bucket
        s3.deleteObject(S3_BUCKET, key);


    }
	
	public static S3ObjectInputStream ReadObject(String S3_ACCESS_KEY_ID,String S3_SECRET_KEY,String S3_ENDPOINT,String S3_ViPR_NAMESPACE, String S3_BUCKET,String key) throws Exception {

		ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID,S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
        // create the object in the demo bucket
		 S3Object object =s3.getObject(S3_BUCKET, key);
        
       return object.getObjectContent();

    }
		
		
    
		
    
}

@@ -865,56 +865,170 @@
 nt;%0A
-%0Aimport org.apache.commons.codec.binary.Base64;%0A
+import com.netflix.servo.examples.EchoServerExample.ClientTask;%0A%0Aimport org.apache.commons.codec.binary.Base64;%0A%0Aimport com.amazonaws.services.s3.S3ClientOptions;
 %0Aimp
@@ -1605,16 +1605,107 @@
 T_KEY);%0A
+        S3ClientOptions opt = new S3ClientOptions();%0A        opt.setPathStyleAccess(true);%0A
 %09%09ViPRS3
@@ -1758,16 +1758,52 @@
 creds);%0A
+%09%09%0A%09%09client.setS3ClientOptions(opt);
 %0A%09%09if (S

fix bucket style access for s3api