Java tutorial
/** * Product : Hiperium Project * Architect: Andres Solorzano. * Created : 08-05-2009 - 23:30:00 * * The contents of this file are copyrighted by Andres Solorzano * and it is protected by the license: "GPL V3." You can find a copy of this * license at: http://www.hiperium.com/about/licence.html * * Copyright 2014 Andres Solorzano. All rights reserved. * */ package com.hiperium.bo.interceptor; import java.util.Locale; import javax.ejb.EJB; import javax.inject.Inject; import javax.interceptor.InvocationContext; import org.apache.commons.lang.StringUtils; import com.hiperium.bo.audit.UserAuditBO; import com.hiperium.bo.manager.exception.ExceptionManager; import com.hiperium.bo.manager.security.SessionManager; import com.hiperium.commons.EnumHiperiumTier; import com.hiperium.commons.EnumI18N; import com.hiperium.commons.HiperiumTier; import com.hiperium.commons.exception.EnumInformationException; import com.hiperium.commons.exception.InformationException; import com.hiperium.commons.log.HiperiumLogger; import com.hiperium.model.EnumCrudOperation; /** * * @author Andres Solorzano * @version 1.0 */ public class UserAuditInterceptor { /** The property log. */ @Inject @HiperiumTier(EnumHiperiumTier.BUSINESS) private HiperiumLogger log; /** The property sessionManager. */ @Inject private SessionManager sessionManager; /** The property userAuditBO. */ @EJB private UserAuditBO userAuditBO; /** The property exceptionManager. */ @EJB private ExceptionManager exceptionManager; /** * * @param context * @return * @throws Exception */ public Object registerUserAudit(InvocationContext context) throws InformationException { this.log.debug("registerUserAudit() - BEGIN: " + context.getMethod().getName()); Object result = null; // Validate the user session ID Object[] params = context.getParameters(); String sessionId = (String) params[params.length - 1]; if (StringUtils.isBlank(sessionId) || !this.sessionManager.isUserLoggedIn(sessionId)) { throw InformationException.generate(EnumI18N.COMMON, EnumInformationException.ACCESS_NOT_ALLOWED, Locale.getDefault()); } try { // Identify the CRUD operation in the method invocation EnumCrudOperation operation = EnumCrudOperation.decodeValue(context.getMethod().getName()); if (EnumCrudOperation.CREATE.equals(operation) || EnumCrudOperation.UPDATE.equals(operation) || EnumCrudOperation.DELETE.equals(operation)) { log.debug("CRUD OPERATION"); result = context.proceed(); // At the return of the method invocation we need to store the audit this.userAuditBO.create(context.getTarget().getClass().getSimpleName(), operation, sessionId); } else if (EnumCrudOperation.READ.equals(operation)) { log.debug("READ OPERATION"); result = context.proceed(); } else { throw InformationException.generate(EnumI18N.COMMON, EnumInformationException.ACCESS_NOT_ALLOWED, this.sessionManager.findUserLocale(sessionId)); } } catch (Exception e) { InformationException infoException = null; if (e.getCause() instanceof InformationException) { infoException = (InformationException) e; throw infoException; } infoException = this.exceptionManager.createMessageException(e, this.sessionManager.findUserLocale(sessionId)); throw infoException; } this.log.debug("registerUserAudit() - END: " + context.getMethod().getName()); return result; } }