/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *
 */
// https://issues.redhat.com/browse/TEIID-3618
package org.teiid.resource.adapter.salesforce;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.simple.parser.JSONParser;
import org.teiid.OAuthCredential;
import org.teiid.resource.adapter.salesforce.transport.SalesforceConnectorConfig;

import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import org.json.simple.JSONObject;

public class TeiidPartnerConnection extends PartnerConnection {

    private static final String AUTHORIZATION = "Authorization"; //$NON-NLS-1$

    public TeiidPartnerConnection(SalesforceConnectorConfig config) throws ConnectionException {
        super(config);
    }

    public com.sforce.soap.partner.LoginResult login(java.lang.String username,java.lang.String password)
            throws com.sforce.ws.ConnectionException {
        
        SalesforceConnectorConfig config = (SalesforceConnectorConfig)getConfig();
        if (config.getCredential(OAuthCredential.class.getName()) == null) {
            return super.login(username, password);
        }
        
        // for details see
        // https://developer.salesforce.com/blogs/developer-relations/2011/03/oauth-and-the-soap-api.html
        OAuthCredential credential = (OAuthCredential)config.getCredential(OAuthCredential.class.getName());
        String id = credential.getAuthrorizationProperty("id");
        if (id == null) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection");
        }
        String accessToken = TeiidPartnerConnection.getAuthorizationHeader(credential);

        OutputStreamWriter wr = null;
        com.sforce.soap.partner.LoginResult loginResult;
        try {
            URL url = new URL(id);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            try {
                conn.setRequestProperty (AUTHORIZATION, accessToken);
                conn.setRequestMethod("POST");
                conn.setUseCaches(false);
                conn.setDoInput(true);
                conn.setDoOutput(true);

                // getting the response
                if (conn.getResponseCode() < 400) {
                    // if we get successful reply
                    JSONObject root;
                    InputStream is = conn.getInputStream();
                    try {
                        // parsing reply
                        root = (JSONObject) new JSONParser().parse(new InputStreamReader(is));
                    } finally {
                        is.close();
                    }

                    loginResult = new com.sforce.soap.partner.LoginResult();
                    loginResult.setSessionId(accessToken.substring(7)); // remove "Bearer " prefix.

                    String endpoint = config.getAuthEndpoint();
                    int index = endpoint.indexOf("Soap/u/"); //$NON-NLS-1$
                    String apiVersion = endpoint.substring(index+7);

                    JSONObject urls = (JSONObject) root.get("urls");
                    String partnerURL = (String) urls.get("partner");
                    partnerURL = partnerURL.replace("{version}", apiVersion);

                    loginResult.setServerUrl(partnerURL);
                } else {
                    // if we get error
                    StringBuilder error = new StringBuilder("Failed to get OAuth based connection; "
                            + "Failed to get user information: response code: " + conn.getResponseCode() + "\n");
                    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                    String line;
                    while ((line = rd.readLine()) != null)
                        error.append("Failed to get user information: " + line + "\n");
                    rd.close();
                    throw new ConnectionException(error.toString());
                }
            } finally {
                if (wr != null)
                    wr.close();
                conn.disconnect();
            }
        } catch (Exception e) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; "
                    + "Failed to get user information", e);
        }

        /*com.sforce.soap.partner.LoginResult loginResult = null;
        WebClient client = WebClient.create(id);
        client.header(AUTHORIZATION, accessToken);
        String response = client.get(String.class);
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new ByteArrayInputStream(response.getBytes()));
            doc.getDocumentElement().normalize();
            
            Element urls = (Element)doc.getDocumentElement().getElementsByTagName("urls").item(0);
            loginResult = new com.sforce.soap.partner.LoginResult();
            loginResult.setSessionId(accessToken.substring(7)); // remove "Bearer " prefix.
            
            String endpoint = config.getAuthEndpoint();
            int index = endpoint.indexOf("Soap/u/"); //$NON-NLS-1$
            String apiVersion = endpoint.substring(index+7);
            
            String partnerURL = urls.getElementsByTagName("partner").item(0).getTextContent();
            partnerURL = partnerURL.replace("{version}", apiVersion);
            
            loginResult.setServerUrl(partnerURL);
            
        } catch (IOException e) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; "
                    + "Failed to get user information", e);
        } catch (ParserConfigurationException e) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; "
                    + "Failed to get user information", e);            
        } catch (IllegalStateException e) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; "
                    + "Failed to get user information", e);            
        } catch (SAXException e) {
            throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; "
                    + "Failed to get user information", e);            
        }*/
        
        return loginResult;
    }

    public static String getAuthorizationHeader(OAuthCredential credential) {
        return credential.getAuthorizationHeader(null, "POST");
    }
}
