3838 * to idp name (depends on DataSource bean mitreIdStats)
3939 * <li><b>filter.[name].serviceProvidersMapTableName</b> - Name of the table with mapping of client_id (SP)
4040 * to client name (depends on DataSource bean mitreIdStats)</li>
41+ * <li><b>filter.[name].ipdIdColumnName</b> - Name for the column which stores IDs of IdPs in statisticsTable</li>
42+ * <li><b>filter.[name].spIdColumnName</b> - Name for the column which stores IDs of SPs in statisticsTable</li>
4143 * </ul>
4244 *
4345 * @author Dominik Baránek <[email protected] > @@ -52,12 +54,16 @@ public class ProxyStatisticsFilter extends PerunRequestFilter {
5254 private static final String STATISTICS_TABLE_NAME = "statisticsTableName" ;
5355 private static final String IDENTITY_PROVIDERS_MAP_TABLE_NAME = "identityProvidersMapTableName" ;
5456 private static final String SERVICE_PROVIDERS_MAP_TABLE_NAME = "serviceProvidersMapTableName" ;
57+ private static final String IDP_ID_COLUMN_NAME = "idpIdColumnName" ;
58+ private static final String SP_ID_COLUMN_NAME = "spIdColumnName" ;
5559
5660 private final String idpNameAttributeName ;
5761 private final String idpEntityIdAttributeName ;
5862 private final String statisticsTableName ;
5963 private final String identityProvidersMapTableName ;
6064 private final String serviceProvidersMapTableName ;
65+ private final String idpIdColumnName ;
66+ private final String spIdColumnName ;
6167 /* END OF CONFIGURATION OPTIONS */
6268
6369 private final DataSource mitreIdStats ;
@@ -73,6 +79,8 @@ public ProxyStatisticsFilter(PerunRequestFilterParams params) {
7379 this .statisticsTableName = params .getProperty (STATISTICS_TABLE_NAME );
7480 this .identityProvidersMapTableName = params .getProperty (IDENTITY_PROVIDERS_MAP_TABLE_NAME );
7581 this .serviceProvidersMapTableName = params .getProperty (SERVICE_PROVIDERS_MAP_TABLE_NAME );
82+ this .idpIdColumnName = params .getProperty (IDP_ID_COLUMN_NAME );
83+ this .spIdColumnName = params .getProperty (SP_ID_COLUMN_NAME );
7684 this .filterName = params .getFilterName ();
7785 }
7886
@@ -105,35 +113,48 @@ protected boolean process(ServletRequest req, ServletResponse res, FilterParams
105113 return true ;
106114 }
107115
108- this .insertLogin (idpEntityId , idpName , clientIdentifier , clientName , userId );
116+ this .insertOrUpdateLogin (idpEntityId , idpName , clientIdentifier , clientName , userId );
109117 this .logUserLogin (idpEntityId , clientIdentifier , clientName , userId );
110118
111119 return true ;
112120 }
113121
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" ;
122+ private void insertOrUpdateLogin (String idpEntityId , String idpName , String spIdentifier , String spName , String userId ) {
123+ Connection c ;
124+ int idpId ;
125+ int spId ;
118126
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- }
127+ try {
128+ c = mitreIdStats .getConnection ();
129+ insertOrUpdateIdpMap (c , idpEntityId , idpName );
130+ insertOrUpdateSpMap (c , spIdentifier , spName );
131+
132+ idpId = extractIdpId (c , idpEntityId );
133+ log .trace ("{} - extracted idpId: {}" , filterName , idpId );
134+
135+ spId = extractSpId (c , spIdentifier );
136+ log .trace ("{} - extracted spId: {}" , filterName , spId );
134137 } catch (SQLException ex ) {
135138 log .warn ("{} - caught SQLException" , filterName );
136139 log .debug ("{} - details:" , filterName , ex );
140+ return ;
141+ }
142+
143+ LocalDate date = LocalDate .now ();
144+
145+ try {
146+ insertLogin (date , c , idpId , spId , userId );
147+ log .trace ("{} - login entry inserted ({}, {}, {}, {}, {})" , filterName , idpEntityId , idpName ,
148+ spIdentifier , spName , userId );
149+ } catch (SQLException ex ) {
150+ try {
151+ updateLogin (date , c , idpId , spId , userId );
152+ log .trace ("{} - login entry updated ({}, {}, {}, {}, {})" , filterName , idpEntityId , idpName ,
153+ spIdentifier , spName , userId );
154+ } catch (SQLException e ) {
155+ log .warn ("{} - caught SQLException" , filterName );
156+ log .debug ("{} - details:" , filterName , e );
157+ }
137158 }
138159 }
139160
@@ -159,29 +180,23 @@ private int extractIdpId(Connection c, String idpEntityId) throws SQLException {
159180 }
160181 }
161182
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 );
183+ private void insertOrUpdateIdpMap (Connection c , String idpEntityId , String idpName ) throws SQLException {
184+ try {
185+ insertIdpMap (c , idpEntityId , idpName );
186+ log .trace ("{} - IdP map entry inserted" , filterName );
187+ } catch (SQLException ex ) {
188+ updateIdpMap (c , idpEntityId , idpName );
189+ log .trace ("{} - IdP map entry updated" , filterName );
172190 }
173191 }
174192
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 = ?" ;
178-
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 );
193+ private void insertOrUpdateSpMap (Connection c , String spIdentifier , String idpName ) throws SQLException {
194+ try {
195+ insertSpMap (c , spIdentifier , idpName );
196+ log .trace ("{} - SP map entry inserted" , filterName );
197+ } catch (SQLException ex ) {
198+ updateSpMap (c , spIdentifier , idpName );
199+ log .trace ("{} - SP map entry updated" , filterName );
185200 }
186201 }
187202
@@ -195,8 +210,72 @@ private String changeParamEncoding(String original) {
195210 }
196211
197212 private void logUserLogin (String idpEntityId , String spIdentifier , String spName , String userId ) {
198- log .info ("User identity: {}, service: {}, serviceName: {}, via IdP: {}" , userId , spIdentifier ,
213+ log .info ("{} - User identity: {}, service: {}, serviceName: {}, via IdP: {}" , filterName , userId , spIdentifier ,
199214 spName , idpEntityId );
200215 }
201216
217+ private void insertLogin (LocalDate date , Connection c , int idpId , int spId , String userId ) throws SQLException {
218+ String insertLoginQuery = "INSERT INTO " + statisticsTableName +
219+ "(day, " + idpIdColumnName + ", " + spIdColumnName + ", user, logins)" +
220+ " VALUES(?, ?, ?, ?, '1')" ;
221+
222+ PreparedStatement preparedStatement = c .prepareStatement (insertLoginQuery );
223+ preparedStatement .setDate (1 , Date .valueOf (date ));
224+ preparedStatement .setInt (2 , idpId );
225+ preparedStatement .setInt (3 , spId );
226+ preparedStatement .setString (4 , userId );
227+ preparedStatement .execute ();
228+ }
229+
230+ private void updateLogin (LocalDate date , Connection c , int idpId , int spId , String userId ) throws SQLException {
231+ String updateLoginQuery = "UPDATE " + statisticsTableName + " SET logins = logins + 1" +
232+ " WHERE day = ? AND " + idpIdColumnName + " = ? AND " + spIdColumnName + " = ? AND user = ?" ;
233+
234+ PreparedStatement preparedStatement = c .prepareStatement (updateLoginQuery );
235+ preparedStatement .setDate (1 , Date .valueOf (date ));
236+ preparedStatement .setInt (2 , idpId );
237+ preparedStatement .setInt (3 , spId );
238+ preparedStatement .setString (4 , userId );
239+ preparedStatement .execute ();
240+ }
241+
242+ private void insertIdpMap (Connection c , String idpEntityId , String idpName ) throws SQLException {
243+ String insertIdpMapQuery = "INSERT INTO " + identityProvidersMapTableName + " (identifier, name)" +
244+ " VALUES (?, ?)" ;
245+
246+ PreparedStatement preparedStatement = c .prepareStatement (insertIdpMapQuery );
247+ preparedStatement .setString (1 , idpEntityId );
248+ preparedStatement .setString (2 , idpName );
249+ preparedStatement .execute ();
250+ }
251+
252+ private void updateIdpMap (Connection c , String idpEntityId , String idpName ) throws SQLException {
253+ String updateIdpMapQuery = "UPDATE " + identityProvidersMapTableName + " SET name = ? WHERE identifier = ?" ;
254+
255+ PreparedStatement preparedStatement = c .prepareStatement (updateIdpMapQuery );
256+ preparedStatement .setString (1 , idpName );
257+ preparedStatement .setString (2 , idpEntityId );
258+ preparedStatement .execute ();
259+ }
260+
261+ private void insertSpMap (Connection c , String spIdentifier , String spName ) throws SQLException {
262+ String insertSpMapQuery = "INSERT INTO " + serviceProvidersMapTableName + " (identifier, name)" +
263+ " VALUES (?, ?)" ;
264+
265+ try (PreparedStatement preparedStatement = c .prepareStatement (insertSpMapQuery )) {
266+ preparedStatement .setString (1 , spIdentifier );
267+ preparedStatement .setString (2 , spName );
268+ preparedStatement .execute ();
269+ }
270+ }
271+
272+ private void updateSpMap (Connection c , String spIdentifier , String idpName ) throws SQLException {
273+ String updateSpMapQuery = "UPDATE " + serviceProvidersMapTableName + " SET name = ? WHERE identifier = ?" ;
274+
275+ PreparedStatement preparedStatement = c .prepareStatement (updateSpMapQuery );
276+ preparedStatement .setString (1 , idpName );
277+ preparedStatement .setString (2 , spIdentifier );
278+ preparedStatement .execute ();
279+ }
280+
202281}
0 commit comments