package cn.com.qmth.stmms.common.domain;

import org.apache.commons.lang.StringUtils;

public class AuthInfo {

    private static final String FIELD_JOINER = ";";

    private static final String VALUE_JOINER = "=";

    private static final String LOGINNAME_KEY = "loginname";

    private static final String PASSWORD_KEY = "password";

    private String loginname;

    private String password;

    public String getLoginname() {
        return loginname;
    }

    private void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public String getPassword() {
        return password;
    }

    private void setPassword(String password) {
        this.password = password;
    }

    private AuthInfo() {
    }

    public static AuthInfo parse(String input) {
        AuthInfo info = new AuthInfo();
        String[] fields = StringUtils.split(input, FIELD_JOINER);
        if (fields != null && fields.length > 0) {
            for (String field : fields) {
                String[] values = StringUtils.split(field, VALUE_JOINER);
                if (values != null && values.length == 2) {
                    if (values[0].equals(LOGINNAME_KEY)) {
                        info.setLoginname(values[1]);
                    } else if (values[0].equals(PASSWORD_KEY)) {
                        info.setPassword(values[1]);
                    }
                }
            }
        }
        return info.getLoginname() != null && info.getPassword() != null ? info : null;
    }
}
