package org.jboss.pnc.auth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class SimpleOAuthConnect {
    
    public static String getAccessToken(String url, String clientId, String username, String password) 
            throws ClientProtocolException, IOException{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // add header
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        List <BasicNameValuePair> urlParameters = new ArrayList <BasicNameValuePair>();
        urlParameters.add(new BasicNameValuePair("username", username));
        urlParameters.add(new BasicNameValuePair("password", password));
        urlParameters.add(new BasicNameValuePair("client_id", clientId));
        httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
        CloseableHttpResponse response = httpclient.execute(httpPost);

        try {
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                if(line.contains("refresh_token")) {
                    String[] respContent = line.split(",");
                    for (int i = 0; i < respContent.length; i++) {
                        String split = respContent[i];
                        if(split.contains("refresh_token")) {
                            String refreshToken = split.split(":")[1].substring(1,split.split(":")[1].length() -1);
                        }
                        if(split.contains("access_token")) {
                            String accessToken = split.split(":")[1].substring(1,split.split(":")[1].length() -1);
                            return accessToken;
                        }
                    }
                }
            }

        } finally {
            response.close();
        }
        return null;

    }
    
    public static void main(String[] args) {
        try {
            System.out.println(">>> Access token: " + SimpleOAuthConnect
                    .getAccessToken(args[0], args[1], args[2], args[3]));
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

@@ -160,20 +160,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -632,173 +632,1575 @@
 t %7B%0A
-    %0A    public static String getAccessToken(String url, String clientId, String username, String password) %0A            throws ClientProtocolException, IOException%7B
+%0A    public static String getAccessToken(String url, Map%3CString, String%3E urlParams) %0A            throws ClientProtocolException, IOException%7B%0A        return connect(url, urlParams)%5B0%5D;%0A    %7D%0A%0A    public static String getRefreshToken(String url, Map%3CString, String%3E urlParams) %0A            throws ClientProtocolException, IOException%7B%0A        return connect(url, urlParams)%5B1%5D;%0A    %7D%0A    %0A    public static String%5B%5D getTokens(String url, Map%3CString, String%3E urlParams) %0A            throws ClientProtocolException, IOException%7B%0A        return connect(url, urlParams);%0A    %7D%0A    %0A    public static String getAccessToken(String url, String clientId, String username, String password) %0A            throws ClientProtocolException, IOException%7B%0A        Map%3CString, String%3E urlParams = new HashMap%3CString, String%3E();%0A        urlParams.put(%22client_id%22, clientId);%0A        urlParams.put(%22username%22, username);%0A        urlParams.put(%22password%22, password);%0A        return connect(url, urlParams)%5B0%5D;%0A    %7D%0A    %0A    public static String getrefreshToken(String url, String clientId, String username, String password) %0A            throws ClientProtocolException, IOException%7B%0A        Map%3CString, String%3E urlParams = new HashMap%3CString, String%3E();%0A        urlParams.put(%22client_id%22, clientId);%0A        urlParams.put(%22username%22, username);%0A        urlParams.put(%22password%22, password);%0A        return connect(url, urlParams)%5B1%5D;%0A    %7D%0A    %0A    private static String%5B%5D connect(String url, Map%3CString, String%3E urlParams) %0A            throws ClientProtocolException, IOException%7B%0A        
 %0A   
@@ -2518,219 +2518,136 @@
     
-urlParameters.add(new BasicNameValuePair(%22username%22, username));%0A        urlParameters.add(new BasicNameValuePair(%22password%22, password));%0A        urlParameters.add(new BasicNameValuePair(%22client_id%22, clientId));
+for(String key : urlParams.keySet()) %7B%0A            urlParameters.add(new BasicNameValuePair(key, urlParams.get(key)));%0A        %7D
 %0A   
@@ -2780,24 +2780,91 @@
 httpPost);%0A%0A
+        String refreshToken = %22%22;%0A        String accessToken = %22%22;%0A
         try 
@@ -3397,39 +3397,32 @@
                 
-String 
 refreshToken = s
@@ -3593,39 +3593,32 @@
                 
-String 
 accessToken = sp
@@ -3685,56 +3685,8 @@
 1);%0A
-                            return accessToken;%0A
     
@@ -3842,19 +3842,81 @@
 rn n
-ull;%0A%0A    %7D
+ew String%5B%5D %7BaccessToken,refreshToken%7D;%0A        %0A        %0A    %7D%0A    %0A    
 %0A   

add simple OAuth client - more methods & refresh token