com.brightcove.com.uploader.helper.LoginHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.brightcove.com.uploader.helper.LoginHelper.java

Source

/**
 * Copyright (C) 2010 Brightcove Inc. All Rights Reserved. No use, copying or distribution of this
 * work may be made except in accordance with a valid license agreement from Brightcove Inc. This
 * notice must be included on all copies, modifications and derivatives of this work.
 * 
 * Brightcove Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * "Brightcove" is a registered trademark of Brightcove Inc.
 */
package com.brightcove.com.uploader.helper;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import com.brightcove.common.logging.BrightcoveLog;

/**
 * LoginHelper is able to connect to a running server, 
 * login and save the token for future calls.
 * 
 * The HTTP Client used is exposed for reuse if needed.
 * 
 * @author storpey
 *
 */
public class LoginHelper {

    private BrightcoveLog mLog = BrightcoveLog.getLogger(this.getClass());

    private DefaultHttpClient client;

    private String pod;
    private String admin_token;
    private String token;

    /**
     *   Initialize instance, creates DefaultHttpClient to use in future actions
     */
    public LoginHelper() {
        super();

        client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();

        client = new DefaultHttpClient(new ThreadSafeClientConnManager(mgr.getSchemeRegistry()), params);

    }

    /**
     * Logs into the server with credentials provided.
     * @param pUri  URI pointing to the login server you want to hit.
     * @param user
     * @param pass
     * @throws HttpException
     * @throws IOException
     * @throws URISyntaxException
     */
    public void loginToStudio(URI pUri, String user, String pass) throws IOException, URISyntaxException {

        NameValuePair[] data = { new BasicNameValuePair("email", user), new BasicNameValuePair("password", pass),
                new BasicNameValuePair("_saveEmailEnabled", "on"), new BasicNameValuePair("redirect", ""),
                new BasicNameValuePair("moverride", "false") };
        mLog.info(pUri);
        mLog.info("Logging in as " + user + " with password " + pass);

        HttpPost method = new HttpPost(pUri);
        method.setHeader("Host", "sigin.brightcove.com");
        method.setEntity(new UrlEncodedFormEntity(Arrays.asList(data), HTTP.UTF_8));

        HttpResponse resp = client.execute(method);

        Header[] headers = resp.getHeaders("Set-Cookie");
        for (int i = 0; i < headers.length; i++) {
            mLog.info(headers[i].getName() + " " + headers[i].getValue());

            String value = headers[i].getValue();
            if (value.contains("BC_TOKEN")) {
                token = value.substring(value.indexOf("BC_TOKEN=") + 9);
                token = token.substring(0, token.indexOf(";"));
                mLog.info(token);
            } else if (value.contains("BC_POD")) {
                pod = value.substring(value.indexOf("BC_POD=") + 7);
                pod = pod.substring(0, pod.indexOf(";"));
                mLog.info(pod);

            }
        }

    }

    public void loginToWacky(URI pUri, String user, String pass) throws IOException {

        NameValuePair[] data = { new BasicNameValuePair("username", user), new BasicNameValuePair("password", pass),
                new BasicNameValuePair("u", "/services/internal/index.jsp"),
                new BasicNameValuePair("Submit", "Post") };
        mLog.info(pUri);

        HttpPost method = new HttpPost(pUri);
        method.setHeader("Host", "services.brightcove.com");
        method.setEntity(new UrlEncodedFormEntity(Arrays.asList(data), HTTP.UTF_8));

        HttpResponse resp = client.execute(method);

        Header[] headers = resp.getHeaders("Set-Cookie");
        for (int i = 0; i < headers.length; i++) {
            mLog.info(headers[i].getName() + " " + headers[i].getValue());

            String value = headers[i].getValue();
            if (value.contains("BC_TOKEN_WACKY")) {
                admin_token = value.substring(value.indexOf("BC_TOKEN_WACKY=") + 9);
                admin_token = admin_token.substring(0, admin_token.indexOf(";"));
                mLog.info(token);
            }
        }

    }

    /**
     * Override the HTTP client used for login.  Useful
     * if you are already managing session yourself.
     * @param client
     */
    public void setClient(DefaultHttpClient client) {
        this.client = client;
    }

    public String getAdmin_token() {
        return admin_token;
    }

    public DefaultHttpClient getClient() {
        return client;
    }

    public String getToken() {
        return token;
    }
}