Skip to content

Instantly share code, notes, and snippets.

@BenRhouma
Last active August 29, 2015 14:07
Show Gist options
  • Save BenRhouma/286766930028da93f82d to your computer and use it in GitHub Desktop.
Save BenRhouma/286766930028da93f82d to your computer and use it in GitHub Desktop.
// you must add this line to the build.scala :: play.Project(.........).settings(....)
// .settings(aspectjSettings: _*).settings(inputs in Aspectj <+= compiledClasses,products in Compile <<= products in Aspectj, products in Runtime <<= products in Compile)
package filters;
import models.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import play.Logger;
import play.mvc.Http;
import security.RemotAuthenticate;
/**
* @author z.benrhouma
* @since 29/03/2014
*/
@Aspect
public class LogFilter {
private static final String messageTemplate = "User(id:%s) is trying to access to (Url:%s) (Method:%s.%s) from(ip: %s) ";
/**
* Description : Access Logs
* Author : zied.ben.rhouma
* LastModified : 31/03/2014
*/
@Before("execution(play.mvc.Result controllers.*Controller.*(..))")
public void logAccessRules(JoinPoint thisJoinPoint) {
try {
//get http context
final Http.Context current = Http.Context.current();
if (current != null) {
// get current logged user
final User user = RemotAuthenticate.getUser(current.session());
if (user != null) {
Logger.of("ACCESS").info(String.format(messageTemplate
,user.comEntityId
,current._requestHeader().path()
,thisJoinPoint.getSignature().getDeclaringTypeName()
,thisJoinPoint.getSignature().getName()
,current._requestHeader().host()));
}
}
} catch (Exception e) {
Logger.error(e.getMessage(), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment