-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from karrotmvp/feature-150/short_url_logging
Feature 150/short url logging
- Loading branch information
Showing
12 changed files
with
193 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/daangn/survey/core/aop/LogEventAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.daangn.survey.core.aop; | ||
|
||
import com.daangn.survey.core.log.annotation.UserLogging; | ||
import com.daangn.survey.core.log.model.LogEvent; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@Slf4j | ||
@Component | ||
@Aspect | ||
public class LogEventAspect implements ApplicationEventPublisherAware { | ||
private ApplicationEventPublisher eventPublisher; | ||
|
||
@Pointcut("@annotation(userLogging)") | ||
public void pointcut(UserLogging userLogging) { | ||
} | ||
|
||
@AfterReturning(pointcut = "pointcut(userLogging)") | ||
public void afterReturning(JoinPoint joinPoint, UserLogging userLogging){ | ||
|
||
switch (userLogging.type()){ | ||
case SHORT_URL: | ||
String referer = null; | ||
String userAgent = null; | ||
String url = null; | ||
|
||
for(Object arg : joinPoint.getArgs()){ | ||
if(arg instanceof HttpServletRequest) { | ||
HttpServletRequest request = (HttpServletRequest) arg; | ||
referer = request.getHeader("referer"); | ||
userAgent = request.getHeader("user-agent"); | ||
} | ||
if(arg instanceof String){ | ||
url = (String) arg; | ||
} | ||
} | ||
|
||
eventPublisher.publishEvent(new LogEvent(url, userAgent, referer)); | ||
break; | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
this.eventPublisher = applicationEventPublisher; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/daangn/survey/core/log/LogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.daangn.survey.core.log; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class LogRepository { | ||
private final MongoTemplate mongoTemplate; | ||
|
||
public void saveUserLog(Object log){ | ||
mongoTemplate.save(log); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/daangn/survey/core/log/annotation/UserLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.daangn.survey.core.log.annotation; | ||
|
||
import com.daangn.survey.core.log.model.LogType; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserLogging { | ||
LogType type(); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/daangn/survey/core/log/component/LogEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.daangn.survey.core.log.component; | ||
|
||
import com.daangn.survey.common.util.shorturl.model.entity.ShortUrl; | ||
import com.daangn.survey.common.util.shorturl.repository.UrlRepository; | ||
import com.daangn.survey.core.log.LogRepository; | ||
import com.daangn.survey.core.log.model.LogEvent; | ||
import com.daangn.survey.core.log.model.ShortUrlLog; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class LogEventHandler { | ||
|
||
private final LogRepository logRepository; | ||
private final UrlRepository urlRepository; | ||
|
||
@Async | ||
@EventListener | ||
public void log(LogEvent event) { | ||
ShortUrl shortUrl = urlRepository.findShortUrlByShortUrl(event.getUrl()); | ||
|
||
logRepository.saveUserLog(new ShortUrlLog( | ||
shortUrl.resolveSurveyId(), | ||
event.getUrl(), | ||
event.getUserAgent(), | ||
event.getReferer() | ||
)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/daangn/survey/core/log/model/LogEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.daangn.survey.core.log.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class LogEvent { | ||
private String url; | ||
private String userAgent; | ||
private String referer; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/daangn/survey/core/log/model/LogType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.daangn.survey.core.log.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum LogType { | ||
SHORT_URL("ShortUrl"), | ||
; | ||
|
||
private String type; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/daangn/survey/core/log/model/ShortUrlLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.daangn.survey.core.log.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import javax.persistence.EnumType; | ||
import javax.persistence.Enumerated; | ||
|
||
@AllArgsConstructor | ||
@Getter @Setter | ||
@Document(collection = "userLogs") | ||
public class ShortUrlLog { | ||
@Enumerated(EnumType.STRING) | ||
private LogType logType; | ||
private Long surveyId; | ||
private String url; | ||
private String userAgent; | ||
private String referer; | ||
|
||
public ShortUrlLog(Long surveyId, String url, String userAgent, String referer) { | ||
this.logType = LogType.SHORT_URL; | ||
this.surveyId = surveyId; | ||
this.url = url; | ||
this.userAgent = userAgent; | ||
this.referer = referer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters