ext.sns.auth.OAuth2Client.java Source code

Java tutorial

Introduction

Here is the source code for ext.sns.auth.OAuth2Client.java

Source

/*
 * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved.
 * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Created on 2014-3-27
 */
package ext.sns.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import play.Logger;
import play.Logger.ALogger;
import utils.WSUtil;
import ext.sns.config.ConfigManager;
import ext.sns.config.ProviderConfig;

/**
 * 
 * 
 * @ClassName: OAuthClient
 * @Description: ?????Authorization Code Grant?
 * @date 2014-3-27 ?3:34:58
 * @author ShenTeng
 * 
 */
public class OAuth2Client {

    private static final ALogger LOGGER = Logger.of(OAuth2Client.class);

    /**
     * ??URI
     * 
     * @param providerName ?????
     * @param type ???nullempty
     * @param callbackParam ??null
     * @param specialParamKey ?Key?providerkey???????null
     * @return ?URI
     */
    public static String getAuthRequestURI(String providerName, String type, Map<String, String> callbackParam,
            String specialParamKey) {
        if (StringUtils.isBlank(providerName)) {
            throw new IllegalArgumentException("??providerString=" + providerName);
        }

        List<String> providerList = ConfigManager.getProviderNameByTypes(type);
        if (!providerList.contains(providerName)) {
            throw new IllegalArgumentException("?providerNametypeproviderName = "
                    + providerName + " ,type = " + type + ".");
        }
        callbackParam.put(ConfigManager.TYPE_KEY, type);

        ProviderConfig providerConfig = ConfigManager.getProviderConfigByName(providerName);
        String requestAuthFullURI = providerConfig.getRequestAuthFullURI(callbackParam, specialParamKey);

        LOGGER.debug("requestAuthFullURI:" + requestAuthFullURI);
        return requestAuthFullURI;
    }

    /**
     * ??URI?Map
     * 
     * @param uri ?URI
     * @return ?Map
     */
    public static Map<String, String> getAuthBackParamByURI(String uri) {
        // ??URI1????&
        int countMatches = StringUtils.countMatches(uri, "?");
        if (countMatches > 1) {
            int firstIndex = uri.indexOf("?");

            StringBuilder uriBuilder = new StringBuilder(uri);
            for (int i = 0; i < countMatches - 1; ++i) {
                int start = uriBuilder.indexOf("?", firstIndex + 1);
                uriBuilder.replace(start, start + 1, "&");
            }
            uri = uriBuilder.toString();
        }

        String paramStr = uri.substring(uri.indexOf("?") + 1);

        Map<String, String> paramMap = new HashMap<String, String>();

        String[] pairArray = paramStr.split("&");
        for (String pair : pairArray) {
            String[] paramArray = pair.split("=");

            String val = paramArray.length == 2 ? paramArray[1] : "";
            paramMap.put(paramArray[0], val);
        }

        return paramMap;
    }

    /**
     * ???AuthResponse
     * 
     * @param params ??
     * @return AuthResponse, null - ???
     */
    public static AuthResponse getAuthResponse(Map<String, String> params) {
        return AuthResponse.create(params);
    }

    /**
     * ?accessToken
     * 
     * @param authResponse AuthResponse
     * @return ???? null - 
     */
    public static AccessTokenResponse accessToken(AuthResponse authResponse) {
        if (null == authResponse) {
            throw new IllegalArgumentException("??authResponse=" + authResponse);
        }

        ProviderConfig providerConfig = ConfigManager.getProviderConfigByAuthResponse(authResponse);

        if (null == providerConfig) {
            throw new IllegalArgumentException("??authResponse" + authResponse.toString());
        }

        String accessTokenFullURI = providerConfig.getAccessTokenFullURI(authResponse);

        LOGGER.debug("accessTokenFullURI:" + accessTokenFullURI);

        String body = WSUtil.postFormURLEncoded(accessTokenFullURI).get(30000).getBody();

        LOGGER.debug("accessTokenResponse:" + body);

        AccessTokenResponse response = AccessTokenResponse.create(body);

        if (null == response) {
            LOGGER.error("accessToken?body" + body);
        }

        return response;
    }
}