Project

General

Profile

Statistics
| Revision:

root / trunk / src / java / org / lidar / api / Management.java

History | View | Annotate | Download (3.47 KB)

1
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

    
23
    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
    public boolean getLockStatus(String authToken) {
35
        Client c = getClient(String.format("token = '%s'", authToken));
36
        if (c == null) {
37
            return true;
38
        }
39

    
40
        return Management.locked;
41
    }
42

    
43
    /**
44
     * Client helper
45
     * @param where
46
     * @return Client
47
     */
48
    private Client getClient(String where) {
49
        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
            c = (Client) query.list().get(0);
59
            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
    public String getAuthenticationToken(String api_key, String username, String password) {
76
        String token = null;
77
        MessageContext mc = wsContext.getMessageContext();
78
        Pam pam = new Pam();
79
        boolean auth = pam.authenticateSuccessful(username, password);
80

    
81
        if (auth) {
82
            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
                if (c != null) {
90
                    token = c.getToken();
91
                }
92
            } catch (Exception e) {
93
                tx.rollback();
94
            }
95
        }
96

    
97
        return token;
98
    }
99

    
100
    @WebMethod()
101
    public String getLockToken(String authToken) {
102
        synchronized (lock) {
103
            Client c = getClient(String.format("token = '%s'", authToken));
104
            if (Management.locked || c == null) {
105
                return null;
106
            }
107

    
108
            Management.locked = true;
109
            Management.lockToken = authToken; // TODO: generate token
110
            Management.lockAccess = new Date();
111
        }
112

    
113
        return Management.lockToken;
114
    }
115

    
116
    @WebMethod
117
    public boolean unlock(String lockToken) {
118
        synchronized (lock) {
119
            if (!Management.locked || !Management.lockToken.equals(lockToken)) {
120
                return false;
121
            }
122
            Management.locked = false;
123
            Management.lockToken = null;
124
        }
125

    
126
        return true;
127
    }
128
}