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

📄 ldapsavingutility.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         resetLDAPTree();
       } catch (ACCreationException acce) {
         issrg.utils.Util.bewail(
             "Can not access the preferred LDAP server \n" +
                                     acce.getMessage(), acce, dialog);
       }
    }

    return getDialog(dialogMode!=SAVE_MODE?"":dn, lastURL, lastLogin, dialogMode);
  }

  public void save(byte [] ac) throws ACCreationException{
    String dn = DN.getText();

    try{
      DirContext root = connectTo(URL.getText(), Login.getText(),
                   new String(Password.getPassword()), Anonymous.isSelected());

      // now let's update the DN by substracting the base DN from it
      dn = LDAP_DIT.dnWithoutBaseDN(root, dn);

      Attribute attr = loadACs(dn);
      attr.add((Object)ac);

      BasicAttributes bas = new BasicAttributes();
      bas.put(attr);

      root.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, bas);

      lastDN=DN.getText();
      lastURL=URL.getText();
      lastLogin=Login.getText();
    }catch (NamingException ne){
      throw new ACCreationException("Cannot access "+dn+" entry\n" +
                                    "Please check URL, Login and Password", ne);
    }catch (NoClassDefFoundError ncdfe){
      throw new ACCreationException("Cannot load the required class.\n" +
                        "Are there any classes missing from the classpath?\n" +
                                 "Class name: ["+ncdfe.getMessage()+"]", ncdfe);
    }catch (Exception e){
      throw new ACCreationException("Internal error", e);
    }
  }

  /**
   * Resets the tree view of the LDAP. It uses URL text field to find the URL
   * of the LDAP, Login and Password to authenticate and Anonymous tickbox to
   * ignore the password, if it is provided.
   */
  public void resetLDAPTree() throws ACCreationException{
    try{
      DirContext root = connectTo(URL.getText(), Login.getText(),
                   new String(Password.getPassword()), Anonymous.isSelected());

      ldapDit.setRoot(root);
    }catch (NamingException ne){
      clearLdapDNTree();
      throw new ACCreationException("Cannot access "+URL.getText()+"\n" + ne.getMessage(), ne);
    }catch (NoClassDefFoundError ncdfe) {
      throw new ACCreationException("Cannot load the required class.\n" +
                          "Are there any classes missing from the classpath?\n"
                              +"Class name: ["+ncdfe.getMessage()+"]", ncdfe);
    }
  }

  private void clearLdapDNTree() throws ACCreationException{
     try {
        ldapDit.setRoot((String)null);
        acv.setACs((AttributeCertificate [])null);
     } catch(NamingException ne) {
         throw new ACCreationException("Can not Clear LdapDN tree\n"
                                                        + ne.getMessage(), ne);
     }
  }

  /**
   * Load all ACs (Attribute Certificate) from the specified entry.
   *
   * @param dn the specifed DN to retrive ACs.
   *
   * @return the attribute certificates (inside the Attribute)
   */

  public Attribute loadACs(String dn) throws ACCreationException {
    try {
      String[] attrIds = new String[] {acType};
      DirContext root = connectTo(URL.getText(), Login.getText(),
                   new String(Password.getPassword()), Anonymous.isSelected());

      Attributes atts = root.getAttributes(LDAP_DIT.dnWithoutBaseDN(root, dn), attrIds);
      NamingEnumeration ne = atts.getAll();

      if(!ne.hasMoreElements()) {
          return new BasicAttribute(acType); // create an empty Attribute (no values)
      }

      return (Attribute)(ne.next());
    } catch (NamingException ne) {
      throw new ACCreationException("Cannot access " + dn
                                    + " entry\n" + ne.getMessage(), ne);
    }
  }

  public void approveSelection(){
    if (acv.getSelectedAC()==null && dialogMode!=SAVE_MODE && acv.getSelectedIndex() == -1){        
        issrg.utils.Util.bewail(
            "X.509 Attribute Certificate could not be loaded: No AC was selected",
            null, dialog);       
    }else if (acv.getSelectedAC()==null && dialogMode==LOAD_MODE) {
          issrg.utils.Util.bewail(
            "Broken X.509 Attribute Certificate could not be loaded",
            null, dialog);       
      } else
    
    {
      if (dialogMode!=LOAD_MODE && Password.getPassword().length==0){ // empty password in save or revocation mode is suspicious
        int option = JOptionPane.showConfirmDialog(dialog, "No Password was specified to access LDAP. This may cause authentication failure. Proceed?", 
                                 "Confirm", JOptionPane.YES_NO_OPTION);

        if (option!=JOptionPane.YES_OPTION) return; // don't approve selection, if clicked NO or closed the Confirmation dialog
      } 

      super.approveSelection();
    }
  }

  public issrg.ac.AttributeCertificate load() throws ACCreationException{
    lastDN=DN.getText();
    lastURL=URL.getText();
    lastLogin=Login.getText();

    issrg.ac.AttributeCertificate ac = acv.getSelectedAC();

    if(ac ==null && acv.getSelectedIndex() == -1)
       throw new ACCreationException("No AC was selected");

    return ac;
  }

  public issrg.ac.AttributeCertificate revoke() throws ACCreationException{
    return load();
  }

  /**
   * Deletes the selected Attribute Certificate from the entry.
   *
   * <p>If the entry contains attributes other than well-formed X.509 ACs under
   * 2.5.4.58 attributeCertificateAttribute, the behaviour is undefined (may
   * spoil the contents of the entry).
   */
  public void delete() throws ACCreationException {
    AttributeCertificate [] acs = acv.getACs();
    int idx = acv.getSelectedIndex();

    if (acs==null || idx<0 || idx>=acs.length) throw new ACCreationException("Cannot delete AC: no AC was selected");
    JDialog splash=null;
    splash=issrg.utils.Util.showSplash(null, "Please, wait...", "Trying to revoke X.509 Attribute Certificate...");    

    String dn = DN.getText();

    try{
      DirContext root = connectTo(URL.getText(), Login.getText(),
                   new String(Password.getPassword()), Anonymous.isSelected());
      dn = LDAP_DIT.dnWithoutBaseDN(root, dn);

      Attribute attr = new BasicAttribute(acType);
      for (int i=0; i<acs.length; i++){
        if (i==idx) continue; // skip the AC that has been selected       
        attr.add(acs[i].getEncoded()); // coding exception will be caught later, no deletion will occur
      }

      BasicAttributes bas = new BasicAttributes();
      bas.put(attr);

      root.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, bas);
      if (splash!=null){
            splash.dispose();
      }
    }catch (NamingException ne){
      throw new ACCreationException("Cannot access "+dn+" entry\n" +
                                    "Please check URL, Login and Password", ne);
    }catch (NoClassDefFoundError ncdfe){
      throw new ACCreationException("Cannot load the required class.\n" +
                        "Are there any classes missing from the classpath?\n" +
                                 "Class name: ["+ncdfe.getMessage()+"]", ncdfe);
    }catch (Exception e){
      throw new ACCreationException("Internal error", e);
    }
  }

  /**
   * This method constructs the panel with the controls with URI/DN/Login/Password fields
   * URL, DN, Login and Password can be filled in, and Anonymous can be ticked.
   */
  private Component getDialog(String dn, String url, String login, int dialogMode){
    DN.setText(dn);
    DN.setEditable(dialogMode!=SAVE_MODE);
    URL.setText(url==null?"":url);
    Login.setText(login==null?"":login);
    Password.setText(""); // always clear the password field

    scLdap.setVisible(dialogMode!=SAVE_MODE);
    acvPanel.setVisible(dialogMode!=SAVE_MODE);
    connect.setVisible(dialogMode!=SAVE_MODE);

    return attachControlButtons(content, dialogMode);
  }

  /**
   * Just a wrapper for establishing connection to the LDAP DIT.
   */
  private DirContext connectTo(String URL, String login, String password, boolean anonymous) throws NamingException{
      java.util.Hashtable e = new java.util.Hashtable();
      e.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      e.put(Context.PROVIDER_URL, URL);
      e.put("java.naming.ldap.version", "3");
      e.put("java.naming.ldap.attributes.binary", acType); // otherwise it may sometimes retrieve it as a String :-(, stupid thing

      if (login!=null && password!=null && password.intern()!="" && !anonymous){
        e.put(Context.SECURITY_AUTHENTICATION, "simple");
        e.put(Context.SECURITY_PRINCIPAL, login);
        e.put(Context.SECURITY_CREDENTIALS, password);
      }

      return new InitialDirContext(e);
  }

  /**
   * Reads in the environment settings: cfg variables, and sets them if needed.
   */
  private void initDefaults(java.util.Map env){
    if (env == null)return;

    if (lastURL == null) lastURL = (String) env.get(this.
        LDAP_SAVING_UTILITY_LDAP_PROVIDER);
    if (lastLogin == null) lastLogin = (String) env.get(this.
        LDAP_SAVING_UTILITY_LOGIN);

    String s = (String) env.get(LDAP_SAVING_UTILITY_AC_TYPE);
    if (s != null) acType = s;
   }


   public void select(LDAP_DIT sender) {
     try {
       DirContext root = ldapDit.getSelectedNode();
       String dn = root.getNameInNamespace();
       DN.setText(dn);

       Attribute acs=loadACs(dn);
       AttributeCertificate[] v = new AttributeCertificate[acs.size()];
       //       boolean allACsOK = true;
       for(int i = 0; i <acs.size(); i++){
         try{
           byte[] tt1 = (byte[]) acs.get(i);
           v[i]=AttributeCertificate.guessEncoding(tt1);
         }catch (Exception e){ // if anything is wrong with the AC, skip it
//           allACsOK=false;
             v[i]=null; 
         }
       }
       acv.setACs(v);
//       if (!allACsOK){
//         communicationError(new Exception("Some of the Attribute Certificates in the entry are malformed"));
//       }
     } catch (NamingException ne) {
         communicationError(ne);
     }catch (ACCreationException acce) {
         communicationError(acce);
     }
   }

   public void doubleclick(LDAP_DIT sender) {
   }

   public void unselect(LDAP_DIT sender) {
   }

   public void communicationError(Throwable th) {
     issrg.utils.Util.bewail(
                         "A Communication error has been thrown \n" +
                                            th.getMessage(), th, dialog);
   }
}

⌨️ 快捷键说明

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