1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 // Generated code 21 import tutorial.*; 22 import shared.*; 23 24 import org.apache.thrift.TException; 25 import org.apache.thrift.transport.TSSLTransportFactory; 26 import org.apache.thrift.transport.TTransport; 27 import org.apache.thrift.transport.TSocket; 28 import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; 29 import org.apache.thrift.protocol.TBinaryProtocol; 30 import org.apache.thrift.protocol.TProtocol; 31 32 public class JavaClient { main(String [] args)33 public static void main(String [] args) { 34 35 if (args.length != 1) { 36 System.out.println("Please enter 'simple' or 'secure'"); 37 System.exit(0); 38 } 39 40 try { 41 TTransport transport; 42 if (args[0].contains("simple")) { 43 transport = new TSocket("localhost", 9090); 44 transport.open(); 45 } 46 else { 47 /* 48 * Similar to the server, you can use the parameters to setup client parameters or 49 * use the default settings. On the client side, you will need a TrustStore which 50 * contains the trusted certificate along with the public key. 51 * For this example it's a self-signed cert. 52 */ 53 TSSLTransportParameters params = new TSSLTransportParameters(); 54 params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS"); 55 /* 56 * Get a client transport instead of a server transport. The connection is opened on 57 * invocation of the factory method, no need to specifically call open() 58 */ 59 transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params); 60 } 61 62 TProtocol protocol = new TBinaryProtocol(transport); 63 Calculator.Client client = new Calculator.Client(protocol); 64 65 perform(client); 66 67 transport.close(); 68 } catch (TException x) { 69 x.printStackTrace(); 70 } 71 } 72 perform(Calculator.Client client)73 private static void perform(Calculator.Client client) throws TException 74 { 75 client.ping(); 76 System.out.println("ping()"); 77 78 int sum = client.add(1,1); 79 System.out.println("1+1=" + sum); 80 81 Work work = new Work(); 82 83 work.op = Operation.DIVIDE; 84 work.num1 = 1; 85 work.num2 = 0; 86 try { 87 int quotient = client.calculate(1, work); 88 System.out.println("Whoa we can divide by 0"); 89 } catch (InvalidOperation io) { 90 System.out.println("Invalid operation: " + io.why); 91 } 92 93 work.op = Operation.SUBTRACT; 94 work.num1 = 15; 95 work.num2 = 10; 96 try { 97 int diff = client.calculate(1, work); 98 System.out.println("15-10=" + diff); 99 } catch (InvalidOperation io) { 100 System.out.println("Invalid operation: " + io.why); 101 } 102 103 SharedStruct log = client.getStruct(1); 104 System.out.println("Check log: " + log.value); 105 } 106 } 107