home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/SampleRealmAuthenticator.java,v 1.3 2005/01/18 11:52:16 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.3 $
 * $Date: 2005/01/18 11:52:16 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Phong Ta Quoc  phongtq@myvietnam.net
 */
package com.mvnforum.auth;

import java.sql.Date;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.mvnforum.MVNForumConfig;
import com.mvnforum.MVNForumConstant;
import com.mvnforum.common.SendMailUtil;
import com.mvnforum.db.DAOFactory;
import com.mvnforum.db.MemberBean;
import com.mvnforum.search.member.MemberIndexer;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This is a sample class that implement Authenticator for authenticate user when the system use
 * authentication type: Realm <p>
 * Use this class as a sample to write your own Single Sign-On solution
 */
public class SampleRealmAuthenticator implements Authenticator {

    private static Log log = LogFactory.getLog(SampleRealmAuthenticator.class);

    /* (non-Javadoc)
     * @see com.mvnforum.auth.Authenticator#getRemoteUser(javax.servlet.http.HttpServletRequest)
     * just return remote user if we find them in the request.getRemoteUser()
     */
    public String getRemoteUser(HttpServletRequest request) {
        String memberName = request.getRemoteUser();
        memberName = StringUtil.getEmptyStringIfNull(memberName);
        //log.debug("Member Name is " + memberName);
        if (memberName.length() > 0) {
            try {
                DAOFactory.getMemberDAO().findByAlternateKey_MemberName(memberName);
            } catch (ObjectNotFoundException onfe) {
                // memberName does not exist, let to add an account "memberName"
                try {
                    createAccount(memberName, request);
                } catch (Exception e) {
                    // just return null;
                    log.error("Error is :" + e);
                    return null;
                }
            } catch (DatabaseException de) {
                // do nothing,
                log.error("Error is :" + de);
            }
        }
        //log.debug("Member Name is " + memberName);
        return memberName;
    }

    private static void createAccount(String memberName, HttpServletRequest request)
        throws ObjectNotFoundException, CreateException, DatabaseException,
        DuplicateKeyException, ForeignKeyNotFoundException {

        Timestamp now = DateUtil.getCurrentGMTTimestamp();
        Date memberBirthday = new java.sql.Date(now.getTime());

        // You should think of creating the email based on your company policy
        String email = memberName + "@yourdomain.com";

        DAOFactory.getMemberDAO().create(memberName, "Realm", email,
                email, MemberBean.MEMBER_EMAIL_VISIBLE, MemberBean.MEMBER_NAME_VISIBLE,
                request.getRemoteAddr(), request.getRemoteAddr(), 0/*memberViewCount*/,
                0/*memberPostCount*/, now, now,now,
                now/*memberLastLogon*/, 0, MemberBean.MEMBER_STATUS_ENABLE,
                "", ""/*memberTempPassword*/, 0/*memberMessageCount*/,
                0, 10, 0/*memberWarnCount*/,
                0/*memberVoteCount*/, 0/*memberVoteTotalStars*/, 0/*memberRewardPoints*/,
                ""/*memberTitle*/, 0/*memberTimeZone*/, ""/*memberSignature*/,
                ""/*memberAvatar*/, ""/*memberSkin*/, ""/*memberLanguage*/,
                ""/*memberFirstname*/, ""/*memberLastname*/, 1/*memberGender*/,
                memberBirthday, ""/*memberAddress*/, ""/*memberCity*/,
                ""/*memberState*/, ""/*memberCountry*/, ""/*memberPhone*/,
                ""/*memberMobile*/, ""/*memberFax*/, ""/*memberCareer*/,
                ""/*memberHomepage*/, ""/*memberYahoo*/, ""/*memberAol*/,
                ""/*memberIcq*/, ""/*memberMsn*/, ""/*memberCoolLink1*/,
                ""/*memberCoolLink2*/);

        // Now, create 4 default folders for each member
        int memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
        int folderStatus = 0;
        int folderOption = 0;
        int folderType = 0;
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_INBOX, memberID, 0/*order*/, folderStatus, folderOption, folderType, now, now);
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_DRAFT, memberID, 1/*order*/, folderStatus, folderOption, folderType, now, now);
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_SENT, memberID, 2/*order*/, folderStatus, folderOption, folderType, now, now);
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_TRASH, memberID, 3/*order*/, folderStatus, folderOption, folderType, now, now);

        // Add member to the lucene index
        MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID);
        MemberIndexer.scheduleAddMemberTask(memberBean);

        // now, if require activation, then we will send mail
        // Note that because after this page succeed,
        // we redirect to usermanagement so not use mvnforum.mail.failed now
        if (MVNForumConfig.getRequireActivation()) {
            String serverName = ParamUtil.getServerPath(); //ParamUtil.getServer2(request);
            try {
                SendMailUtil.sendActivationCodeEmail(memberID, serverName);
            } catch (Exception ex) {
                log.error("Cannot send mail after registration!", ex);
                request.setAttribute("mvnforum.mail.failed", "Cannot send activation email after registration!");
                //@todo: save the error message to displayed later
            }
        }
    }
}


my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.