Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.monitoring.svc.impl; import org.apigw.monitoring.svc.OAuthMonitoringService; import org.apigw.monitoring.svc.exception.ApigwMonitoringException; import org.apigw.monitoring.types.enums.RequestState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; import org.springframework.security.oauth2.provider.code.AuthorizationRequestHolder; import java.util.Set; /** * Intercepts and monitors calls to AuthorizationCodeServices * * @author albert */ public class MonitoredAuthorizationCodeServices implements AuthorizationCodeServices { private static final Logger log = LoggerFactory.getLogger(MonitoredAuthorizationCodeServices.class); @Autowired private OAuthMonitoringService monitoringService; private AuthorizationCodeServices authorizationCodeServices; @Override public String createAuthorizationCode(AuthorizationRequestHolder authentication) { log.debug("createAuthorizationCode - start"); Set<String> scope = authentication.getAuthenticationRequest().getScope(); String clientId = authentication.getAuthenticationRequest().getClientId(); UserDetails user = (UserDetails) authentication.getUserAuthentication().getPrincipal(); String username = user.getUsername(); try { String authorizationCode = authorizationCodeServices.createAuthorizationCode(authentication); monitorCreateAuthorizationCode(clientId, scope, authorizationCode, RequestState.SUCCESS, null, username); log.debug("createAuthorizationCode - end"); return authorizationCode; } catch (ApigwMonitoringException e) { throw e; } catch (RuntimeException e) { log.error("error creating authorizationCode", e); monitorCreateAuthorizationCode(clientId, scope, null, RequestState.SERVER_FAILURE, e.getMessage(), username); throw e; } } private void monitorCreateAuthorizationCode(String clientId, Set<String> scope, String code, RequestState state, String message, String user) { try { monitoringService.logAuthorizationGrant(System.currentTimeMillis(), clientId, scope, code, state.toString(), message, user); } catch (ApigwMonitoringException e) { log.error( "failed to monitor createAuthorizationCode clientId[{}], scope[{}], code[{}], state[{}], message[{}]", clientId, scope, code, state, message); throw e; } } @Override public AuthorizationRequestHolder consumeAuthorizationCode(String code) { throw new UnsupportedOperationException("Consume Authorization Code is unsupported in this scope."); } /** * @param authorizationCodeServices the authorizationCodeServices to set */ public void setAuthorizationCodeServices(AuthorizationCodeServices authorizationCodeServices) { this.authorizationCodeServices = authorizationCodeServices; } }