⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsauthorizationbean.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.ou.kmi.buddyspace.core;

/*
 * BSAuthorizationBean.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 16 July 2002, 11:49
 */

import java.util.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

/**
 * <code>BSAuthorizationBean</code> provides athentication handling.
 * It relies on <code>BSInfoQueryBean</code>, which must be set after each
 * reconnection.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSAuthorizationBean implements PacketListener {
    private IQBean iqBean = null;
    private BSAuthState state;
    private String user = null;
    private String password = null;
    private String resource = null;
    private final String name = "Authorization";
    private Vector authListeners;
    
    /**
     * Constructor
     */
    BSAuthorizationBean() {
        state = new BSAuthState();
        state.value = BSAuthState.NOT_AUTHORIZED;
        authListeners = new Vector();
    }
    
    /**
     * Constructor, which sets existing and connected <code>IQBean</code>.
     * Then this is registered as listener for IQ packets.
     */
    BSAuthorizationBean(IQBean iqBean) {
        this();
        setIQBean(iqBean);
    }
    
    /**
     * Sets existing and connected <code>IQBean</code>.
     * Then this is registered as listener for IQ packets.
     */
    protected void setIQBean(IQBean iqBean) {
        if (this.iqBean != null)
            this.iqBean.delPacketListener(this);
        this.iqBean = iqBean;
        if (iqBean != null)
            iqBean.addPacketListener(this);
        
        state.value = BSAuthState.NOT_AUTHORIZED;
        state.servedID = null;
        state.jid = null;
    }
    
    /**
     * Returns currently used <code>IQBean</code>.
     */
    protected IQBean getIQBean() {
        return iqBean;
    }
    
    /**
     * Frees all object bindings to allow object destroy
     */
    protected void prepareToDestroy() {
        removeAllAuthListeners();
        if (iqBean != null)
            iqBean.delPacketListener(this);
        iqBean = null;
    }
    
    /**
     * Sets current state in authentication process.
     */
    private void setState(JID jid, int state, String id) {
        this.state.value = state;
        this.state.jid = jid;
        this.state.servedID = id;
        notifyAuthListeners(new BSAuthEvent(this, state));
    }
    
    /**
     * Invokes authentication of given <code>user</code>.
     */
    protected void authorize(String user, String password, String resource) {
        this.user = user;
        this.password = password;
        this.resource = resource;
        
        // returns when not set properly
        if (iqBean == null || iqBean.getConnection() == null) {
            BSCore.logEvent(name, "error: not connected");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }

        // starts loging in
        String id = new String("BS_AUTH_" + String.valueOf(BSCore.getNextID()));
        setState(null, BSAuthState.AUTHORIZING1, id);
        InfoQueryBuilder iqBuilder = new InfoQueryBuilder();
        IQAuthBuilder iqAuthBuilder = new IQAuthBuilder();
        iqAuthBuilder.setUsername(user);
        
        try {
            iqBuilder.addExtension(iqAuthBuilder.build());
            iqBuilder.setType("get");
            iqBuilder.setIdentifier(id);
            //iqBean.send((InfoQuery)iqBuilder.build());
            iqBean.getConnection().send(iqBuilder.build());
        } catch (InstantiationException e) {
            BSCore.logEvent(name, "error: IQ builder failed");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
        }
        
        BSCore.logEvent(name, "authentication phase 1");
    }
    
    /**
     * Sends user information including password.
     * This is done during the second phase of authentication process.
     */
    private void sendPassword(IQAuth extension) {
        
        // should ask user for the info according to extension
        
        //sendPassword(user, password, resource);
        sendPassword(extension, user, password, resource);
    }
    
    /**
     * This is now depricated - use the other one instead.
     * Sends user information including password.
     * This way the first phase when asking for required data is omitted
     * and data is sent directly.
     */
    protected void sendPassword(String user, String password, String resource) {
        this.user = user;
        this.password = password;
        this.resource = resource;
        
        // returns when not set properly
        if (iqBean == null || iqBean.getConnection() == null) {
            BSCore.logEvent(name, "error: not connected");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }
        // sends the authorization request
        //eventsTextArea.append("RECEIVED: iq auth result\n");
        String id = new String("BS_AUTH_" + String.valueOf(BSCore.getNextID()));
        setState(null, BSAuthState.AUTHORIZING2, id);
        InfoQueryBuilder iqBuilder = new InfoQueryBuilder();
        IQAuthBuilder iqAuthBuilder = new IQAuthBuilder();
        iqAuthBuilder.setUsername(user);
        iqAuthBuilder.setPassword(password);
        iqAuthBuilder.setResource(resource);
        
        try {
            iqBuilder.addExtension(iqAuthBuilder.build());
            iqBuilder.setType("set");
            iqBuilder.setIdentifier(id);
            //iqBean.send((InfoQuery)iqBuilder.build());
            iqBean.getConnection().send(iqBuilder.build());
        } catch (InstantiationException e) {
            BSCore.logEvent(name, "error: IQ builder failed");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }
        
        // will wait for auth response
        BSCore.logEvent(name, "authentication phase 2");
    }
    
    
    /**
     * Sends user information including password - appropriate password coding
     * is used according to given <code>IQAuth</code> extenion.
     */
    protected void sendPassword(IQAuth resultExt, String user, String password, String resource) {
        this.user = user;
        this.password = password;
        this.resource = resource;
        
        // returns when not set properly
        if (iqBean == null || iqBean.getConnection() == null) {
            BSCore.logEvent(name, "error: not connected");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }
        // sends the authorization request
        //eventsTextArea.append("RECEIVED: iq auth result\n");
        IQAuthBuilder iqAuthBuilder = getIQSetAuthBuilder(resultExt, user, password, resource);
        if (iqAuthBuilder == null) {
            BSCore.logEvent(name, "error: IQ auth builder failed");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }
        String id = new String("BS_AUTH_" + String.valueOf(BSCore.getNextID()));
        setState(null, BSAuthState.AUTHORIZING2, id);
        InfoQueryBuilder iqBuilder = new InfoQueryBuilder();
        
        try {
            iqBuilder.addExtension(iqAuthBuilder.build());
            iqBuilder.setType("set");
            iqBuilder.setIdentifier(id);
            //iqBean.send((InfoQuery)iqBuilder.build());
            iqBean.getConnection().send(iqBuilder.build());
        } catch (InstantiationException e) {
            BSCore.logEvent(name, "error: IQ builder failed");
            setState(null, BSAuthState.NOT_AUTHORIZED, null);
            return;
        }
        
        // will wait for auth response
        BSCore.logEvent(name, "authentication phase 2");
    }
    
    
    /**
     * Returns <code>IQAuthBuilder</code> with filled in information
     * according to given authentication method and user information.
     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -