com.matel.pg.components.OAuthController.java Source code

Java tutorial

Introduction

Here is the source code for com.matel.pg.components.OAuthController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matel.pg.components;

import java.lang.ProcessBuilder;
import com.intuit.ia.connection.IAPlatformClient;
import com.intuit.ia.exception.OAuthException;
import com.matel.pg.oauth.CompanyRequestTokenSecret;
import com.matel.pg.oauth.OAuthInfoProvider;
import com.matel.utils.SpringComponent;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author badri
 */
@Component(OAuthController.COMP_NAME)
@Scope(SpringComponent.SCOPE_SESSION)
public class OAuthController implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String COMP_NAME = "components_oauthController";

    @Autowired
    private OAuthInfoProvider oAuthInfoProvider;

    /**
     * Creates a new instance of OAuthController
     */
    public OAuthController() {
    }

    //    @Autowired
    //    private Properties oAuthProps;

    private String requestToken;

    private String requestTokenSecret;

    public void requestOAuthToken() {
        //Instantiate the QuickBook SDK's IAPlatformClient object
        IAPlatformClient client = new IAPlatformClient();
        try {

            //            System.out.println(oAuthProps.get("consumerSecret"));
            //Use the IAPlatformClient to get a Request Token and Request Token Secret from Intuit
            final Map<String, String> requestTokenAndSecret = client.getRequestTokenAndSecret(
                    oAuthInfoProvider.getConsumerKey(), oAuthInfoProvider.getConsumerSecret());

            if (requestTokenAndSecret == null) {
                throw new RuntimeException(
                        "The system does not recognize the provided consumer key.  The development consumer key is only recognized in the sandbox environment; the production consumer key is only recognized in the 'published' environment.");
            }

            //Pull the values out of the map
            final String requestToken = requestTokenAndSecret.get("requestToken");
            final String requestTokenSecret = requestTokenAndSecret.get("requestTokenSecret");
            this.requestToken = requestToken;
            this.requestTokenSecret = requestTokenSecret;
            //Persist the request token and request token secret in the app database on the given company, we will need the
            //Request Token Secret to make the final request to Intuit for the Access Tokens
            oAuthInfoProvider.setRequestTokenValuesForCompany("1", requestToken, requestTokenSecret);
            // Retrieve the Authorize URL
            final String authURL = client.getOauthAuthorizeUrl(requestToken);
            FacesContext.getCurrentInstance().getExternalContext().redirect(authURL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void redirect(String url) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(url);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void requestTokenReady() throws IOException {
        HttpServletRequest request = getRequest();

        IAPlatformClient client = new IAPlatformClient();

        final String verifierCode = request.getParameter("oauth_verifier");
        final String realmID = request.getParameter("realmId");
        final String requestToken = request.getParameter("oauth_token");

        final CompanyRequestTokenSecret companyRequestTokenSecret = oAuthInfoProvider
                .getCompanyRequestTokenSecret(requestToken);

        try {
            final Map<String, String> oAuthAccessToken = client.getOAuthAccessToken(verifierCode, requestToken,
                    companyRequestTokenSecret.getRequestTokenSecret(), oAuthInfoProvider.getConsumerKey(),
                    oAuthInfoProvider.getConsumerSecret());

            final String accessToken = oAuthAccessToken.get("accessToken");
            final String accessTokenSecret = oAuthAccessToken.get("accessTokenSecret");
            oAuthInfoProvider.setAccessTokenForCompany(companyRequestTokenSecret.getAppCompanyId(), realmID,
                    accessToken, accessTokenSecret);
            QuickBooksConnectModel.getInstance().setConnectedToQbo(false);
            redirect(getProtocolHostnameAndPort(request) + "/close.html");

        } catch (OAuthException e) {
            e.printStackTrace();
        }
    }

    /**
     * get HttpServletRequest object from FacesContext
     *
     * @return
     */
    private static HttpServletRequest getRequest() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        if (request == null) {
            throw new RuntimeException("Sorry. Got a null request from faces context");
        }
        return request;
    }

    /**
     *
     * @param request
     * @return
     */
    public static String getProtocolHostnameAndPort(final HttpServletRequest request) {
        String protocol = request.getProtocol().split("/")[0].toLowerCase();
        String hostname = request.getServerName();
        int port = request.getServerPort();

        StringBuilder result = new StringBuilder(protocol + "://" + hostname);
        if (port != 80) {
            result.append(":").append(port);
        }

        return result.toString();
    }

}