/**
 * Name : Jswift.java
 * 
 * Author: Masanori Itoh <masanori.itoh@gmail.com>
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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.github.thatsdone.jopst;

import com.woorea.openstack.keystone.Keystone;
import com.woorea.openstack.keystone.model.Access;
import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
import com.woorea.openstack.keystone.utils.KeystoneUtils;

import com.woorea.openstack.swift.Swift;
import com.woorea.openstack.swift.model.Account;
import com.woorea.openstack.swift.model.Container;
import com.woorea.openstack.swift.model.Object;
import com.woorea.openstack.swift.model.ObjectDownload;
import com.woorea.openstack.swift.model.ObjectForUpload;

import java.lang.System;
import java.util.List;

import com.github.thatsdone.jopst.Jopst;
import com.github.thatsdone.jopst.Utils;

import org.codehaus.jackson.type.TypeReference;
import org.codehaus.jackson.map.ObjectMapper;

public class Jswift {

    private static Jopst jopst;
    private static Utils util;

    public static void swift(String[] args) {
        if(jopst.isDebug()) {
            System.out.println("token() called."); 
        }

        String command = args[0];

            Keystone keystoneClient = new Keystone(jopst.getOsAuthUrl());

            // Set account information, and issue an authentication request.
            Access access = keystoneClient.tokens()
                .authenticate(new UsernamePassword(jopst.getOsUsername(),
                                                   jopst.getOsPassword()))
                .withTenantName(jopst.getOsTenantName())
                .execute();

            String swiftEndpoint = KeystoneUtils
                .findEndpointURL(access.getServiceCatalog(),
                                 "object-store", null, "public");
            if (jopst.isDebug()) {
                System.out.println("DEBUG: " + swiftEndpoint);
            }
            // Create a Nova client object.
            Swift swiftClient = new Swift(swiftEndpoint);
            swiftClient.token(access.getToken().getId());

        if (command.equals("list")) {
            /*
            Container container;
            container = swiftClient.containers().show(args[1]).execute();
            */
            /*
             * FIXME(thatsdone):
             * A POC implementation using a work around in
             * openstack-java-sdk layer which returns a String, not
             * a List<Container>. The below does deserialization by itself.
             */
            String container = swiftClient.containers().list().execute();
            //System.out.println("result: " + container);
            //util.printJson(container);
            try {
                List<Container>containerObj =
                    new ObjectMapper()
                    .readValue(container,
                               new TypeReference<List<Container>>(){});
                // Note that the below dows not work.
                //              .readValue(container, List<Container>);
                util.printJson(containerObj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -1876,21 +1876,21 @@
 rintln(%22
-token
+swift
 () calle
@@ -1950,20 +1950,16 @@
         
-    
 Keystone
@@ -2021,20 +2021,16 @@
         
-   
- 
 // Set a
@@ -2094,20 +2094,16 @@
         
-    
 Access a
@@ -2146,20 +2146,16 @@
         
- 
-   
 .authent
@@ -2291,20 +2291,16 @@
         
-    
 .withTen
@@ -2344,20 +2344,16 @@
         
-    
 .execute
@@ -2361,28 +2361,24 @@
 );%0A%0A        
- 
-   
 String swift
@@ -2402,20 +2402,16 @@
 neUtils%0A
-    
         
@@ -2529,28 +2529,24 @@
 %22);%0A        
-    
 if (jopst.is
@@ -2560,36 +2560,32 @@
 ) %7B%0A            
-  
-  
 System.out.print
@@ -2619,34 +2619,26 @@
 t);%0A        
-    %7D%0A    
+%7D%0A
         // C
@@ -2673,20 +2673,16 @@
         
-  
-  
 Swift sw
@@ -2715,28 +2715,24 @@
 tEndpoint);%0A
-    
         swif
@@ -2843,557 +2843,168 @@
     
-Container container;%0A            container = swiftClient.containers().show(args%5B1%5D).execute();%0A            */%0A            /*%0A             * FIXME(thatsdone):%0A             * A POC implementation using a work around in%0A             * openstack-java-sdk layer which returns a String, not%0A             * a List%3CContainer%3E. The below does deserialization by itself.%0A             */%0A            String container = swiftClient.containers().list().execute();%0A            //System.out.println(%22result: %22 + container);%0A            //util.printJson(container);
+ * NOTE(thatsdone):%0A             * The below assumes a special version of Swift client class%0A             * which returns List%3CContainer%3E class.%0A             */
 %0A   
@@ -3049,16 +3049,17 @@
 ntainer%3E
+ 
 containe
@@ -3063,290 +3063,102 @@
 iner
-Obj =%0A                    new ObjectMapper()%0A                    .readValue(container,%0A                               new TypeReference%3CList%3CContainer%3E%3E()%7B%7D);%0A                // Note that the below dows not work.%0A                //              .readValue(container, List%3CContainer%3E
+s = swiftClient.containers()%0A                    .list().queryParam(%22format%22, %22json%22).execute(
 );%0A 
@@ -3196,19 +3196,17 @@
 ontainer
-Obj
+s
 );%0A     

Update Jswift::swift() to work with a newer version of openstack-java-sdk.