root / trunk / src / java / org / lidar / api / Management.java
History | View | Annotate | Download (3.47 KB)
1 | 9 | andrej.cim | package org.lidar.api; |
---|---|---|---|
2 | |||
3 | import java.util.Date; |
||
4 | import javax.annotation.Resource; |
||
5 | import javax.jws.WebMethod; |
||
6 | import javax.jws.WebService; |
||
7 | import javax.xml.ws.WebServiceContext; |
||
8 | import javax.xml.ws.handler.MessageContext; |
||
9 | import net.sf.jpam.Pam; |
||
10 | import org.hibernate.Query; |
||
11 | import org.hibernate.Session; |
||
12 | import org.hibernate.Transaction; |
||
13 | import org.lidar.HibernateUtil; |
||
14 | import org.lidar.db.Client; |
||
15 | |||
16 | /**
|
||
17 | * Management service
|
||
18 | * @author Andrej Cimpersek
|
||
19 | */
|
||
20 | @WebService()
|
||
21 | public class Management { |
||
22 | 11 | andrej.cim | |
23 | 9 | andrej.cim | private final Object lock = new Object(); |
24 | public static Boolean locked = false; |
||
25 | public static String lockToken; |
||
26 | public static Date lockAccess; |
||
27 | @Resource
|
||
28 | private WebServiceContext wsContext;
|
||
29 | |||
30 | /**
|
||
31 | * Web service operation
|
||
32 | */
|
||
33 | @WebMethod()
|
||
34 | 11 | andrej.cim | public boolean getLockStatus(String authToken) { |
35 | 10 | andrej.cim | Client c = getClient(String.format("token = '%s'", authToken)); |
36 | 11 | andrej.cim | if (c == null) { |
37 | return true; |
||
38 | } |
||
39 | |||
40 | 10 | andrej.cim | return Management.locked;
|
41 | 9 | andrej.cim | } |
42 | |||
43 | /**
|
||
44 | * Client helper
|
||
45 | * @param where
|
||
46 | * @return Client
|
||
47 | */
|
||
48 | 11 | andrej.cim | private Client getClient(String where) { |
49 | 9 | andrej.cim | Client c = null;
|
50 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
||
51 | Transaction tx = null;
|
||
52 | try {
|
||
53 | // TODO: check if api_key exists in DB
|
||
54 | // TODO: generate new auth token and save it to DB
|
||
55 | tx = session.beginTransaction(); |
||
56 | |||
57 | Query query = session.createQuery(String.format("from Client as client where %s", where)); |
||
58 | 11 | andrej.cim | c = (Client) query.list().get(0);
|
59 | 9 | andrej.cim | tx.commit(); |
60 | } catch (Exception e) { |
||
61 | tx.rollback(); |
||
62 | } |
||
63 | |||
64 | return c;
|
||
65 | } |
||
66 | |||
67 | /**
|
||
68 | * Get authentication token
|
||
69 | * @param api_key
|
||
70 | * @param username
|
||
71 | * @param password
|
||
72 | * @return null | authentication token
|
||
73 | */
|
||
74 | @WebMethod()
|
||
75 | 11 | andrej.cim | public String getAuthenticationToken(String api_key, String username, String password) { |
76 | 9 | andrej.cim | String token = null; |
77 | MessageContext mc = wsContext.getMessageContext(); |
||
78 | Pam pam = new Pam();
|
||
79 | boolean auth = pam.authenticateSuccessful(username, password);
|
||
80 | |||
81 | 11 | andrej.cim | if (auth) {
|
82 | 9 | andrej.cim | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
83 | Transaction tx = null;
|
||
84 | try {
|
||
85 | // TODO: check if api_key exists in DB
|
||
86 | // TODO: generate new auth token and save it to DB
|
||
87 | Client c = getClient(String.format("apikey = '%s'", api_key)); |
||
88 | |||
89 | 11 | andrej.cim | if (c != null) { |
90 | 9 | andrej.cim | token = c.getToken(); |
91 | } |
||
92 | } catch (Exception e) { |
||
93 | tx.rollback(); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return token;
|
||
98 | } |
||
99 | |||
100 | @WebMethod()
|
||
101 | 11 | andrej.cim | public String getLockToken(String authToken) { |
102 | synchronized (lock) {
|
||
103 | 9 | andrej.cim | Client c = getClient(String.format("token = '%s'", authToken)); |
104 | 11 | andrej.cim | if (Management.locked || c == null) { |
105 | return null; |
||
106 | } |
||
107 | 9 | andrej.cim | |
108 | Management.locked = true;
|
||
109 | Management.lockToken = authToken; // TODO: generate token
|
||
110 | Management.lockAccess = new Date(); |
||
111 | } |
||
112 | 11 | andrej.cim | |
113 | 9 | andrej.cim | return Management.lockToken;
|
114 | } |
||
115 | |||
116 | @WebMethod
|
||
117 | 11 | andrej.cim | public boolean unlock(String lockToken) { |
118 | synchronized (lock) {
|
||
119 | if (!Management.locked || !Management.lockToken.equals(lockToken)) {
|
||
120 | return false; |
||
121 | } |
||
122 | 9 | andrej.cim | Management.locked = false;
|
123 | Management.lockToken = null;
|
||
124 | } |
||
125 | |||
126 | return true; |
||
127 | } |
||
128 | } |