public class OAuthConfig {

    private final String _consumerKey;
    private final String _consumerSecret;
    private final String _callbackUrl;
    private final String _signatureMethod;
    private final String _transportName;
    private String id;
    private Class<?> providerImplClass;


    public OAuthConfig(final String consumerKey, final String consumerSecret, final String callbackUrl,
                       final String signatureMethod, final String transportName) {
        _consumerKey = consumerKey;
        _consumerSecret = consumerSecret;
        if (signatureMethod == null || signatureMethod.length() == 0) {
            _signatureMethod = Constants.HMACSHA1_SIGNATURE;
        } else {
            _signatureMethod = signatureMethod;
        }
        if (transportName == null || transportName.length() == 0) {
            _transportName = MethodType.GET.toString();
        } else {
            _transportName = transportName;
        }
        if (callbackUrl == null || callbackUrl.length() == 0) {
            _callbackUrl = "";
        } else {
            _callbackUrl = callbackUrl;
        }

    }

    public OAuthConfig(final String consumerKey, final String consumerSecret, final String callbackUrl) {
        _consumerKey = consumerKey;
        _consumerSecret = consumerSecret;
        _transportName = MethodType.GET.toString();
        _signatureMethod = Constants.HMACSHA1_SIGNATURE;


        if (callbackUrl == null || callbackUrl.length() == 0) {
            _callbackUrl = "";
        } else {
            _callbackUrl = callbackUrl;
        }
    }

    public String get_consumerKey() {
        return _consumerKey;
    }

    public String get_consumerSecret() {
        return _consumerSecret;
    }

    public String get_callbackUrl() {
        return _callbackUrl;
    }

    public String get_signatureMethod() {
        return _signatureMethod;
    }

    public String get_transportName() {
        return _transportName;
    }

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public Class<?> getProviderImplClass() {
        return providerImplClass;
    }

    public void setProviderImplClass(final Class<?> providerImplClass) {
        this.providerImplClass = providerImplClass;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
        result.append(this.getClass().getName() + " Object {" + NEW_LINE);
        result.append(" consumerKey: " + _consumerKey + NEW_LINE);
        result.append(" consumerSecret: " + _consumerSecret + NEW_LINE);
        result.append(" signatureMethod: " + _signatureMethod + NEW_LINE);
        result.append(" transportName: " + _transportName + NEW_LINE);
        result.append(" id: " + id + NEW_LINE);
        result.append(" providerImplClass: " + providerImplClass + NEW_LINE);
        result.append("}");
        return result.toString();
    }


}
 public class Profile {

    private String name;
    private String link;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }


}
 public class ServerDataException extends Exception {

    private static final long serialVersionUID = 7313004590704350522L;

    public ServerDataException() {
        super();
    }

    /**
     * @param message
     */
    public ServerDataException(final String message) {
        super(message);
    }

    /**
     * @param message
     * @param cause
     */
    public ServerDataException(final String message, final Throwable cause) {
        super(message, cause);
    }

    /**
     * @param cause
     */
    public ServerDataException(final Throwable cause) {
        super(cause);
    }
}
 public class SocialAuthException extends Exception {

    /**
     *
     */
    private static final long serialVersionUID = -6856436294292027306L;

    public SocialAuthException() {
        super();
    }

    public SocialAuthException(final String message) {
        super(message);
    }

    public SocialAuthException(final Throwable cause) {
        super(cause);
    }

    public SocialAuthException(final String message, final Throwable cause) {
        super(message, cause);
    }
}
 public class UserDeniedPermissionException extends Exception {

    private static final long serialVersionUID = 8089726143500613807L;

    public UserDeniedPermissionException() {
        super("User has denied permission to access his/her account");
    }

    /**
     * @param message
     */
    public UserDeniedPermissionException(final String message) {
        super(message);
    }

    /**
     * @param cause
     */
    public UserDeniedPermissionException(final Throwable cause) {
        super(cause);
    }

    /**
     * @param message
     * @param cause
     */
    public UserDeniedPermissionException(final String message,
                                         final Throwable cause) {
        super(message, cause);
    }
}
 public class OAuth1 implements OAuthStrategyBase {

    private OAuthConsumer oauth;
    private Map<String, String> endpoints;
    private String scope;
    private String providerId;
    private String successUrl;

    public OAuth1(final OAuthConfig config, final Map<String, String> endpoints) {
        this.oauth = new OAuthConsumer(config);
        this.endpoints = endpoints;
        this.providerId = config.getId();
        this.successUrl = config.get_callbackUrl();
    }

    @Override
    public RequestToken getLoginRedirectURL() throws Exception {
        AccessGrant requestToken = oauth.getRequestToken(
                endpoints.get(Constants.OAUTH_REQUEST_TOKEN_URL), successUrl);
        String authUrl = endpoints.get(Constants.OAUTH_AUTHORIZATION_URL);
        if (scope != null) {
            authUrl += scope;
        }
        StringBuilder urlBuffer = oauth.buildAuthUrl(authUrl, requestToken, successUrl);
        System.out.println("Redirection to following URL should happen : " + urlBuffer.toString());

        return new RequestToken(urlBuffer.toString(), requestToken);
    }

    @Override
    public AccessGrant verifyResponse(AccessGrant requestToken,
                                      Map<String, String> requestParams) throws Exception {
        return verifyResponse(requestToken, requestParams, MethodType.GET.toString());
    }

    @Override
    public AccessGrant verifyResponse(
            AccessGrant requestToken,
            Map<String, String> requestParams, String methodType)
            throws Exception {
        if (requestToken == null) {
            throw new SocialAuthException("Request token is null");
        }
        String verifier = requestParams.get(Constants.OAUTH_VERIFIER);
        if (verifier != null) {
            requestToken.setAttribute(Constants.OAUTH_VERIFIER, verifier);
        }
        System.out.println("Call to fetch Access Token");
        AccessGrant accessToken = oauth.getAccessToken(
                endpoints.get(Constants.OAUTH_ACCESS_TOKEN_URL), requestToken);
        accessToken.setProviderId(providerId);
        return accessToken;
    }

    @Override
    public Response executeFeed(AccessGrant accessToken, String url)
            throws Exception {
        return oauth.httpGet(url, null, accessToken);
    }

    @Override
    public Response executeFeed(AccessGrant accessToken, String urlStr,
                                String methodType, Map<String, String> params,
                                Map<String, String> headerParams, String body) throws Exception {
        Response response = null;
        if (accessToken == null) {
            throw new SocialAuthException(
                    "Please call verifyResponse function first to get Access Token");
        }
        if (MethodType.GET.toString().equals(methodType)) {
            try {
                response = oauth.httpGet(urlStr, headerParams, accessToken);
            } catch (Exception ie) {
                throw new SocialAuthException(
                        "Error while making request to URL : " + urlStr, ie);
            }
        } else if (MethodType.PUT.toString().equals(methodType)) {
            try {
                response = oauth.httpPut(urlStr, params, headerParams, body,
                        accessToken);
            } catch (Exception e) {
                throw new SocialAuthException(
                        "Error while making request to URL : " + urlStr, e);
            }
        } else if (MethodType.POST.toString().equals(methodType)) {
            try {
                response = oauth.httpPost(urlStr, params, headerParams, body,
                        accessToken);
            } catch (Exception e) {
                throw new SocialAuthException(
                        "Error while making request to URL : " + urlStr, e);
            }
        }
        return response;
    }

    @Override
    public void setScope(String scope) {
        this.scope = scope;
    }

    @Override
    public void setAccessTokenParameterName(String accessTokenParameterName) {
        // 'accessTokenParameterName' is not implemented for OAuth1
    }


}
 public interface OAuthStrategyBase {

    public RequestToken getLoginRedirectURL() throws Exception;

    public AccessGrant verifyResponse(
            AccessGrant requestToken,
            Map<String, String> requestParams)
            throws Exception;

    public AccessGrant verifyResponse(
            AccessGrant requestToken,
            Map<String, String> requestParams,
            String methodType) throws Exception;

    public Response executeFeed(
            AccessGrant accessGrant,
            String url) throws Exception;

    public Response executeFeed(
            AccessGrant accessGrant,
            String url,
            String methodType,
            Map<String, String> params,
            Map<String, String> headerParams,
            String body) throws Exception;

    public void setScope(final String scope);

    public void setAccessTokenParameterName(String accessTokenParameterName);


}
 public class RequestToken {

    private String url;
    private AccessGrant accessGrant;

    public RequestToken(String url, AccessGrant accessGrant) {
        this.url = url;
        this.accessGrant = accessGrant;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public AccessGrant getAccessGrant() {
        return accessGrant;
    }

    public void setAccessGrant(AccessGrant accessGrant) {
        this.accessGrant = accessGrant;
    }


} public class AccessGrant implements Serializable {

    private static final long serialVersionUID = -7120362372191191930L;
    private String key;
    private String secret;
    private String providerId;
    private Map<String, Object> _attributes;

    /**
     * @param key    the Key
     * @param secret the Secret
     */
    public AccessGrant(final String key, final String secret) {
        this.key = key;
        this.secret = secret;
    }

    public AccessGrant() {
    }

    /**
     * Retrieves the Token Key
     *
     * @return the Token Key
     */
    public String getKey() {
        return key;
    }

    /**
     * Updates the Token Key
     *
     * @param key the Token Key
     */
    public void setKey(final String key) {
        this.key = key;
    }

    /**
     * Retrieves the Token Secret
     *
     * @return the Token Secret
     */
    public String getSecret() {
        return secret;
    }

    /**
     * Updates the Token Secret
     *
     * @param secret the Token Secret
     */
    public void setSecret(final String secret) {
        this.secret = secret;
    }

    /**
     * Gets the attributes of this token.
     */
    public Map<String, Object> getAttributes() {
        return _attributes;
    }

    /**
     * Gets an attribute based from the given key.
     */
    public Object getAttribute(final String key) {
        return _attributes == null ? null : _attributes.get(key);
    }

    /**
     * Sets an attribute based from the given key and value.
     */
    public void setAttribute(final String key, final Object value) {
        if (_attributes == null) {
            _attributes = new HashMap<String, Object>();
        }

        _attributes.put(key, value);
    }

    /**
     * Sets an attributes from given attributes map.
     */
    public void setAttributes(final Map<String, Object> attributes) {
        if (_attributes == null) {
            _attributes = new HashMap<String, Object>();
        }

        _attributes.putAll(attributes);
    }

    /**
     * Retrieves the provider id.
     *
     * @return the provider id.
     */
    public String getProviderId() {
        return providerId;
    }

    /**
     * Updates the provider id.
     *
     * @param providerId the provider id.
     */
    public void setProviderId(final String providerId) {
        this.providerId = providerId;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
        result.append(this.getClass().getName() + " Object {" + NEW_LINE);
        result.append(" token key : " + key + NEW_LINE);
        result.append(" token secret : " + secret + NEW_LINE);
        result.append("provider id : " + providerId + NEW_LINE);
        if (_attributes != null) {
            result.append(_attributes.toString());
        }
        result.append("}");

        return result.toString();
    }

}
 public interface Constants {

    /**
     * UTF-8
     */
    public static final String ENCODING = "UTF-8";

    /**
     * oauth_consumer_key
     */
    public static final String OAUTH_CONSUMER_KEY = "oauth_consumer_key";

    /**
     * oauth_signature_method
     */
    public static final String OAUTH_SIGNATURE_METHOD = "oauth_signature_method";

    /**
     * oauth_signature
     */
    public static final String OAUTH_SIGNATURE = "oauth_signature";

    /**
     * "oauth_timestamp"
     */
    public static final String OAUTH_TIMESTAMP = "oauth_timestamp";

    /**
     * "oauth_nonce"
     */
    public static final String OAUTH_NONCE = "oauth_nonce";

    /**
     * "oauth_version"
     */
    public static final String OAUTH_VERSION = "oauth_version";

    /**
     * "oauth_token"
     */
    public static final String OAUTH_TOKEN = "oauth_token";

    /**
     * "oauth_token_secret"
     */
    public static final String OAUTH_TOKEN_SECRET = "oauth_token_secret";

    /**
     * "oauth_callback"
     */
    public static final String OAUTH_CALLBACK = "oauth_callback";

    /**
     * "oauth_callback_confirmed"
     */
    public static final String OAUTH_CALLBACK_CONFIRMED = "oauth_callback_confirmed";

    /**
     * "oauth_problem"
     */
    public static final String OAUTH_PROBLEM = "oauth_problem";

    /**
     * "oauth_verifier"
     */
    public static final String OAUTH_VERIFIER = "oauth_verifier";

    /**
     * "oauth_"
     */
    public static final String PREFIX = "oauth_";

    /**
     * "1.0"
     */
    public static final String CURRENT_VERSION = "1.0";

    /**
     * "oob"
     */
    public static final String OOB = "oob";

    /**
     * HMAC-SHA1
     */
    public static final String HMACSHA1_SIGNATURE = "HMAC-SHA1";

    /**
     * request token url
     */
    public static final String OAUTH_REQUEST_TOKEN_URL = "reqTokenURL";

    /**
     * authorization url
     */
    public static final String OAUTH_AUTHORIZATION_URL = "authorizationURL";

    /**
     * access token url
     */
    public static final String OAUTH_ACCESS_TOKEN_URL = "accessTokenURL";

    /**
     * token expires string
     */
    public static final String EXPIRES = "expires";

    /**
     * facebook
     */
    public static final String FACEBOOK = "facebook";

    /**
     * foursquare
     */
    public static final String FOURSQUARE = "foursquare";

    /**
     * google
     */
    public static final String GOOGLE = "google";


    /**
     * google +
     */
    public static final String GOOGLEPLUS = "googleplus";

    /**
     * hotmail
     */
    public static final String HOTMAIL = "hotmail";

    /**
     * linkedin
     */
    public static final String LINKEDIN = "linkedin";

    /**
     * myspace
     */
    public static final String MYSPACE = "myspace";

    /**
     * twitter
     */
    public static final String TWITTER = "twitter";

    /**
     * yahoo
     */
    public static final String YAHOO = "yahoo";

    /**
     * salesforce
     */
    public static final String SALESFORCE = "salesforce";

    /**
     * yammer
     */
    public static final String YAMMER = "yammer";

    /**
     * mendeley
     */
    public static final String MENDELEY = "mendeley";

    /**
     * runkeeper
     */
    public static final String RUNKEEPER = "runkeeper";

    /**
     * access token parameter name
     */
    public static final String ACCESS_TOKEN_PARAMETER_NAME = "access_token";

    /**
     * Proxy host property name
     */
    public static final String PROXY_HOST = "proxy.host";

    /**
     * Proxy port property name
     */
    public static final String PROXY_PORT = "proxy.port";

    /**
     * HTTP connection timeout property
     */
    public static final String HTTP_CONNECTION_TIMEOUT = "http.connectionTimeOut";

    /**
     * Content Encoding Header
     */
    public static final String CONTENT_ENCODING_HEADER = "Content-Encoding";

    /**
     * GZip Content Encoding
     */
    public static final String GZIP_CONTENT_ENCODING = "gzip";
}
 public class Response {

    private final HttpURLConnection _connection;

    Response(final HttpURLConnection connection) {
        _connection = connection;
    }

    /**
     * Closes the connection
     *
     * @throws java.io.IOException
     */
    public void close() throws IOException {
        _connection.disconnect();
    }

    public String getHeader(final String name) {
        return _connection.getHeaderField(name);
    }

    /**
     * Gets the response content via InputStream.
     *
     * @return response input stream
     * @throws java.io.IOException
     */
    public InputStream getInputStream() throws IOException {
        return _connection.getInputStream();
    }

    /**
     * Gets the response HTTP status.
     *
     * @return the HTTP status
     */
    public int getStatus() {
        try {
            return _connection.getResponseCode();
        } catch (IOException e) {
            return 404;
        }
    }

    /**
     * Gets the response content as String using given encoding
     *
     * @param encoding the encoding type
     * @return Response body
     * @throws Exception
     */
    public String getResponseBodyAsString(final String encoding)
            throws Exception {
        String line = null;
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();

        if (Constants.GZIP_CONTENT_ENCODING.equals(_connection
                .getHeaderField(Constants.CONTENT_ENCODING_HEADER))) {

            reader = new BufferedReader(
                    new InputStreamReader(
                            new GZIPInputStream(_connection.getInputStream()), encoding));
        } else {
            reader = new BufferedReader(new InputStreamReader(_connection.getInputStream(), encoding));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}

import io.stalk.common.oauth.OAuthConfig;
import io.stalk.common.oauth.Profile;
import io.stalk.common.oauth.exception.ServerDataException;
import io.stalk.common.oauth.exception.SocialAuthException;
import io.stalk.common.oauth.exception.UserDeniedPermissionException;
import io.stalk.common.oauth.strategy.OAuth1;
import io.stalk.common.oauth.strategy.OAuthStrategyBase;
import io.stalk.common.oauth.strategy.RequestToken;
import io.stalk.common.oauth.utils.AccessGrant;
import io.stalk.common.oauth.utils.Constants;
import io.stalk.common.oauth.utils.Response;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

package io.stalk.common.oauth.provider;



public class TwitterImpl extends AbstractProvider implements AuthProvider {


    // https://dev.twitter.com/docs/api/1.1/get/users/show (2013 추석에 청주에서!!)
    private static final String PROFILE_URL = "http://api.twitter.com/1.1/users/show.json?screen_name=";
    private static final Map<String, String> ENDPOINTS;

    private OAuthConfig config;
    private OAuthStrategyBase authenticationStrategy;

    static {
        ENDPOINTS = new HashMap<String, String>();
        ENDPOINTS.put(Constants.OAUTH_REQUEST_TOKEN_URL,
                "http://api.twitter.com/oauth/request_token");
        ENDPOINTS.put(Constants.OAUTH_AUTHORIZATION_URL,
                "https://api.twitter.com/oauth/authenticate");
        ENDPOINTS.put(Constants.OAUTH_ACCESS_TOKEN_URL,
                "https://api.twitter.com/oauth/access_token");
    }

    public TwitterImpl(final OAuthConfig providerConfig) throws Exception {
        config = providerConfig;
        authenticationStrategy = new OAuth1(config, ENDPOINTS);
    }

    @Override
    public RequestToken getLoginRedirectURL() throws Exception {
        return authenticationStrategy.getLoginRedirectURL();
    }


    @Override
    public Profile connect(final AccessGrant requestToken, final Map<String, String> requestParams)
            throws Exception {

        if (requestParams.get("denied") != null) {

            throw new UserDeniedPermissionException();
