@@ -105,36 +105,44 @@ protected boolean process(ServletRequest req, ServletResponse res, FilterParams
105
105
return true ;
106
106
}
107
107
108
- this .insertLogin (idpEntityId , idpName , clientIdentifier , clientName , userId );
108
+ this .insertOrUpdateLogin (idpEntityId , idpName , clientIdentifier , clientName , userId );
109
109
this .logUserLogin (idpEntityId , clientIdentifier , clientName , userId );
110
110
111
111
return true ;
112
112
}
113
113
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 ;
118
118
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 );
134
125
} catch (SQLException ex ) {
135
126
log .warn ("{} - caught SQLException" , filterName );
136
127
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
+ }
137
142
}
143
+
144
+ log .trace ("{} - login entry stored ({}, {}, {}, {}, {})" , filterName , idpEntityId , idpName ,
145
+ spIdentifier , spName , userId );
138
146
}
139
147
140
148
private int extractSpId (Connection c , String spIdentifier ) throws SQLException {
@@ -159,30 +167,24 @@ private int extractIdpId(Connection c, String idpEntityId) throws SQLException {
159
167
}
160
168
}
161
169
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 );
172
175
}
173
- }
174
176
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
+ }
178
179
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 );
185
185
}
186
+
187
+ log .trace ("{} - SP map entry inserted" , filterName );
186
188
}
187
189
188
190
private String changeParamEncoding (String original ) {
@@ -199,4 +201,68 @@ private void logUserLogin(String idpEntityId, String spIdentifier, String spName
199
201
spName , idpEntityId );
200
202
}
201
203
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
+
202
268
}
0 commit comments