Skip to content

Commit 8ceba01

Browse files
Fix NPE in DynamicLogLevelFilter
Configuration or Processor may not be available. In that case the filter should not create exceptions.
1 parent 306713d commit 8ceba01

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313

14-
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelConfiguration;
1514
import com.sap.hcp.cf.logging.servlet.dynlog.DynLogEnvironment;
15+
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelConfiguration;
1616
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelProcessor;
1717

1818
/**
@@ -64,7 +64,7 @@ public DynamicLogLevelFilter(ConcurrentInitializer<DynamicLogLevelConfiguration>
6464
protected DynamicLogLevelProcessor initialize() throws ConcurrentException {
6565
return getConfiguration().map(DynamicLogLevelConfiguration::getRsaPublicKey).map(
6666
DynamicLogLevelProcessor::new)
67-
.get();
67+
.orElse(null);
6868
}
6969
};
7070
}
@@ -94,7 +94,7 @@ public DynamicLogLevelFilter(ConcurrentInitializer<DynamicLogLevelConfiguration>
9494
*/
9595
protected Optional<DynamicLogLevelConfiguration> getConfiguration() {
9696
try {
97-
return Optional.of(configuration.get());
97+
return Optional.ofNullable(configuration.get());
9898
} catch (ConcurrentException cause) {
9999
LOG.debug("Cannot initialize dynamic log level environment. Will continue without this feature", cause);
100100
return Optional.empty();
@@ -109,7 +109,7 @@ protected Optional<DynamicLogLevelConfiguration> getConfiguration() {
109109
*/
110110
protected Optional<DynamicLogLevelProcessor> getProcessor() {
111111
try {
112-
return Optional.of(processor.get());
112+
return Optional.ofNullable(processor.get());
113113
} catch (ConcurrentException cause) {
114114
LOG.debug("Cannot initialize dynamic log level processor. Will continue without this feature", cause);
115115
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public void doesNotCallProcessorOnMissingHeader() throws Exception {
5656

5757
}
5858

59+
@Test
60+
public void doesNotFailOnDefaultConfiguration() throws Exception {
61+
new DynamicLogLevelFilter().doFilter(request, response, chain);
62+
}
63+
64+
@Test
65+
public void doesNotFailOnAbsentConfiguration() throws Exception {
66+
new DynamicLogLevelFilter(() -> null).doFilter(request, response, chain);
67+
68+
}
5969
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import javax.servlet.http.HttpServletRequest;
2626
import javax.servlet.http.HttpServletResponse;
2727

28-
import org.hamcrest.Matchers;
2928
import org.junit.Before;
3029
import org.junit.Rule;
3130
import org.junit.Test;
@@ -87,7 +86,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
8786
public void testSimple() throws IOException, ServletException {
8887
FilterChain mockFilterChain = mock(FilterChain.class);
8988

90-
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
89+
new NewRequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
9190
assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN));
9291
assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString()));
9392
assertThat(getField(Fields.REQUEST_ID), is(nullValue()));

0 commit comments

Comments
 (0)