com.fb.action.FacebookConnectAction.java Source code

Java tutorial

Introduction

Here is the source code for com.fb.action.FacebookConnectAction.java

Source

/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
package com.fb.action;

import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
import com.liferay.portal.kernel.struts.StrutsAction;
import com.liferay.portal.kernel.util.CalendarFactoryUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.expando.DuplicateColumnNameException;
import com.liferay.portlet.expando.NoSuchTableException;
import com.liferay.portlet.expando.model.ExpandoColumn;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
import com.liferay.portlet.expando.model.ExpandoTable;
import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Julio Camarero
 */
public class FacebookConnectAction extends BaseStrutsAction {
    @Override
    public String execute(StrutsAction originalStrutsAction, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        if (!FacebookConnectUtil.isEnabled(themeDisplay.getCompanyId())) {
            throw new PrincipalException();
        }

        HttpSession session = request.getSession();

        String redirect = ParamUtil.getString(request, "redirect");

        String code = ParamUtil.getString(request, "code");

        String token = FacebookConnectUtil.getAccessToken(themeDisplay.getCompanyId(), redirect, code);

        if (Validator.isNotNull(token)) {
            session.setAttribute(FACEBOOK_ACCESS_TOKEN, token);

            setFacebookCredentials(session, themeDisplay.getCompanyId(), token);
        } else {
            _log.error("there is not token from facebook");

            //return mapping.findForward("/common/referer_jsp.jsp");
        }

        response.sendRedirect(redirect);

        return null;
    }

    protected User addUser(HttpSession session, long companyId, JSONObject jsonObject) throws Exception {

        long creatorUserId = 0;
        boolean autoPassword = true;
        String password1 = StringPool.BLANK;
        String password2 = StringPool.BLANK;
        boolean autoScreenName = true;
        String screenName = StringPool.BLANK;
        String emailAddress = jsonObject.getString("email");
        long facebookId = jsonObject.getLong("id");
        String openId = StringPool.BLANK;
        Locale locale = LocaleUtil.getDefault();
        String firstName = jsonObject.getString("first_name");
        String middleName = StringPool.BLANK;
        String lastName = jsonObject.getString("last_name");
        int prefixId = 0;
        int suffixId = 0;
        boolean male = Validator.equals(jsonObject.getString("gender"), "male");
        int birthdayMonth = Calendar.JANUARY;
        int birthdayDay = 1;
        int birthdayYear = 1970;
        String jobTitle = StringPool.BLANK;
        long[] groupIds = null;
        long[] organizationIds = null;
        long[] roleIds = null;
        long[] userGroupIds = null;
        boolean sendEmail = true;

        ServiceContext serviceContext = new ServiceContext();

        User user = UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password1, password2,
                autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName, middleName,
                lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
                organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);

        user = UserLocalServiceUtil.updateLastLogin(user.getUserId(), user.getLoginIP());

        user = UserLocalServiceUtil.updatePasswordReset(user.getUserId(), false);

        user = UserLocalServiceUtil.updateEmailAddressVerified(user.getUserId(), true);

        session.setAttribute(FACEBOOK_USER_EMAIL_ADDRESS, emailAddress);

        return user;
    }

    protected void saveTokenExpando(User user, String accesToken) throws Exception {

        ExpandoTable table = null;

        long companyId = user.getCompanyId();

        try {
            table = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, User.class.getName());
        } catch (NoSuchTableException nste) {
            table = ExpandoTableLocalServiceUtil.addDefaultTable(companyId, User.class.getName());
        }

        ExpandoColumn column = null;

        long tableId = table.getTableId();

        try {
            column = ExpandoColumnLocalServiceUtil.addColumn(tableId, "access_token",
                    ExpandoColumnConstants.STRING);
        } catch (DuplicateColumnNameException dcne) {
            column = ExpandoColumnLocalServiceUtil.getColumn(tableId, "access_token");
        }

        long classNameId = table.getClassNameId();
        long columnId = column.getColumnId();
        long classPK = user.getUserId();

        ExpandoValueLocalServiceUtil.addValue(classNameId, tableId, columnId, classPK, accesToken);
    }

    protected void setFacebookCredentials(HttpSession session, long companyId, String token) throws Exception {

        JSONObject jsonObject = FacebookConnectUtil.getGraphResources(companyId, "/me", token,
                "id,email,first_name,last_name,gender");

        if ((jsonObject == null) || (jsonObject.getJSONObject("error") != null)) {

            return;
        }

        if (FacebookConnectUtil.isVerifiedAccountRequired(companyId) && !jsonObject.getBoolean("verified")) {

            return;
        }

        User user = null;

        long facebookId = jsonObject.getLong("id");

        if (facebookId > 0) {
            session.setAttribute(FACEBOOK_USER_ID, String.valueOf(facebookId));

            try {
                user = UserLocalServiceUtil.getUserByFacebookId(companyId, facebookId);
            } catch (NoSuchUserException nsue) {
            }
        }

        String emailAddress = jsonObject.getString("email");

        if ((user == null) && Validator.isNotNull(emailAddress)) {
            session.setAttribute(FACEBOOK_USER_EMAIL_ADDRESS, emailAddress);

            try {
                user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);
            } catch (NoSuchUserException nsue) {
            }
        }

        if (user != null) {
            user = updateUser(user, jsonObject);
        } else {
            user = addUser(session, companyId, jsonObject);
        }

        saveTokenExpando(user, token);
    }

    protected User updateUser(User user, JSONObject jsonObject) throws Exception {

        long facebookId = jsonObject.getLong("id");
        String emailAddress = jsonObject.getString("email");
        String firstName = jsonObject.getString("first_name");
        String lastName = jsonObject.getString("last_name");
        boolean male = Validator.equals(jsonObject.getString("gender"), "male");

        if ((facebookId == user.getFacebookId()) && emailAddress.equals(user.getEmailAddress())
                && firstName.equals(user.getFirstName()) && lastName.equals(user.getLastName())
                && (male == user.isMale())) {

            return user;
        }

        Contact contact = user.getContact();

        Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

        birthdayCal.setTime(contact.getBirthday());

        int birthdayMonth = birthdayCal.get(Calendar.MONTH);
        int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
        int birthdayYear = birthdayCal.get(Calendar.YEAR);

        long[] groupIds = null;
        long[] organizationIds = null;
        long[] roleIds = null;
        List<UserGroupRole> userGroupRoles = null;
        long[] userGroupIds = null;

        ServiceContext serviceContext = new ServiceContext();

        if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
            UserLocalServiceUtil.updateEmailAddress(user.getUserId(), StringPool.BLANK, emailAddress, emailAddress);
        }

        UserLocalServiceUtil.updateEmailAddressVerified(user.getUserId(), true);

        return UserLocalServiceUtil.updateUser(user.getUserId(), StringPool.BLANK, StringPool.BLANK,
                StringPool.BLANK, false, user.getReminderQueryQuestion(), user.getReminderQueryAnswer(),
                user.getScreenName(), emailAddress, facebookId, user.getOpenId(), user.getLanguageId(),
                user.getTimeZoneId(), user.getGreeting(), user.getComments(), firstName, user.getMiddleName(),
                lastName, contact.getPrefixId(), contact.getSuffixId(), male, birthdayMonth, birthdayDay,
                birthdayYear, contact.getSmsSn(), contact.getAimSn(), contact.getFacebookSn(), contact.getIcqSn(),
                contact.getJabberSn(), contact.getMsnSn(), contact.getMySpaceSn(), contact.getSkypeSn(),
                contact.getTwitterSn(), contact.getYmSn(), contact.getJobTitle(), groupIds, organizationIds,
                roleIds, userGroupRoles, userGroupIds, serviceContext);
    }

    private String FACEBOOK_ACCESS_TOKEN = "FACEBOOK_ACCESS_TOKEN";

    private String FACEBOOK_USER_EMAIL_ADDRESS = "FACEBOOK_USER_EMAIL_ADDRESS";

    private String FACEBOOK_USER_ID = "FACEBOOK_USER_ID";

    private static Log _log = LogFactoryUtil.getLog(FacebookConnectAction.class);

}