Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle user-agent passed via the LogEventVO #4

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java
dist: trusty
jdk:
- openjdk7
- openjdk8
- oraclejdk8
- oraclejdk9
matrix:
Expand All @@ -10,6 +10,7 @@ matrix:
branches:
only:
- master
- /^hotfix\/.*$/
before_install:
- mkdir -p ~/.m2; wget -q -O ~/.m2/settings.xml https://raw.githubusercontent.com/AtlasOfLivingAustralia/travis-build-configuration/master/travis_maven_settings.xml
script: mvn clean install deploy
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/ala/client/appender/RestfulAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,17 @@ private Map<String, String> constructHttpHeaders(LoggingEvent event) {
Map<String, String> headers = new HashMap<String, String>();

if (event != null) {
String userAgent = (String) event.getMDC(Constants.USER_AGENT_PARAM);
if (StringUtils.isBlank(userAgent)) {
userAgent = Constants.UNDEFINED_USER_AGENT_VALUE;

Object message = event.getMessage();
if (message instanceof LogEventVO && !StringUtils.isBlank(((LogEventVO) message).getUserAgent())) {
headers.put(Constants.USER_AGENT_PARAM, ((LogEventVO) message).getUserAgent());
} else {
String userAgent = (String) event.getMDC(Constants.USER_AGENT_PARAM);
if (StringUtils.isBlank(userAgent)) {
userAgent = Constants.UNDEFINED_USER_AGENT_VALUE;
}
headers.put(Constants.USER_AGENT_PARAM, userAgent);
}
headers.put(Constants.USER_AGENT_PARAM, userAgent);
}

return headers;
Expand Down
53 changes: 42 additions & 11 deletions src/main/java/org/ala/client/model/LogEventVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private static Map<String, Integer> mapToInt(ConcurrentMap<String, AtomicInteger
private int eventTypeId = 0;

private String userIP = "";

private String userAgent = "";

private Map<String, Integer> recordCounts = new HashMap<>();

Expand Down Expand Up @@ -120,7 +122,33 @@ public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, S
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, month, mapToInt(recordCounts));
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String month, Map<String, Integer> recordCounts) {
public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String month, ConcurrentMap<String, AtomicInteger> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, month, mapToInt(recordCounts), sourceUrl);
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String month, Map<String, Integer> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, month, recordCounts);
this.sourceUrl = sourceUrl;
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String userAgent, Map<String, Integer> recordCounts) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, userAgent, null, recordCounts);
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String userAgent, String month, ConcurrentMap<String, AtomicInteger> recordCounts) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, userAgent, month, mapToInt(recordCounts));
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String userAgent, String month, ConcurrentMap<String, AtomicInteger> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, userAgent, month, mapToInt(recordCounts), sourceUrl);
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String userAgent, String month, Map<String, Integer> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, userAgent, month, recordCounts);
this.sourceUrl = sourceUrl;
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String userAgent, String month, Map<String, Integer> recordCounts) {
this.sourceTypeId = sourceTypeId;
this.reasonTypeId = reasonTypeId;
this.eventTypeId = eventTypeId;
Expand All @@ -133,6 +161,10 @@ public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, S
if(userIP != null){
this.userIP = userIP;
}
if (userAgent != null) {
this.userAgent = userAgent;
}

if(month != null){
this.month = month;
}
Expand All @@ -141,15 +173,6 @@ public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, S
}
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String month, ConcurrentMap<String, AtomicInteger> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, month, mapToInt(recordCounts), sourceUrl);
}

public LogEventVO(int eventTypeId, Integer reasonTypeId, Integer sourceTypeId, String userEmail, String comment, String userIP, String month, Map<String, Integer> recordCounts, String sourceUrl) {
this(eventTypeId, reasonTypeId, sourceTypeId, userEmail, comment, userIP, month, recordCounts);
this.sourceUrl = sourceUrl;
}

public String getComment() {
return this.comment;
}
Expand Down Expand Up @@ -194,7 +217,15 @@ public String getUserIP() {
public void setUserIP(String userIP) {
this.userIP = userIP;
}


public String getUserAgent() {
return userAgent;
}

public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

public String getMonth() {
return month;
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/ala/client/RestfulClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ala.client;

import org.ala.client.appender.RestfulAppender;
import org.ala.client.model.LogEventVO;
import org.ala.client.util.Constants;
import org.ala.client.util.RestfulClient;
import org.apache.commons.httpclient.HttpClient;
Expand All @@ -16,6 +17,11 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -80,4 +86,32 @@ public void testUserAgentSetToValueProvidedInContext() throws Exception {
assertEquals("myUserAgent", sentValue);
}

@Test
public void testUserAgentSetToValueProvidedEvent() throws Exception {
HttpClient mockHttpClient = mock(HttpClient.class);
when(mockHttpClient.getParams()).thenReturn(mock(HttpClientParams.class));
whenNew(HttpClient.class).withAnyArguments().thenReturn(mockHttpClient);

LogEventVO logEventVO = new LogEventVO(1, 1, 1, "userEmail", "For doing some research with..", "123.11.01.112", "myUserAgent", new HashMap<String, Integer>() {{
put("dp123", 32);
put("dr143", 22);
put("ins322", 55);
}});

RestfulAppender appender = new RestfulAppender();
appender.setUrlTemplate("somurl");
LoggingEvent event = mock(LoggingEvent.class);
when(event.getMessage()).thenReturn(logEventVO);

// when(event.getMDC(eq(Constants.USER_AGENT_PARAM))).thenReturn("myUserAgent");
appender.doAppend(event);

ArgumentCaptor<HttpMethod> captor = ArgumentCaptor.forClass(HttpMethod.class);
verify(mockHttpClient).executeMethod(captor.capture());

String sentValue = captor.getValue().getRequestHeader(Constants.USER_AGENT_PARAM).getValue();
assertEquals("myUserAgent", sentValue);
}


}