Skip to content

Commit 38081ed

Browse files
Corrections
Change-Id: I316363d8316b65ef2e293f40e9eacf71327a80c2
1 parent 129227f commit 38081ed

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/helper/Environment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public class Environment {
44

5+
public static final String LOG_REMOTE_IP = "LOG_REMOTE_IP";
6+
57
public String getVariable(String name) {
68
return System.getenv(name);
79
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/LogRemoteIPSettings.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,36 @@ public class LogRemoteIPSettings {
99

1010
private static final Logger LOGGER = LoggerFactory.getLogger(LogRemoteIPSettings.class);
1111

12-
private final String logRemoteIPSetting;
12+
private final boolean logRemoteIPSetting;
1313

1414
public LogRemoteIPSettings() {
1515
this(new Environment());
1616
}
1717

1818
LogRemoteIPSettings(Environment environment) {
19-
String tempLogRemoteIP = environment.getVariable("LogRemoteIP");
19+
String tempLogRemoteIP = environment.getVariable(Environment.LOG_REMOTE_IP);
2020

2121
if (tempLogRemoteIP != null) {
2222
if (tempLogRemoteIP.equals("true") || tempLogRemoteIP.equals("True") || tempLogRemoteIP.equals("TRUE")) {
23-
tempLogRemoteIP = "true";
23+
logRemoteIPSetting = true;
2424
LOGGER.info("Logging remote IPs has been ACTIVATED via environment variable");
2525
} else if (tempLogRemoteIP.equals("false") || tempLogRemoteIP.equals("False") || tempLogRemoteIP.equals(
2626
"FALSE")) {
27-
tempLogRemoteIP = "false";
27+
logRemoteIPSetting = false;
2828
LOGGER.info("Logging remote IPs has been DEACTIVATED via environment variable");
2929
} else {
3030
LOGGER.error("Logging remote IPs is DEACTIVATED by default. Environment variable \"LogRemoteIP = {}\" could not " +
3131
"be read. It should be set to true to activate Logging of remote IPs or to false to deactivate it",
3232
tempLogRemoteIP);
33-
tempLogRemoteIP = "false";
33+
logRemoteIPSetting = false;
3434
}
3535
} else {
36-
tempLogRemoteIP = "false";
36+
logRemoteIPSetting = false;
3737
LOGGER.info("Logging remote IPs is DEACTIVATED by default.");
3838
}
39-
logRemoteIPSetting = tempLogRemoteIP;
4039
}
4140

42-
public String getLogRemoteIPSetting() {
41+
public boolean getLogRemoteIPSetting() {
4342
return logRemoteIPSetting;
4443
}
4544
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class RequestLoggingFilter implements Filter {
3535
public static final String LOG_PROVIDER = "[SERVLET]";
3636
public static final String WRAP_RESPONSE_INIT_PARAM = "wrapResponse";
3737
public static final String WRAP_REQUEST_INIT_PARAM = "wrapRequest";
38-
public static final String LOG_REMOTE_IP = "LogRemoteIP";
3938

4039
private boolean wrapResponse = true;
4140
private boolean wrapRequest = true;
@@ -171,7 +170,7 @@ private void addHeaders(HttpServletRequest request, RequestRecord lrec) {
171170
: request.getRequestURI());
172171
lrec.addTag(Fields.METHOD, request.getMethod());
173172
lrec.addTag(Fields.PROTOCOL, getValue(request.getProtocol()));
174-
if (logRemoteIPSettings.getLogRemoteIPSetting().equals("true")) {
173+
if (logRemoteIPSettings.getLogRemoteIPSetting()) {
175174
lrec.addTag(Fields.REMOTE_IP, getValue(request.getRemoteAddr()));
176175
lrec.addTag(Fields.REMOTE_HOST, getValue(request.getRemoteHost()));
177176
lrec.addTag(Fields.REMOTE_PORT, Integer.toString(request.getRemotePort()));

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/LogRemoteIPSettingsTest.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,56 @@ public class LogRemoteIPSettingsTest {
1313
@Test
1414
public void testLogRemoteIPSettingsTrue() {
1515
Environment mockEnvironment = mock(Environment.class);
16-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("true");
17-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
16+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("true");
17+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), true);
1818
}
1919

2020
@Test
2121
public void testLogRemoteIPSettingsTrueWithCapitalT() {
2222
Environment mockEnvironment = mock(Environment.class);
23-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("True");
24-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
23+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("True");
24+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), true);
2525
}
2626

2727
@Test
2828
public void testLogRemoteIPSettingsTRUE() {
2929
Environment mockEnvironment = mock(Environment.class);
30-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("TRUE");
31-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "true");
30+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("TRUE");
31+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), true);
3232
}
3333

3434
@Test
3535
public void testLogRemoteIPSettingsFalse() {
3636
Environment mockEnvironment = mock(Environment.class);
37-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("false");
38-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
37+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("false");
38+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), false);
3939
}
4040

4141
@Test
4242
public void testLogRemoteIPSettingsFalseWithCapitalF() {
4343
Environment mockEnvironment = mock(Environment.class);
44-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("False");
45-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
44+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("False");
45+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), false);
4646
}
4747

4848
@Test
4949
public void testLogRemoteIPSettingsFALSE() {
5050
Environment mockEnvironment = mock(Environment.class);
51-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("FALSE");
52-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
51+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("FALSE");
52+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), false);
5353
}
5454

5555
@Test
5656
public void testLogRemoteIPSettingsInvalidEnvVariable() {
5757
Environment mockEnvironment = mock(Environment.class);
58-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn("someInvalidString");
59-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
58+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn("someInvalidString");
59+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), false);
6060
}
6161

6262
@Test
6363
public void testLogRemoteIPSettingsEmptyEnvVariable() {
6464
Environment mockEnvironment = mock(Environment.class);
65-
when(mockEnvironment.getVariable(RequestLoggingFilter.LOG_REMOTE_IP)).thenReturn(null);
66-
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), "false");
65+
when(mockEnvironment.getVariable(Environment.LOG_REMOTE_IP)).thenReturn(null);
66+
assertEquals(new LogRemoteIPSettings(mockEnvironment).getLogRemoteIPSetting(), false);
6767
}
68-
6968
}

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
137137
@Test
138138
public void testWithSettings() throws IOException, ServletException {
139139
LogRemoteIPSettings mockLogRemoteIPSettings = mock(LogRemoteIPSettings.class);
140-
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn("true");
140+
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn(true);
141141
HttpServletRequest mockReq = mock(HttpServletRequest.class);
142142
HttpServletResponse mockResp = mock(HttpServletResponse.class);
143143
PrintWriter mockWriter = mock(PrintWriter.class);
@@ -164,7 +164,7 @@ public void testWithSettings() throws IOException, ServletException {
164164
@Test
165165
public void testWithSettingsWithoutRemoteIP() throws IOException, ServletException {
166166
LogRemoteIPSettings mockLogRemoteIPSettings = mock(LogRemoteIPSettings.class);
167-
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn("false");
167+
when(mockLogRemoteIPSettings.getLogRemoteIPSetting()).thenReturn(false);
168168
HttpServletRequest mockReq = mock(HttpServletRequest.class);
169169
HttpServletResponse mockResp = mock(HttpServletResponse.class);
170170
PrintWriter mockWriter = mock(PrintWriter.class);

sample/manifest.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ applications:
66
- name: logging-sample-app
77
host: logging-sample-app
88
instances: 1
9-
path: target/logging-sample-app-2.1.1.war
9+
path: target/logging-sample-app-2.1.2.war
1010
env:
1111
RANDOM_SLEEP: true
12-
# Set LogRemoteIP: true to activate logging of remote IPs
13-
LogRemoteIP: false
12+
# Set LOG_REMOTE_IP: true to activate logging of remote IPs
13+
LOG_REMOTE_IP: false

0 commit comments

Comments
 (0)