11/*
2- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2929
3030package test ;
3131
32+ import java .io .PrintStream ;
3233import java .net .*;
3334import java .util .*;
3435import javax .naming .*;
4041
4142public class ConnectWithAuthzId {
4243
44+ static {
45+ final PrintStream out = new PrintStream (System .out , true );
46+ final PrintStream err = new PrintStream (System .err , true );
47+
48+ System .setOut (out );
49+ System .setErr (err );
50+ }
51+
4352 // LDAP capture file
4453 private static final String LDAP_CAPTURE_FILE =
4554 System .getProperty ("test.src" ) +
4655 "/src/test/test/ConnectWithAuthzId.ldap" ;
47- // LDAPServer socket
48- private static ServerSocket serverSocket ;
4956
5057 public static void main (String [] args ) throws Exception {
5158
@@ -68,67 +75,69 @@ public static void main(String[] args) throws Exception {
6875 * Launch the LDAP server with the ConnectWithAuthzId.ldap capture file
6976 */
7077
71- serverSocket = new ServerSocket (0 );
72- new Thread (new Runnable () {
73- @ Override
74- public void run () {
75- try {
76- new LDAPServer (serverSocket , LDAP_CAPTURE_FILE );
77- } catch (Exception e ) {
78- System .out .println ("ERROR: unable to launch LDAP server" );
79- e .printStackTrace ();
80- }
78+ try (ServerSocket serverSocket = new ServerSocket ()) {
79+ serverSocket .bind (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ));
80+ new Thread (new Runnable () {
81+ @ Override
82+ public void run () {
83+ try {
84+ new LDAPServer (serverSocket , LDAP_CAPTURE_FILE );
85+ } catch (Exception e ) {
86+ System .out .println ("ERROR: unable to launch LDAP server" );
87+ e .printStackTrace ();
88+ }
89+ }
90+ }).start ();
91+
92+ /*
93+ * Connect to the LDAP directory
94+ */
95+
96+ Hashtable <String ,Object > env = new Hashtable <>();
97+ env .put (Context .INITIAL_CONTEXT_FACTORY ,
98+ "com.sun.jndi.ldap.LdapCtxFactory" );
99+ URI ldapUri = new URI (args [0 ]);
100+ if (ldapUri .getPort () == -1 ) {
101+ ldapUri = new URI (ldapUri .getScheme (), null , ldapUri .getHost (),
102+ serverSocket .getLocalPort (), ldapUri .getPath (), null , null );
103+ }
104+ env .put (Context .PROVIDER_URL , ldapUri .toString ());
105+ env .put (Context .SECURITY_AUTHENTICATION , "simple" );
106+ env .put (Context .SECURITY_PRINCIPAL , "cn=admin,dc=ie,dc=oracle,dc=com" );
107+ env .put (Context .SECURITY_CREDENTIALS , "changeit" );
108+ env .put (LdapContext .CONTROL_FACTORIES ,
109+ "org.example.authz.AuthzIdResponseControlFactory" );
110+ if (args [args .length - 1 ].equalsIgnoreCase ("-trace" )) {
111+ env .put ("com.sun.jndi.ldap.trace.ber" , System .out );
81112 }
82- }).start ();
83-
84- /*
85- * Connect to the LDAP directory
86- */
87-
88- Hashtable <String ,Object > env = new Hashtable <>();
89- env .put (Context .INITIAL_CONTEXT_FACTORY ,
90- "com.sun.jndi.ldap.LdapCtxFactory" );
91- URI ldapUri = new URI (args [0 ]);
92- if (ldapUri .getPort () == -1 ) {
93- ldapUri = new URI (ldapUri .getScheme (), null , ldapUri .getHost (),
94- serverSocket .getLocalPort (), ldapUri .getPath (), null , null );
95- }
96- env .put (Context .PROVIDER_URL , ldapUri .toString ());
97- env .put (Context .SECURITY_AUTHENTICATION , "simple" );
98- env .put (Context .SECURITY_PRINCIPAL , "cn=admin,dc=ie,dc=oracle,dc=com" );
99- env .put (Context .SECURITY_CREDENTIALS , "changeit" );
100- env .put (LdapContext .CONTROL_FACTORIES ,
101- "org.example.authz.AuthzIdResponseControlFactory" );
102- if (args [args .length - 1 ].equalsIgnoreCase ("-trace" )) {
103- env .put ("com.sun.jndi.ldap.trace.ber" , System .out );
104- }
105113
106- System .out .println ("ConnectWithAuthzId: connecting to " + ldapUri );
107- LdapContext ctx = null ;
108- Control [] connectionControls = { new AuthzIdRequestControl (false ) };
109-
110- try {
111- ctx = new InitialLdapContext (env , connectionControls );
112- System .out .println ("ConnectWithAuthzId: connected" );
113- // Retrieve the response controls
114- Control [] responseControls = ctx .getResponseControls ();
115- if (responseControls != null ) {
116- for (Control responseControl : responseControls ) {
117- System .out .println ("ConnectWithAuthzId: received response" +
118- " control: " + responseControl .getID ());
119- if (responseControl instanceof AuthzIdResponseControl ) {
120- AuthzIdResponseControl authzId =
121- (AuthzIdResponseControl )responseControl ;
122- System .out .println ("ConnectWithAuthzId: identity is " +
123- authzId .getIdentity ());
114+ System .out .println ("ConnectWithAuthzId: connecting to " + ldapUri );
115+ LdapContext ctx = null ;
116+ Control [] connectionControls = { new AuthzIdRequestControl (false ) };
117+
118+ try {
119+ ctx = new InitialLdapContext (env , connectionControls );
120+ System .out .println ("ConnectWithAuthzId: connected" );
121+ // Retrieve the response controls
122+ Control [] responseControls = ctx .getResponseControls ();
123+ if (responseControls != null ) {
124+ for (Control responseControl : responseControls ) {
125+ System .out .println ("ConnectWithAuthzId: received response" +
126+ " control: " + responseControl .getID ());
127+ if (responseControl instanceof AuthzIdResponseControl ) {
128+ AuthzIdResponseControl authzId =
129+ (AuthzIdResponseControl )responseControl ;
130+ System .out .println ("ConnectWithAuthzId: identity is " +
131+ authzId .getIdentity ());
132+ }
124133 }
125134 }
126- }
127- } catch ( NamingException e ) {
128- System . err . println ( "ConnectWithAuthzId: error connecting " + e );
129- } finally {
130- if ( ctx != null ) {
131- ctx . close ();
135+ } catch ( NamingException e ) {
136+ System . err . println ( "ConnectWithAuthzId: error connecting " + e );
137+ } finally {
138+ if ( ctx != null ) {
139+ ctx . close ();
140+ }
132141 }
133142 }
134143 }
0 commit comments