Java to Python

zaheer.agadi at gmail.com zaheer.agadi at gmail.com
Sat Feb 7 17:24:41 CET 2009


Hi

I have a following class that is written Java and makes use of apache
http client library,I am new to python can any one suggest me a python
equivalent of this following class,

Thanks ,

public class Authenticate{

 private String storageUserName=null;
    private String storagePassword=null;
    private String authorization=null;
    private String identityHostName = null;
    private String identityPortNumber = null;

    private String accessKey=null;
    private String secretKey=null;

    public String getStoragePassword() {
        return storagePassword;
    }

    public void setStoragePassword(String storagePassword) {
        this.storagePassword = storagePassword;
    }

    public String getStorageUserName() {
        return storageUserName;
    }

    public void setStorageUserName(String storageUserName) {
        this.storageUserName = storageUserName;
    }

    public String getIdentityHostName() {
        return identityHostName;
    }

    public void setIdentityHostName(String identityHostName) {
        this.identityHostName = identityHostName;
    }

    public String getIdentityPortNumber() {
        return identityPortNumber;
    }

    public void setIdentityPortNumber(String identityPortNumber) {
        this.identityPortNumber = identityPortNumber;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }


     /**
     * <p>Convenience string for Base 64 encoding.</p>
     */
    private static final String BASE64_CHARS =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
            "abcdefghijklmnopqrstuvwxyz" +
            "0123456789+/";

    /**
     * <p>Encode the specified credentials into a String as required
by
     * HTTP Basic Authentication (<a href="http://www.ietf.org/rfc/
rfc2617.txt">RFC 2617</a>).</p>
     *
     * @param username Username to be encoded
     * @param password Password to be encoded
     * @return String string containing encoded username and password.
     */
    public String encodeCredentialsBasic(String username, String
password) {
        String encode = username + ":" + password;
        int paddingCount = (3 - (encode.length() % 3)) % 3;
        encode += "\0\0".substring(0, paddingCount);
        StringBuilder encoded = new StringBuilder();

        for (int i = 0; i < encode.length(); i += 3) {
        }
        return encoded.toString();
    }

    public void fetchDetails(){
        HttpClient client=new HttpClient();
        //reqDetails = new RequestDetails();
        //String identityURL=MessageUtil.getMessage
("IDENTITY_INSTANCE");
        //int portNumber=Integer.parseInt(MessageUtil.getMessage
("IDENTITY_PORT"));
        authorization="Basic " + encodeCredentialsBasic
(storageUserName, storagePassword);
        String url="https://"+identityHostName+
                ":"+identityPortNumber+"/test/ndcsd2/persons/"+UserName
+"/attributes/";

        Protocol https=null;
        //try {
            https = new Protocol("https", new
EasySSLProtocolSocketFactory(), Integer.parseInt(identityPortNumber));
        /*} catch (GeneralSecurityException ex) {
            Logger.getLogger(Authenticate.class.getName()).log
(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Authenticate.class.getName()).log
(Level.SEVERE, null, ex);
        }*/
        Protocol.registerProtocol("https", https);
        GetMethod method=new GetMethod(url);
        method.setRequestHeader("Authorization",authorization);
        method.setRequestHeader("Accept","application/xml");
        try {
            int responseCode=client.executeMethod(method);
            if(responseCode==200){
                InputStream is=method.getResponseBodyAsStream();
                BufferedReader bis=new BufferedReader(new
InputStreamReader(is));
                String temp=null,sKey=null, aKey=null;
                String accessKeySearchString="AccessKey</
Name><Value>";
                String secretKeySearchString="SecretKey</
Name><Value>";
                int searchStringLength=0;
                while((temp=bis.readLine())!=null){
                    if(temp.indexOf(accessKeySearchString)!=-1){
                        int beginIndex=temp.indexOf
(accessKeySearchString);
                        searchStringLength=accessKeySearchString.length
();
                        int endIndex=temp.indexOf("</
Value>",beginIndex);
                        aKey=temp.substring(beginIndex
+searchStringLength,endIndex);
                    }
                    if(temp.indexOf(secretKeySearchString)!=-1){
                        int beginIndex=temp.indexOf
(secretKeySearchString);
                        searchStringLength=secretKeySearchString.length
();
                        int endIndex=temp.indexOf("</
Value>",beginIndex);
                        sKey=temp.substring(beginIndex
+searchStringLength,endIndex);
                    }
                }
                setSecretKey(sKey);
                setAccessKey(aKey);
            } else {
                System.out.println("Not able to get the credentials.
Returned : " + responseCode + " response code!!!");
            }
        } catch (IOException ex) {
            Logger.getLogger(Authenticate.class.getName()).log
(Level.SEVERE, null, ex);
        }
    }



More information about the Python-list mailing list