📄 jnditest.java
字号:
package com.ca.commons.jndi;
import java.io.*;
import com.ca.commons.naming.*;
// REQUIRES EXTENSION JAR FILE FOR SORT CONTROL TESTING import com.sun.jndi.ldap.ctl.*;
import javax.naming.*;
import javax.naming.directory.*;
/**
* This is a general test bed interface to test the various jndi calls
* used by JXplorer. It used ldif change file format requests, and can
* be run directly from the command line, or given an ldif change file
* to process.<p>
*
* Requests are either standard ldif change file requests, such as:
* <pre>
*
* version: 1
* # Add a new entry
* dn: cn=Fiona Jensen, ou=Marketing, dc=airius, dc=com
* changetype: add
* objectclass: top
* objectclass: person
* objectclass: organizationalPerson
* cn: Fiona Jensen
* sn: Jensen
* uid: fiona
* telephonenumber: +1 408 555 1212
* jpegphoto:< file:///usr/local/directory/photos/fiona.jpg
* <pre>
* where valid 'changetype' values are [add|delete|modrdn|modify]
* (see draft-good-ldap-ldif-04.html) <p>
* This is extended by jnditest to include extra (non-standard) changetype
* commands, such as [connect|disconnect|list|search|searchOneLevel|read], to allow
* general directory access and testing.
* The 'connect' command takes the attributes; 'url', and the optional \n"+
* attributes: 'user','pwd','tracing','ldapVersion','referral' and 'useSSL' \n" +
*
* XXX coming soon - [copyTree|deleteTree|moveTree]
*
*/
public class jnditest
{
/**
* Open the appropriate input stream, and create a jdbctest object.
*/
// static String version = "1.0(build"+BuildNumber.value +")";
BufferedReader in;
PrintStream out;
DXOps myOps = null;
//AdvancedOps myAdvOps = null;
LdifUtility ldifutil = new LdifUtility(); // ldif utility used to read / write ldif files
boolean debug = true;
boolean terminating = false; // whether the program should fail, returning -1, on error
boolean printstack = false; //TE: whether to print the stack trace.
public static int OK = 0;
public static int ERROR = -1;
public static int FINISHED = 1;
public static void main (String args[])
{
String fileName = null;
String url = null;
String user = null;
String pwd = null;
String version = "3";
String referral = "follow";
boolean useSSL = false;
boolean tracing = false;
boolean debugFlag = true;
boolean terminateFlag = false;
boolean printstackFlag = false;
int i=0;
try
{
while (i<args.length)
{
String arg = (args[i].charAt(0) != '-')?args[i]:args[i].substring(1);
switch(arg.charAt(0))
{
case '?':
case 'H':
case 'h': if (args.length>i+1)
printHelp(args[i+1]);
else
printHelp(null);
return; // print and exit program
case 'C':
case 'c': url = args[++i]; break;
case 'D':
case 'd': debugFlag = true; break;
case 'E':
case 'e': terminateFlag = true; break;
case 'F':
case 'f': fileName = args[++i]; break;
case 'P':
case 'p': pwd = args[++i]; break;
case 'R':
case 'r': referral = args[++i]; break;
case 'S':
case 's': useSSL = true; break;
case 'T':
case 't': tracing = true; break;
case 'U':
case 'u': user = args[++i]; break;
case 'V':
case 'v': version = args[++i]; break;
case 'X':
case 'x': printstackFlag = true; break;
default : System.out.println("\n\nInvalid command line argument: -" + arg);
printHelp(null);
return; // print and exit program
}
i++;
}
}
catch (Exception e)
{
System.out.println("Error reading command line arguments.");
printHelp("");
System.exit(-1);
}
// create jnditest object
jnditest tester = new jnditest(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag);
// use jnditest object to process data until input finished.
tester.processInput();
tester.out.println("\nnormal finish\n");
}
public static void printHelp(String subject)
{
if ("full".equalsIgnoreCase(subject)) {printFullHelp(); return; }
if ("changetype".equalsIgnoreCase(subject)) {printChangeTypeHelp(); return; }
System.out.println(""+
"\n\njndiTest is a small utility/test program designed to check the ldap/jndi\n"+
"functionality of JXplorer. It reads input or files in ldif change file format\n"+
"with some extra commands added to test searching and attribute reading.\n\n"+
" usage: java jnditest [<options>]\n\n" +
" options:\n" +
" -c an optional connection url of the form ldap:\\host:port\n" +
" -d debug (verbose) mode\n" +
" -e exit on error, returning -1\n" +
" -f filename name of an ldif changes input file\n" +
" -h [subject] this help message [full|changetype]\n" +
" -p password an option password\n" +
" -r referral the jndi referral type [follow|ignore|throw]\n" +
" -t set BER tracing on\n" +
" -u userdn an optional user dn\n" +
" -v set ldap version (default 3)\n\n" +
" -x print stack trace\n\n" +
"in addition to normal ldif changes commands (cf draft-good-ldap-ldif-04.html) a:\n" +
"new keyword 'actiontype' with values [list|search|read|connect|disconnect]\n" +
"is defined, with a usage similar to 'changetype'");
}
protected static void printFullHelp()
{
printChangeTypeHelp();
}
protected static void printChangeTypeHelp()
{
System.out.println("" +
" * This is a general test bed interface to test the various jndi calls \n"+
" * used by JXplorer. It used ldif change file format requests, and can \n"+
" * be run directly from the command line, or given an ldif change file\n"+
" * to process.<p>\n"+
" *\n"+
" * Requests are either standard ldif change file requests, such as:\n"+
" *\n"+
" * version: 1\n"+
" * # Add a new entry\n"+
" * changetype: add\n"+
" * objectclass: top\n"+
" * objectclass: person\n"+
" * objectclass: organizationalPerson\n"+
" * cn: Fiona Jensen\n"+
" * sn: Jensen\n"+
" * uid: fiona\n"+
" * telephonenumber: +1 408 555 1212\n"+
" * jpegphoto:< file:///usr/local/directory/photos/fiona.jpg\n"+
" * <pre>\n"+
" * where valid 'changetype' values are [add|delete|modrdn|modify] \n"+
" * This is extended by jnditest to include extra (non-standard) changetype\n"+
" * commands, such as [connect|disconnect|list|search|searchOneLevel|read], to allow\n"+
" * general directory access and testing.\n"+
" * The 'connect' command takes the attributes; 'url', and the optional \n"+
" * attributes: 'user','pwd','tracing','ldapVersion','referral' and 'useSSL' \n" +
" *\n"+
" * XXX coming soon - [copyTree|deleteTree|moveTree]\n"+
" *");
}
/**
* Constructs a jnditest object, opening a connection (if non-null input is
* passed for connection opening) and opening an input file (if a non-null
* file name is passed).<p>
*
* All the following parameters can be null.
*
* @param fileName the name of an LDIF changes input file.
* @param url a url of the form ldap://hostname:portnumber.
* @param user a user to bind to the directory as.
* @param pwd the user's password.
* @param tracing whether to set BER tracing on or not.
* @param version the LDAP Version (2 or 3) being used.
* @param debugFlag echo all system statement.
* @param terminateFlag exit on error, returning -1.
* @param referral the jndi referral type [follow|ignore|throw].
* @param useSSL to use SSL.
* @param printstackFlag whether to print a stack trace.
*/
public jnditest(String fileName, String url, String user, String pwd, boolean tracing, String version, boolean debugFlag, boolean terminateFlag, String referral, boolean useSSL, boolean printstackFlag)
{
out = System.out; // may want to make this configurable in future :-)
debug = debugFlag;
terminating = terminateFlag;
printstack = printstackFlag;
// open a connection (if we've been given one)
if (url != null)
openConnection(url, user, pwd, tracing, version, referral, useSSL);
// open an input stream - file if we have one, console if not...
if (fileName != null)
in = openFile(fileName);
else
in = new BufferedReader(new InputStreamReader(System.in));
}
/**
* Open an ldap connection using jndi, via the standard JXplorer classes
* (DXOps.java, which wraps BasicOps.java).
*
* @param url a url of the form ldap://hostname:portnumber
* @param user a user to bind to the directory as.
* @param pwd the user's password.
* @param tracing whether to set BER tracing on or not
* @param version the LDAP Version (2 or 3) being used.
* @param referralType the jndi referral type [follow|ignore|throw].
* @param useSSL to use SSL.
*/
public void openConnection(String url, String user, String pwd, boolean tracing, String version, String referralType, boolean useSSL)
{
if (referralType == null) referralType = "follow";
if ("ignorefollowthrow".indexOf(referralType)==-1)
{
error("unknown referraltype " + referralType, null);
referralType = "follow";
}
try
{
if (debug) out.println("opening connection with :\n url: " + url + "\n user: " + user + "\n pwd: " + pwd + "\n tracing: " + tracing + "\n version: " + version + "\n referral: " + referralType);
if ((url == null) || (url.length()==0))
{ error("Unable to open connection - no url supplied", null); return;}
if ((version == null) || (version.length()==0)) version = "3"; // default to ldap version 3
char[] password = (pwd==null||pwd.length()==0)?null:pwd.toCharArray();
int versn = Integer.parseInt(version);
ConnectionData connectionData = new ConnectionData(versn, url, user, password, tracing, null, null, false, null, null, null, null, null, null, false, null);
if(url.startsWith("http://"))
connectionData.setProtocol(ConnectionData.DSML);
else if(url.startsWith("ldap://"))
connectionData.setProtocol(ConnectionData.LDAP);
else
error("Unable to open connection due to invalid url - url must start with either http:// or ldap://", null);
DirContext ctx = BasicOps.openContext(connectionData);
if (ctx != null)
myOps = new DXOps(ctx);
if (myOps == null)
error("unable to open connection " + url, null);
else
if (debug) out.println("connection " + url + " open. ");
}
catch (Exception e)
{
error("error opening connection " + url + "\n ", e);
}
}
/**
* Opens a text file as a Buffered Reader.
*
* @param fileName the name of the file to open.
* @return the Input Stream.
*/
public BufferedReader openFile(String fileName)
{
try
{
File myFile = new File(fileName);
ldifutil.setFileDir(myFile.getParent());
return new BufferedReader(new InputStreamReader(new FileInputStream(myFile)));
}
catch (Exception e)
{
error("unable to open file : " + fileName + "\n ", e);
System.exit(-1);
}
return null;
}
/**
* Reads input as an ldif entry at a time
* (i.e. as a series of colon seperated att:value pairs, until
* a blank line or eof is read).
*/
public void processInput()
{
// start the infinite loop...
try
{
DXEntry commandSet;
while ((commandSet = ldifutil.readLdifEntry(in))!=null)
{
if (commandSet == null)
error ("internal error in jnditest - parsed ldif command set null", null);
if (processCommand(commandSet) == FINISHED)
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -