diff --git a/server/src/main/java/nl/altindag/server/aspect/LogFromHeader.java b/server/src/main/java/nl/altindag/server/aspect/LogFromHeader.java new file mode 100644 index 0000000..529ca12 --- /dev/null +++ b/server/src/main/java/nl/altindag/server/aspect/LogFromHeader.java @@ -0,0 +1,27 @@ +/* + * Copyright 2018 Thunderberry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.altindag.server.aspect; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface LogFromHeader { + +} diff --git a/server/src/main/java/nl/altindag/server/aspect/LogFromHeaderAspect.java b/server/src/main/java/nl/altindag/server/aspect/LogFromHeaderAspect.java new file mode 100644 index 0000000..ea5df81 --- /dev/null +++ b/server/src/main/java/nl/altindag/server/aspect/LogFromHeaderAspect.java @@ -0,0 +1,48 @@ +/* + * Copyright 2018 Thunderberry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.altindag.server.aspect; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import static java.util.Objects.nonNull; + +@Aspect +@Configuration +@EnableAspectJAutoProxy +public class LogFromHeaderAspect { + + private static final Logger LOGGER = LogManager.getLogger(LogFromHeaderAspect.class); + private static final String HEADER_KEY_FROM = "from"; + + @Before("@annotation(nl.altindag.server.aspect.LogFromHeader)") + public void logIfPresent() { + String from = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) + .getRequest() + .getHeader(HEADER_KEY_FROM); + + if (nonNull(from)) { + LOGGER.info("Hello there! It seems like {} is knocking on my door...", from); + } + } + +} diff --git a/server/src/main/java/nl/altindag/server/controller/HelloWorldController.java b/server/src/main/java/nl/altindag/server/controller/HelloWorldController.java index a7e4cd1..82a1870 100644 --- a/server/src/main/java/nl/altindag/server/controller/HelloWorldController.java +++ b/server/src/main/java/nl/altindag/server/controller/HelloWorldController.java @@ -23,11 +23,13 @@ import nl.altindag.server.aspect.LogCertificate; import nl.altindag.server.aspect.LogClientType; +import nl.altindag.server.aspect.LogFromHeader; import org.springframework.web.bind.annotation.RequestHeader; @Controller public class HelloWorldController { + @LogFromHeader @LogClientType @LogCertificate(detailed = true) @GetMapping(value = "/api/hello", produces = MediaType.TEXT_PLAIN_VALUE) diff --git a/server/src/test/java/nl/altindag/server/aspect/LogFromAspectShould.java b/server/src/test/java/nl/altindag/server/aspect/LogFromAspectShould.java new file mode 100644 index 0000000..c751720 --- /dev/null +++ b/server/src/test/java/nl/altindag/server/aspect/LogFromAspectShould.java @@ -0,0 +1,59 @@ +/* + * Copyright 2018 Thunderberry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.altindag.server.aspect; + +import nl.altindag.log.LogCaptor; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class LogFromAspectShould { + + private final LogFromHeaderAspect logFromHeaderAspect = new LogFromHeaderAspect(); + + @Test + void logFromHeaderIfPresent() { + LogCaptor logCaptor = LogCaptor.forClass(LogFromHeaderAspect.class); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("from", "Foo"); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + logFromHeaderAspect.logIfPresent(); + + List logs = logCaptor.getLogs(); + assertThat(logs).containsExactly("Hello there! It seems like Foo is knocking on my door..."); + } + + @Test + void notLogFromHeaderIfAbsent() { + LogCaptor logCaptor = LogCaptor.forClass(LogFromHeaderAspect.class); + + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + + logFromHeaderAspect.logIfPresent(); + + List logs = logCaptor.getLogs(); + assertThat(logs).isEmpty(); + } + +}