David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 20 | // Generated code |
| 21 | import tutorial.*; |
| 22 | import shared.*; |
| 23 | |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 24 | import org.apache.thrift.TException; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 25 | import org.apache.thrift.transport.TSSLTransportFactory; |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 26 | import org.apache.thrift.transport.TTransport; |
| 27 | import org.apache.thrift.transport.TSocket; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 28 | import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 29 | import org.apache.thrift.protocol.TBinaryProtocol; |
| 30 | import org.apache.thrift.protocol.TProtocol; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 31 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 32 | public class JavaClient { |
| 33 | public static void main(String [] args) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 34 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 35 | if (args.length != 1) { |
| 36 | System.out.println("Please enter 'simple' or 'secure'"); |
| 37 | System.exit(0); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 40 | try { |
| 41 | TTransport transport; |
| 42 | if (args[0].contains("simple")) { |
| 43 | transport = new TSocket("localhost", 9090); |
| 44 | transport.open(); |
| 45 | } |
| 46 | else { |
| 47 | /* |
Roger Meier | 3f067a8 | 2011-03-04 13:35:05 +0000 | [diff] [blame] | 48 | * Similar to the server, you can use the parameters to setup client parameters or |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 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 | } |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 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 | } |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 106 | } |