Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package it.inaf.ia2.gms.persistence;
import it.inaf.ia2.gms.persistence.model.ClientEntity;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class ClientsDAO {
private final JdbcTemplate jdbcTemplate;
@Autowired
public ClientsDAO(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
public Optional<ClientEntity> findClientById(String clientId) {
String sql = "SELECT client_secret, allowed_actions, ip_filter FROM gms_client WHERE client_id = ?";
return jdbcTemplate.query(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, clientId);
return ps;
}, resultSet -> {
if (resultSet.next()) {
ClientEntity client = new ClientEntity();
client.setId(clientId);
client.setSecret(resultSet.getString("client_secret"));
client.setAllowedActions(getAllowedActions(resultSet));
client.setIpFilter(resultSet.getString("ip_filter"));
return Optional.of(client);
}
return Optional.empty();
});
}
private List<String> getAllowedActions(ResultSet resultSet) throws SQLException {
List<String> actions = new ArrayList<>();
ResultSet items = resultSet.getArray("allowed_actions").getResultSet();
while (items.next()) {
String action = items.getString(1);
actions.add(action);
}
return actions;
}
}