package com.ning.http.client.providers.grizzly;

import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.GSSName;
import org.ietf.jgss.Oid;

import com.ning.http.util.Base64;

public class GSSSPNEGOWrapper {

	private static final String KERBEROS_OID = "1.2.840.113554.1.2.2";
	
	static GSSManager getManager() {
        return GSSManager.getInstance();
    }
	
	static  byte[] generateGSSToken(
            final byte[] input, final Oid oid, final String authServer) throws GSSException {
        byte[] token = input;
        if (token == null) {
            token = new byte[0];
        }
        GSSManager manager = getManager();
        GSSName serverName = manager.createName("HTTP@" + authServer, GSSName.NT_HOSTBASED_SERVICE);
        GSSContext gssContext = manager.createContext(
                serverName.canonicalize(oid), oid, null, GSSContext.DEFAULT_LIFETIME);
        gssContext.requestMutualAuth(true);
        gssContext.requestCredDeleg(true);
        return gssContext.initSecContext(token, 0, token.length);
    }
	
	public  static String generateToken(String authServer)
	{
		String  returnVal = "";
		Oid oid;
		try {
			oid = new Oid(KERBEROS_OID);
			byte[] token = GSSSPNEGOWrapper.generateGSSToken(null, oid, authServer);
			returnVal = new String(Base64.encode(token));
		} catch (GSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return returnVal;
	}
}

@@ -233,16 +233,48 @@
 .Base64;
+%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -299,18 +299,120 @@
 apper %7B%0A
-%0A%09
+    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(GSSSPNEGOWrapper.class);%0A    
 private 
@@ -470,19 +470,21 @@
 1.2.2%22;%0A
-%09%0A%09
+%0A    
 static G
@@ -559,19 +559,20 @@
   %7D%0A
-%09%0A%09
+%0A    
 static 
- 
 byte
@@ -1237,19 +1237,20 @@
   %7D%0A
-%09%0A%09
+%0A    
 public 
- 
 stat
@@ -1295,22 +1295,26 @@
 ver)
-%0A%09%7B%0A%09%09
+ %7B%0A        
 String 
- 
 retu
@@ -1325,18 +1325,24 @@
 l = %22%22;%0A
-%09%09
+        
 Oid oid;
@@ -1346,19 +1346,34 @@
 id;%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 oid 
@@ -1397,19 +1397,28 @@
 S_OID);%0A
-%09%09%09
+            
 byte%5B%5D t
@@ -1482,19 +1482,28 @@
 erver);%0A
-%09%09%09
+            
 returnVa
@@ -1510,19 +1510,8 @@
 l = 
-new String(
 Base
@@ -1526,21 +1526,26 @@
 e(token)
-)
 ;%0A
-%09%09
+        
 %7D catch 
@@ -1567,79 +1567,69 @@
 ) %7B%0A
-%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09
+            LOGGER.warn(e.toString(), e);%0A        %7D%0A%0A        
 retu
@@ -1646,9 +1646,12 @@
 al;%0A
-%09
+    
 %7D%0A%7D%0A

Add logging.