FileDocCategorySizeDatePackage
Client.javaAPI DocJBoss 4.2.13720Fri Jul 13 20:55:16 BST 2007org.jboss.tutorial.http_https.client

Client

public class Client extends Object

Fields Summary
Constructors Summary
Methods Summary
private static voidcalculate(java.lang.String jndiName)

      // Establish the proxy with an incorrect security identity
      Properties env = new Properties();
      env.setProperty(Context.SECURITY_PRINCIPAL, "kabir");
      env.setProperty(Context.SECURITY_CREDENTIALS, "invalidpassword");
      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
      InitialContext ctx = new InitialContext(env);
      Calculator calculator = (Calculator) ctx.lookup(jndiName);

      System.out.println("Kabir is a student.");
      System.out.println("Kabir types in the wrong password");
      try
      {
         System.out.println("1 + 1 = " + calculator.add(1, 1));
      }
      catch (Exception ex)
      {
         System.out.println("Saw expected SecurityException: " + ex.getMessage());
      }

      System.out.println("Kabir types in correct password.");
      System.out.println("Kabir does unchecked addition.");

      // Re-establish the proxy with the correct security identity
      env.setProperty(Context.SECURITY_CREDENTIALS, "validpassword");
      ctx = new InitialContext(env);
      calculator = (Calculator) ctx.lookup(jndiName);

      System.out.println("1 + 1 = " + calculator.add(1, 1));

      System.out.println("Kabir is not a teacher so he cannot do division");
      try
      {
         calculator.divide(16, 4);
      }
      catch (Exception  ex)
      {
         System.out.println(ex.getMessage());
      }

      System.out.println("Students are allowed to do subtraction");
      System.out.println("1 - 1 = " + calculator.subtract(1, 1));
   
public static voidmain(java.lang.String[] args)

      setHostNameVerifier();
      
      // Invoke via HTTP
      calculate("CalculatorHttpBean/remote");
      
      // Invoke via HTTPS
      calculate("CalculatorHttpsBean/remote");
   
private static voidsetHostNameVerifier()

      HostnameVerifier hv = new HostnameVerifier()
      {
         public boolean verify(String urlHostName, SSLSession session)
         {
          System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
          return true;
       }
      };
     HttpsURLConnection.setDefaultHostnameVerifier(hv);