Package com.box.boxjavalibv2.authorization

Source Code of com.box.boxjavalibv2.authorization.OAuthAuthorization

package com.box.boxjavalibv2.authorization;

import com.box.boxjavalibv2.dao.BoxOAuthToken;
import com.box.boxjavalibv2.exceptions.AuthFatalFailureException;
import com.box.restclientv2.authorization.DefaultRequestAuth;
import com.box.restclientv2.exceptions.BoxRestException;
import com.box.restclientv2.interfaces.IBoxRequest;

/**
* This is authorization class for API requests using OAuth.
*/
public class OAuthAuthorization extends DefaultRequestAuth {

    private static final String BEARER = "Bearer";
    private final OAuthDataController mOAuth;

    /**
     * Constructor.
     *
     * @param oAuth
     *            OAuth
     * @param autoRefresh
     *            Whether allow auto refresh the OAuth token when it's expired. Note you should only set this to true if you are making API calls in a single
     *            threaded fashion. Multiple threads trying to auto refresh at a same time will cause trouble. In that case you should implement your own logic
     *            to refresh synchronously.
     */
    public OAuthAuthorization(final OAuthDataController oAuth) {
        this.mOAuth = oAuth;
    }

    public void setOAuthData(BoxOAuthToken data) {
        mOAuth.setOAuthData(data);
        mOAuth.initialize();
    }

    /**
     * Refresh the OAuth token.
     *
     * @throws AuthFatalFailureException
     *             exception
     */
    public void refresh() throws AuthFatalFailureException {
        mOAuth.refresh();
    }

    /**
     * Initialize this auth. This need to be called before making an API request using this auth.
     */
    public void initOAuthForRequest() {
        mOAuth.initialize();
    }

    @Override
    public void setAuth(final IBoxRequest request) throws BoxRestException, AuthFatalFailureException {
        super.setAuth(request);

        request.addHeader(AUTH_HEADER_NAME, getAuthString());
    }

    /**
     * Get auth string to be put into request header.
     *
     * @return string
     * @throws AuthFatalFailureException
     */
    private String getAuthString() throws AuthFatalFailureException {
        BoxOAuthToken data = mOAuth.getAuthData();
        return BEARER + " " + data.getAccessToken();
    }
}
TOP

Related Classes of com.box.boxjavalibv2.authorization.OAuthAuthorization

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.