33import java .io .BufferedReader ;
44import java .io .IOException ;
55import java .io .InputStreamReader ;
6+ import java .net .PasswordAuthentication ;
67import java .net .URI ;
78import java .net .URISyntaxException ;
89import java .util .Properties ;
2627import com .kaazing .net .http .HttpRedirectPolicy ;
2728import com .kaazing .net .ws .WebSocketFactory ;
2829
30+ import com .kaazing .net .auth .BasicChallengeHandler ;
31+ import com .kaazing .net .auth .ChallengeHandler ;
32+ import com .kaazing .net .auth .LoginHandler ;
33+
2934public class JavaJMSClientDemo {
3035 private InitialContext jndiInitialContext ;
3136 private ConnectionFactory connectionFactory ;
@@ -47,7 +52,7 @@ public JavaJMSClientDemo(URI url, String login, String password) throws JMSExcep
4752 }
4853
4954 public void handleConnection () throws JMSException {
50- if (login .equals ("" ) && password .equals ("" )){
55+ if (login .equals ("" ) && password .equals ("" )) {
5156 System .out .println ("Connecting to: " + url + ". Please wait!" );
5257 }else {
5358 System .out .println ("Connecting to: " + url + " with username: " + login + "and password: " + password + ". Please wait!" );
@@ -64,15 +69,16 @@ public void handleConnection() throws JMSException {
6469 connectionFactory = (ConnectionFactory ) jndiInitialContext .lookup ("ConnectionFactory" );
6570 } catch (NamingException e ) {
6671 throw new RuntimeException ("Error locating connection factory for JMS!" , e );
67- }
72+ }
6873 JmsConnectionFactory jmsConnectionFactory = (JmsConnectionFactory ) connectionFactory ;
6974 jmsConnectionFactory .setGatewayLocation (url );
7075 WebSocketFactory webSocketFactory = jmsConnectionFactory .getWebSocketFactory ();
7176 webSocketFactory .setDefaultRedirectPolicy (HttpRedirectPolicy .ALWAYS );
77+ webSocketFactory .setDefaultChallengeHandler (createChallengeHandler ());
7278 try {
7379 connection = connectionFactory .createConnection (login , password );
7480 } catch (JMSException e ) {
75- System .out .println ("EXCEPTION: " + e .getMessage () + ".\n " +
81+ System .out .println ("EXCEPTION: " + e .getMessage () + ".\n " +
7682 " Please check the inputed URI, username, password and restart the demo!" );
7783 throw new RuntimeException ("Error connecting to gateway with " + url .toString () + ", credentials " + login + "/" + password , e );
7884 }
@@ -120,7 +126,7 @@ public void handleConnection() throws JMSException {
120126 } catch (JMSException e ) {
121127 e .printStackTrace ();
122128 }
123- } else {
129+ }else {
124130 System .err .println ("Received message of an unexpected type " + message .getClass ().getName ());
125131 }
126132 });
@@ -129,7 +135,6 @@ public void handleConnection() throws JMSException {
129135 connection .stop ();
130136 connection .close ();
131137 throw new RuntimeException ("Cannot create messages listener for subscription topic " + subTopicName , e );
132-
133138 }
134139 Destination pubDestination ;
135140 try {
@@ -139,7 +144,6 @@ public void handleConnection() throws JMSException {
139144 session .close ();
140145 connection .stop ();
141146 connection .close ();
142-
143147 throw new RuntimeException ("Cannot locate publishing topic " + pubTopicName , e );
144148 }
145149 try {
@@ -160,7 +164,6 @@ public void disconnect() throws JMSException {
160164 connection .stop ();
161165 connection .close ();
162166 System .out .println ("Kaazing Java JMS Demo terminated successfully" );
163-
164167 }
165168
166169 public void sendMessage (String message ) {
@@ -173,4 +176,25 @@ public void sendMessage(String message) {
173176 }
174177 System .out .println ("-> MESSAGE PUBLISHED: " + message );
175178 }
179+
180+ private ChallengeHandler createChallengeHandler () {
181+ final LoginHandler loginHandler = new LoginHandler () {
182+ private String username ;
183+ private char [] passwordLH ;
184+ @ Override
185+ public PasswordAuthentication getCredentials () {
186+ try {
187+ username = login ;
188+ passwordLH = password .toCharArray ();
189+
190+ }catch (Exception e ) {
191+ e .printStackTrace ();
192+ }
193+ return new PasswordAuthentication (username , passwordLH );
194+ }
195+ };
196+ BasicChallengeHandler challengeHandler = BasicChallengeHandler .create ();
197+ challengeHandler .setLoginHandler (loginHandler );
198+ return challengeHandler ;
199+ }
176200}
0 commit comments