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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
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
;
32 public class JavaClient
{
33 public static void main(String
[] args
) {
35 if (args
.length
!= 1) {
36 System
.out
.println("Please enter 'simple' or 'secure'");
42 if (args
[0].contains("simple")) {
43 transport
= new TSocket("localhost", 9090);
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.
53 TSSLTransportParameters params
= new TSSLTransportParameters();
54 params
.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS");
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()
59 transport
= TSSLTransportFactory
.getClientSocket("localhost", 9091, 0, params
);
62 TProtocol protocol
= new TBinaryProtocol(transport
);
63 Calculator
.Client client
= new Calculator
.Client(protocol
);
68 } catch (TException x
) {
73 private static void perform(Calculator
.Client client
) throws TException
76 System
.out
.println("ping()");
78 int sum
= client
.add(1,1);
79 System
.out
.println("1+1=" + sum
);
81 Work work
= new Work();
83 work
.op
= Operation
.DIVIDE
;
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
);
93 work
.op
= Operation
.SUBTRACT
;
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
);
103 SharedStruct log
= client
.getStruct(1);
104 System
.out
.println("Check log: " + log
.value
);