InfoDirectory使用手引書
目次 索引 前ページ次ページ

第3部 SDK編 > 第6章 アプリケーション開発環境(JNDI) > 6.4 サンプルプログラム

6.4.3 サンプルプログラムの紹介

 ここでは、ディレクトリ検索を行う場合のサンプルプログラムを紹介します。このサンプルは、指定したディレクトリサーバに対してSSLを使用した簡易認証による検索を行い、検索結果のエントリ情報を出力します。

 これ以外のサンプルプログラムについては、ソースファイルを直接参照してください。

image

jndi1.1

import java.util.Hashtable;
import java.util.Enumeration;

import javax.naming.*;
import javax.naming.directory.*;
import com.sun.jndi.ldap.*;
import com.sun.jndi.ssl.*;// for SSL

class Search {

/*
* config parameter
*/
public static final String ldapurl = "ldap://localhost:636";
public static final String binddn = "cn=admin";
public static final String password = "admin";
public static final String s_base = "c=jp";
public static final String filter = "cn=taro";

public static void main(String[] args) {

   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

   /* Specify host and port to use for directory service */
   env.put(Context.PROVIDER_URL, ldapurl );
   env.put(Context.SECURITY_AUTHENTICATION, "simple");
   env.put(Context.SECURITY_PRINCIPAL, binddn );
   env.put(Context.SECURITY_CREDENTIALS, password);
   // for SSL
   env.put("java.naming.ldap.factory.socket", "FjSSLLSocket" );
   env.put(SSLSocket.SSL_VERSION , "3" );
   env.put(SSLSocket.CRYPT , "RSA-3DES-SHA:RSA-DES-SHA");
   env.put(SSLSocket.SLOT_PATH , "c:\\ssl_env\\slot" );
   env.put(SSLSocket.TKN_LBL , "token1" );
   env.put(SSLSocket.TKN_PWD , "slot123" );
   env.put(SSLSocket.CERT_PATH , "c:\\ssl_env\\sslcert" );
   env.put(SSLSocket.USER_CERT_NAME , "client_cert" );
   
   try
   {
      /* get a handle to an Initial DirContext */
      DirContext ctx = new InitialDirContext(env);
      
      /* specify search constraints to search subtree */
      SearchControls constraints = new SearchControls();
      constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

      /* search for all entries with commonname of taro */
      NamingEnumeration results = ctx.search(s_base, filter, constraints);

      /* for each entry print out name + all attrs and values */
      while (results != null && results.hasMore()) {
         SearchResult si = (SearchResult)results.next();

         /* print its name */
         System.out.println("name: " + si.getName());

         Attributes attrs = si.getAttributes();
         if (attrs == null) {
            System.out.println("No attributes");
         } else {
            /* print each attribute */
            for (NamingEnumeration ae = attrs.getAll();
               ae.hasMoreElements();) {
               Attribute attr = (Attribute)ae.next();
               String attrId = attr.getID();

               /* print each value */
               for (Enumeration vals = attr.getAll();
                  vals.hasMoreElements();
                  System.out.println(attrId + ": " + vals.nextElement()));
            }
         }
         System.out.println();
      }
      ctx.close();
   }
   catch(SSLException se)
   {
      System.out.println("Search example failed.");
      int errorType = se.getType();
      if(errorType == SSLException.CLNT_CERT_EXPIRED)
      {
         System.out.println("Client's cert is expired");
      }
      else if(errorType == SSLException.MEMORY_ERROR)
      {
         System.out.println("Parameter Error in SSL Environment File");
      }
      else
      {
         se.printStackTrace();
      }
   }
   catch(NamingException ne)
   {
      System.out.println("Search example failed.");
      System.out.println(ne);
      ne.printStackTrace();
   }
   catch(Exception e)
   {
      System.out.println(e.getMessage());
      System.out.println(e.getLocalizedMessage());
      e.printStackTrace();
   }
}

}

jndi1.2

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;
import com.sun.jndi.ldap.*;

class Search {

   /*
    * 実行環境に合わせて以下のパラメタを変更してください。
    */
   public static final String ldapurl = "ldap://localhost:636";
   public static final String binddn = "cn=admin";
   public static final String password = "admin";
   public static final String sslenvfile = "c:\\ssl_env\\sslconfig.cfg";
   public static final String ssllogdir = "c:\\ssl_env\\";
   public static final String s_base = "c=jp";
   public static final String filter = "cn=taro";

public static void main(String[] args) {

   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

   /* 環境プロパティのセット */
   env.put(Context.PROVIDER_URL, ldapurl );
   env.put(Context.SECURITY_AUTHENTICATION, "simple" );
   env.put(Context.SECURITY_PRINCIPAL, binddn );
   env.put(Context.SECURITY_CREDENTIALS, password );
   env.put("java.naming.referral", "follow" );
   /* SSL用の環境プロパティのセット */
   env.put("java.naming.ldap.factory.socket",    "com.fujitsu.ssl.FjSSLSocketFactory");
   env.put(Context.SECURITY_PROTOCOL, "ssl" );
   
   //システムプロパティの取得
   Properties prop = System.getProperties();
   
   prop.put("user.sslenvfile", sslenvfile);
   prop.put("user.ssllogdir", ssllogdir );

   try
   {
      /* ディレクトリサーバに接続 */
      DirContext ctx = new InitialDirContext(env);

      /* 検索範囲の指定 */
      SearchControls constraints = new SearchControls();
      constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

      /* 検索開始位置:c=jp、検索フィルタ:cn=taro で検索実行 */
      NamingEnumeration results = ctx.search(s_base, filter, constraints);

      /* 検索結果の表示 */
      while (results != null && results.hasMore()) {
         SearchResult si = (SearchResult)results.next();
         System.out.println("name: " + si.getName());
         Attributes attrs = si.getAttributes();
         if (attrs == null) {
            System.out.println("No attributes");
         } else {
            /* print each attribute */
            for (NamingEnumeration ae = attrs.getAll();
               ae.hasMoreElements(); ) {
               Attribute attr = (Attribute)ae.next();
               String attrId = attr.getID();
               for (Enumeration vals = attr.getAll();
               vals.hasMoreElements();
               System.out.println(attrId + ": " + vals.nextElement()));
            }
         }
         System.out.println();
      }
      ctx.close();
   }
   catch(NamingException ne)
   {
      Throwable msg = ne.getRootCause();
      String msgStr = null;
      int ssl_error = -1;

      if ( msg != null ) {
         /* エラーメッセージの取得 */
         msgStr = msg.toString();
         /* エラーを取得するキー検索 */
         ssl_error = msgStr.indexOf("FjSSLSocket");
      }
      /* SSLのときの処理 */
      if ( ssl_error != -1 ) {
         int index1 = msgStr.indexOf("errtype=") + "errtype=".length();
         if ( index1 != -1 ) {
            /* エラータイプの取得 */
            String error = msgStr.substring(index1, index1 + 2);
            /* エラータイプの表示 */
            System.out.println("SSL Error code : " + error);
         }
      }
      System.out.println("Search example failed.");
      ne.printStackTrace();
   }
   catch(Exception e)
   {
      System.out.println(e.getMessage());
      System.out.println(e.getLocalizedMessage());
      e.printStackTrace();
   }
}

}

image

jndi1.1

import java.util.Hashtable;
import java.util.Enumeration;

import javax.naming.*;
import javax.naming.directory.*;
import com.sun.jndi.ldap.*;
import com.sun.jndi.ssl.*;// for SSL

class Search {

/*
* config parameter
*/
public static final String ldapurl = "ldap://localhost:636";
public static final String binddn = "cn=admin";
public static final String password = "admin";
public static final String s_base = "c=jp";
public static final String filter = "cn=taro";

public static void main(String[] args) {

   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

   /* Specify host and port to use for directory service */
   env.put(Context.PROVIDER_URL, ldapurl );
   env.put(Context.SECURITY_AUTHENTICATION, "simple");
   env.put(Context.SECURITY_PRINCIPAL, binddn );
   env.put(Context.SECURITY_CREDENTIALS, password);
   // for SSL
   env.put("java.naming.ldap.factory.socket", "FjSSLLSocket" );
   env.put(SSLSocket.SSL_VERSION , "3" );
   env.put(SSLSocket.CRYPT , "RSA-3DES-SHA:RSA-DES-SHA");
   env.put(SSLSocket.SLOT_PATH , "/home/slot" );
   env.put(SSLSocket.TKN_LBL , "token1" );
   env.put(SSLSocket.TKN_PWD , "slot123" );
   env.put(SSLSocket.CERT_PATH , "/home/sslcert" );
   env.put(SSLSocket.USER_CERT_NAME , "client_cert" );
   
   try
   {
      /* get a handle to an Initial DirContext */
      DirContext ctx = new InitialDirContext(env);
      
      /* specify search constraints to search subtree */
      SearchControls constraints = new SearchControls();
      constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

      /* search for all entries with commonname of taro */
      NamingEnumeration results = ctx.search(s_base, filter, constraints);

      /* for each entry print out name + all attrs and values */
      while (results != null && results.hasMore()) {
         SearchResult si = (SearchResult)results.next();

         /* print its name */
         System.out.println("name: " + si.getName());

         Attributes attrs = si.getAttributes();
         if (attrs == null) {
            System.out.println("No attributes");
         } else {
            /* print each attribute */
            for (NamingEnumeration ae = attrs.getAll();
               ae.hasMoreElements();) {
               Attribute attr = (Attribute)ae.next();
               String attrId = attr.getID();

               /* print each value */
               for (Enumeration vals = attr.getAll();
                  vals.hasMoreElements();
                  System.out.println(attrId + ": " + vals.nextElement()));
            }
         }
         System.out.println();
      }
      ctx.close();
   }
   catch(SSLException se)
   {
      System.out.println("Search example failed.");
      int errorType = se.getType();
      if(errorType == SSLException.CLNT_CERT_EXPIRED)
      {
         System.out.println("Client's cert is expired");
      }
      else if(errorType == SSLException.MEMORY_ERROR)
      {
         System.out.println("Parameter Error in SSL Environment File");
      }
      else
      {
         se.printStackTrace();
      }
   }
   catch(NamingException ne)
   {
      System.out.println("Search example failed.");
      System.out.println(ne);
      ne.printStackTrace();
   }
   catch(Exception e)
   {
      System.out.println(e.getMessage());
      System.out.println(e.getLocalizedMessage());
      e.printStackTrace();
   }
}

}

jndi1.2

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;
import com.sun.jndi.ldap.*;

class Search {

   /*
    * 実行環境に合わせて以下のパラメタを変更してください。
    */
   public static final String ldapurl = "ldap://localhost:636";
   public static final String binddn = "cn=admin";
   public static final String password = "admin";
   public static final String sslenvfile = "/SslEnv/sslconfig.cfg";
   public static final String ssllogdir = "/SslEnv";
   public static final String s_base = "c=jp";
   public static final String filter = "cn=taro";

public static void main(String[] args) {

   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

   /* 環境プロパティのセット */
   env.put(Context.PROVIDER_URL, ldapurl );
   env.put(Context.SECURITY_AUTHENTICATION, "simple" );
   env.put(Context.SECURITY_PRINCIPAL, binddn );
   env.put(Context.SECURITY_CREDENTIALS, password );
   env.put("java.naming.referral", "follow" );
   /* SSL用の環境プロパティのセット */
   env.put("java.naming.ldap.factory.socket",    "com.fujitsu.ssl.FjSSLSocketFactory");
   env.put(Context.SECURITY_PROTOCOL, "ssl" );
   
   //システムプロパティの取得
   Properties prop = System.getProperties();
   
   prop.put("user.sslenvfile", sslenvfile);
   prop.put("user.ssllogdir", ssllogdir );

   try
   {
      /* ディレクトリサーバに接続 */
      DirContext ctx = new InitialDirContext(env);

      /* 検索範囲の指定 */
      SearchControls constraints = new SearchControls();
      constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

      /* 検索開始位置:c=jp、検索フィルタ:cn=taro で検索実行 */
      NamingEnumeration results = ctx.search(s_base, filter, constraints);

      /* 検索結果の表示 */
      while (results != null && results.hasMore()) {
         SearchResult si = (SearchResult)results.next();
         System.out.println("name: " + si.getName());
         Attributes attrs = si.getAttributes();
         if (attrs == null) {
            System.out.println("No attributes");
         } else {
            /* print each attribute */
            for (NamingEnumeration ae = attrs.getAll();
               ae.hasMoreElements(); ) {
               Attribute attr = (Attribute)ae.next();
               String attrId = attr.getID();
               for (Enumeration vals = attr.getAll();
               vals.hasMoreElements();
               System.out.println(attrId + ": " + vals.nextElement()));
            }
         }
         System.out.println();
      }
      ctx.close();
   }
   catch(NamingException ne)
   {
      Throwable msg = ne.getRootCause();
      String msgStr = null;
      int ssl_error = -1;

      if ( msg != null ) {
         /* エラーメッセージの取得 */
         msgStr = msg.toString();
         /* エラーを取得するキー検索 */
         ssl_error = msgStr.indexOf("FjSSLSocket");
      }
      /* SSLのときの処理 */
      if ( ssl_error != -1 ) {
         int index1 = msgStr.indexOf("errtype=") + "errtype=".length();
         if ( index1 != -1 ) {
            /* エラータイプの取得 */
            String error = msgStr.substring(index1, index1 + 2);
            /* エラータイプの表示 */
            System.out.println("SSL Error code : " + error);
         }
      }
      System.out.println("Search example failed.");
      ne.printStackTrace();
   }
   catch(Exception e)
   {
      System.out.println(e.getMessage());
      System.out.println(e.getLocalizedMessage());
      e.printStackTrace();
   }
}

}


目次 索引 前ページ次ページ

All Rights Reserved, Copyright (C) 富士通株式会社 2003