| /* | |
| * Copyright (c) 2002, 2018 Oracle and/or its affiliates. All rights reserved. | |
| * | |
| * This program and the accompanying materials are made available under the | |
| * terms of the Eclipse Public License v. 2.0, which is available at | |
| * http://www.eclipse.org/legal/epl-2.0. | |
| * | |
| * This Source Code may also be made available under the following Secondary | |
| * Licenses when the conditions for such availability set forth in the | |
| * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | |
| * version 2 with the GNU Classpath Exception, which is available at | |
| * https://www.gnu.org/software/classpath/license.html. | |
| * | |
| * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | |
| */ | |
| package com.sun.s1peqe.security.ssl.converter.client; | |
| import java.util.Properties; | |
| import java.math.BigDecimal; | |
| import javax.naming.Context; | |
| import javax.naming.InitialContext; | |
| import javax.rmi.PortableRemoteObject; | |
| import com.sun.s1peqe.security.ssl.converter.ejb.*; | |
| import com.sun.ejte.ccl.reporter.SimpleReporterAdapter; | |
| /** | |
| * A simple java client. This uses the services provided by the <code>ConverterBean</code> and | |
| * converts 100 US dollars to Yen and 100 Yen to Euro. | |
| * <p>In this regard, it does the following in order | |
| * <ul> | |
| * <li>Locates the home interface of the enterprise bean | |
| * <li>Gets a reference to the remote interface | |
| * <li>Invokes business methods | |
| * </ul> | |
| * <br> | |
| * <b>Locating the home interface:</b> | |
| * <blockquote><pre> | |
| * Context initial = new InitialContext(); | |
| * Context myEnv = (Context)initial.lookup("java:comp/env"); | |
| * Object objref = myEnv.lookup("ejb/SimpleConverter"); | |
| * ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class); | |
| * </pre></blockquote> | |
| * <br> | |
| * <b>Creating the remote interface:</b> | |
| * <blockquote><pre> | |
| * Converter currencyConverter = home.create(); | |
| * </pre></blockquote> | |
| * <br> | |
| * <b>Invoking business methods:</b> | |
| * <blockquote><pre> | |
| * BigDecimal param = new BigDecimal ("100.00"); | |
| * amount = currencyConverter.dollarToYen(param); | |
| * amount = currencyConverter.yenToEuro(param); | |
| * </pre></blockquote> | |
| * <br> | |
| * <b>Output:</b> | |
| * <pre> | |
| * 12160.00 | |
| * 0.77 | |
| * </pre> | |
| * | |
| * | |
| */ | |
| public class ConverterClient { | |
| private static SimpleReporterAdapter stat = | |
| new SimpleReporterAdapter("appserv-tests"); | |
| ConverterClient() { | |
| } | |
| /** | |
| * The main method of the client. This invokes the <code>ConverterBean</code> to use | |
| * its services. It then asks the bean to convert 100 dollars to yen and | |
| * 100 yen to euro. The results are printed at the terminal where the client is run. | |
| * See <code>appclient</code> documentation in SunONE app server to run the clinet. | |
| * | |
| */ | |
| public static void main(String[] args) { | |
| ConverterClient client = new ConverterClient(); | |
| client.run(args); | |
| } | |
| private void run(String[] args) { | |
| String url = null; | |
| String testId = null; | |
| String jndiName = null; | |
| Context context = null; | |
| String ctxFactory = null; | |
| java.lang.Object obj = null; | |
| try { | |
| stat.addDescription("Security::SSL tester -converter sample."); | |
| if (args.length == 3) { | |
| url = args[0]; | |
| ctxFactory = args[1]; | |
| jndiName = args[2]; | |
| } | |
| if ( (url == null) || (ctxFactory == null) ) { | |
| testId = "Sec::Mutual_SSL_simple Converter Sample AppClient"; | |
| // Initialize the Context with default properties | |
| context = new InitialContext(); | |
| System.out.println("Default Context Initialized..."); | |
| // Create Home object | |
| obj = context.lookup("java:comp/env/ejb/SSLSimpleConverter"); | |
| } else { | |
| testId = "Sec::Mutual_SSL_simple Standalone-Client"; | |
| Properties env = new Properties(); | |
| env.put("java.naming.provider.url", url); | |
| env.put("java.naming.factory.initial", ctxFactory); | |
| // Initialize the Context with JNDI specific properties | |
| context = new InitialContext(env); | |
| System.out.println("Context Initialized with " + | |
| "URL: " + url + ", Factory: " + ctxFactory); | |
| // Create Home object | |
| obj = context.lookup(jndiName); | |
| } | |
| String mys = "CN=SSLTest,OU=Sun Java System Application Server,O=Sun Microsystems,L=Santa Clara,ST=California,C=US"; | |
| System.out.println("Certname in appclication-client ->"+mys); | |
| ConverterRemoteHome home = | |
| (ConverterRemoteHome) PortableRemoteObject.narrow(obj, | |
| ConverterRemoteHome.class); | |
| ConverterRemote currencyConverter = home.create(); | |
| String s = currencyConverter.myCallerPrincipal(); | |
| System.out.println(" The caller principal received from ejb ->"+s); | |
| if(!mys.equalsIgnoreCase(s)){ | |
| String error = "The caller principals dont match!! - Test Failed"; | |
| System.out.println(error); | |
| throw new Exception(error); | |
| }else{ | |
| System.out.println("Mutual Authentication worked - calling other methods"); | |
| } | |
| BigDecimal param = new BigDecimal ("100.00"); | |
| BigDecimal amount = currencyConverter.dollarToYen(param); | |
| System.out.println("\n\n\n===========Beginning Simple Test=====\n\n"); | |
| System.out.println("$100 is : "+amount+"Yen"); | |
| amount = currencyConverter.yenToEuro(param); | |
| System.out.println("Yen is :"+amount+"Euro"); | |
| stat.addStatus(testId, stat.PASS); | |
| } catch (Exception ex) { | |
| stat.addStatus(testId, stat.FAIL); | |
| System.err.println("Caught an unexpected exception!"); | |
| ex.printStackTrace(); | |
| } finally { | |
| stat.printSummary(testId); | |
| } | |
| } | |
| } |