| 48 |
ashish |
1 |
package in.shop2020.thrift.clients;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.thrift.userprofilemanager.User;
|
|
|
4 |
import in.shop2020.thrift.userprofilemanager.UserProfileService;
|
|
|
5 |
import in.shop2020.utils.Logger;
|
|
|
6 |
|
|
|
7 |
import org.apache.thrift.protocol.TCompactProtocol;
|
|
|
8 |
import org.apache.thrift.protocol.TProtocol;
|
|
|
9 |
import org.apache.thrift.transport.TFramedTransport;
|
|
|
10 |
import org.apache.thrift.transport.TSocket;
|
|
|
11 |
import org.apache.thrift.transport.TTransport;
|
|
|
12 |
|
|
|
13 |
public class UserProfileClient {
|
|
|
14 |
private String host = "localhost";
|
|
|
15 |
private int port = 8080;
|
|
|
16 |
private TSocket socket = null;
|
|
|
17 |
private TTransport transport = null;
|
|
|
18 |
private TProtocol protocol = null;
|
|
|
19 |
private UserProfileService.Client client = null;
|
|
|
20 |
|
|
|
21 |
private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
public UserProfileClient(){
|
|
|
25 |
this("localhost", 8080);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
public UserProfileClient(String host, int port){
|
|
|
29 |
if(host == null || host.equals("")){
|
|
|
30 |
|
|
|
31 |
}
|
|
|
32 |
socket = new TSocket(host, port);
|
|
|
33 |
socket.setTimeout(SOCKET_TIMEOUT);
|
|
|
34 |
transport = new TFramedTransport(socket);
|
|
|
35 |
protocol = new TCompactProtocol(transport);
|
|
|
36 |
client = new UserProfileService.Client(protocol);
|
|
|
37 |
Logger.log("Created UserProfileClient for host "+ host+" and port "+ port, this);
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
//TODO:- put proper exceptions heirarchy
|
|
|
43 |
|
|
|
44 |
public User addUser(User u) throws Exception{
|
|
|
45 |
if(!transport.isOpen()){
|
|
|
46 |
transport.open();
|
|
|
47 |
}
|
|
|
48 |
return client.addUser(u);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public boolean authenticateUser(String username, String password) throws Exception{
|
|
|
52 |
if(!transport.isOpen()){
|
|
|
53 |
transport.open();
|
|
|
54 |
}
|
|
|
55 |
return client.authenticateUser(username, password);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public User getUser(int id) throws Exception{
|
|
|
59 |
if(!transport.isOpen()){
|
|
|
60 |
transport.open();
|
|
|
61 |
}
|
|
|
62 |
return client.getUser(id);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public User updatePassword(User u, String password) throws Exception{
|
|
|
66 |
if(!transport.isOpen()){
|
|
|
67 |
transport.open();
|
|
|
68 |
}
|
|
|
69 |
return client.updatePassword(u, password);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
}
|