@@ -105,36 +105,44 @@ protected boolean process(ServletRequest req, ServletResponse res, FilterParams
105105 return true;
106106 }
107107
108- this.insertLogin (idpEntityId, idpName, clientIdentifier, clientName, userId);
108+ this.insertOrUpdateLogin (idpEntityId, idpName, clientIdentifier, clientName, userId);
109109 this.logUserLogin(idpEntityId, clientIdentifier, clientName, userId);
110110
111111 return true;
112112 }
113113
114- private void insertLogin (String idpEntityId, String idpName, String spIdentifier, String spName, String userId) {
115- LocalDate date = LocalDate.now() ;
116- String insertLoginQuery = "INSERT INTO " + statisticsTableName + "(day, idpId, spId, user, logins)" +
117- " VALUES(?, ?, ?, ?, '1') ON DUPLICATE KEY UPDATE logins = logins + 1" ;
114+ private void insertOrUpdateLogin (String idpEntityId, String idpName, String spIdentifier, String spName, String userId) {
115+ Connection c ;
116+ int idpId;
117+ int spId ;
118118
119- try (Connection c = mitreIdStats.getConnection()) {
120- insertIdpMap(c, idpEntityId, idpName);
121- insertSpMap(c, spIdentifier, spName);
122- int idpId = extractIdpId(c, idpEntityId);
123- int spId = extractSpId(c, spIdentifier);
124-
125- try (PreparedStatement preparedStatement = c.prepareStatement(insertLoginQuery)) {
126- preparedStatement.setDate(1, Date.valueOf(date));
127- preparedStatement.setInt(2, idpId);
128- preparedStatement.setInt(3, spId);
129- preparedStatement.setString(4, userId);
130- preparedStatement.execute();
131- log.trace("{} - login entry stored ({}, {}, {}, {}, {})", filterName, idpEntityId, idpName,
132- spIdentifier, spName, userId);
133- }
119+ try {
120+ c = mitreIdStats.getConnection();
121+ insertOrUpdateIdpMap(c, idpEntityId, idpName);
122+ insertOrUpdateSpMap(c, spIdentifier, spName);
123+ idpId = extractIdpId(c, idpEntityId);
124+ spId = extractSpId(c, spIdentifier);
134125 } catch (SQLException ex) {
135126 log.warn("{} - caught SQLException", filterName);
136127 log.debug("{} - details:", filterName, ex);
128+ return;
129+ }
130+
131+ LocalDate date = LocalDate.now();
132+
133+ try {
134+ insertLogin(date, c, idpId, spId, userId);
135+ } catch (SQLException ex) {
136+ try {
137+ updateLogin(date, c, idpId, spId, userId);
138+ } catch (SQLException e) {
139+ log.warn("{} - caught SQLException", filterName);
140+ log.debug("{} - details:", filterName, e);
141+ }
137142 }
143+
144+ log.trace("{} - login entry stored ({}, {}, {}, {}, {})", filterName, idpEntityId, idpName,
145+ spIdentifier, spName, userId);
138146 }
139147
140148 private int extractSpId(Connection c, String spIdentifier) throws SQLException {
@@ -159,30 +167,24 @@ private int extractIdpId(Connection c, String idpEntityId) throws SQLException {
159167 }
160168 }
161169
162- private void insertSpMap(Connection c, String spIdentifier, String spName) throws SQLException {
163- String insertSpMapQuery = "INSERT INTO " + serviceProvidersMapTableName + "(identifier, name)" +
164- " VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?";
165-
166- try (PreparedStatement preparedStatement = c.prepareStatement(insertSpMapQuery)) {
167- preparedStatement.setString(1, spIdentifier);
168- preparedStatement.setString(2, spName);
169- preparedStatement.setString(3, spName);
170- preparedStatement.execute();
171- log.trace("{} - SP map entry inserted", filterName);
170+ private void insertOrUpdateIdpMap(Connection c, String idpEntityId, String idpName) throws SQLException {
171+ try {
172+ insertIdpMap(c, idpEntityId, idpName);
173+ } catch (SQLException ex) {
174+ updateIdpMap(c, idpEntityId, idpName);
172175 }
173- }
174176
175- private void insertIdpMap(Connection c, String idpEntityId, String idpName) throws SQLException {
176- String insertIdpMapQuery = "INSERT INTO " + identityProvidersMapTableName + "(identifier, name)" +
177- " VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?";
177+ log.trace("{} - IdP map entry inserted", filterName);
178+ }
178179
179- try (PreparedStatement preparedStatement = c.prepareStatement(insertIdpMapQuery)) {
180- preparedStatement.setString(1, idpEntityId);
181- preparedStatement.setString(2, idpName);
182- preparedStatement.setString(3, idpName);
183- preparedStatement.execute();
184- log.trace("{} - IdP map entry inserted", filterName);
180+ private void insertOrUpdateSpMap(Connection c, String spIdentifier, String idpName) throws SQLException {
181+ try {
182+ insertSpMap(c, spIdentifier, idpName);
183+ } catch (SQLException ex) {
184+ updateSpMap(c, spIdentifier, idpName);
185185 }
186+
187+ log.trace("{} - SP map entry inserted", filterName);
186188 }
187189
188190 private String changeParamEncoding(String original) {
@@ -199,4 +201,68 @@ private void logUserLogin(String idpEntityId, String spIdentifier, String spName
199201 spName, idpEntityId);
200202 }
201203
204+ private void insertLogin(LocalDate date, Connection c, int idpId, int spId, String userId) throws SQLException {
205+ String insertLoginQuery = "INSERT INTO " + statisticsTableName +
206+ "(day, idpId, spId, user, logins)" +
207+ " VALUES(?, ?, ?, ?, '1')";
208+
209+ PreparedStatement preparedStatement = c.prepareStatement(insertLoginQuery);
210+ preparedStatement.setDate(1, Date.valueOf(date));
211+ preparedStatement.setInt(2, idpId);
212+ preparedStatement.setInt(3, spId);
213+ preparedStatement.setString(4, userId);
214+ preparedStatement.execute();
215+ }
216+
217+ private void updateLogin(LocalDate date, Connection c, int idpId, int spId, String userId) throws SQLException {
218+ String updateLoginQuery = "UPDATE " + statisticsTableName + " SET logins = logins + 1" +
219+ " WHERE day = ? AND idpId = ? AND spId = ? AND user = ?";
220+
221+ PreparedStatement preparedStatement = c.prepareStatement(updateLoginQuery);
222+ preparedStatement.setDate(1, Date.valueOf(date));
223+ preparedStatement.setInt(2, idpId);
224+ preparedStatement.setInt(3, spId);
225+ preparedStatement.setString(4, userId);
226+ preparedStatement.execute();
227+ }
228+
229+ private void insertIdpMap(Connection c, String idpEntityId, String idpName) throws SQLException {
230+ String insertIdpMapQuery = "INSERT INTO " + identityProvidersMapTableName + "(identifier, name)" +
231+ " VALUES (?, ?)";
232+
233+ PreparedStatement preparedStatement = c.prepareStatement(insertIdpMapQuery);
234+ preparedStatement.setString(1, idpEntityId);
235+ preparedStatement.setString(2, idpName);
236+ preparedStatement.execute();
237+ }
238+
239+ private void updateIdpMap(Connection c, String idpEntityId, String idpName) throws SQLException {
240+ String updateIdpMapQuery = "UPDATE " + identityProvidersMapTableName + "SET name = ? WHERE identifier = ?";
241+
242+ PreparedStatement preparedStatement = c.prepareStatement(updateIdpMapQuery);
243+ preparedStatement.setString(1, idpName);
244+ preparedStatement.setString(2, idpEntityId);
245+ preparedStatement.execute();
246+ }
247+
248+ private void insertSpMap(Connection c, String spIdentifier, String spName) throws SQLException {
249+ String insertSpMapQuery = "INSERT INTO " + serviceProvidersMapTableName + "(identifier, name)" +
250+ " VALUES (?, ?)";
251+
252+ try (PreparedStatement preparedStatement = c.prepareStatement(insertSpMapQuery)) {
253+ preparedStatement.setString(1, spIdentifier);
254+ preparedStatement.setString(2, spName);
255+ preparedStatement.execute();
256+ }
257+ }
258+
259+ private void updateSpMap(Connection c, String spIdentifier, String idpName) throws SQLException {
260+ String updateSpMapQuery = "UPDATE " + serviceProvidersMapTableName + "SET name = ? WHERE identifier = ?";
261+
262+ PreparedStatement preparedStatement = c.prepareStatement(updateSpMapQuery);
263+ preparedStatement.setString(1, idpName);
264+ preparedStatement.setString(2, spIdentifier);
265+ preparedStatement.execute();
266+ }
267+
202268}
0 commit comments