Skip to content

Commit

Permalink
Added logger for from header
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Jan 18, 2025
1 parent 1e8a9fe commit 1b2c822
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
27 changes: 27 additions & 0 deletions server/src/main/java/nl/altindag/server/aspect/LogFromHeader.java
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> logs = logCaptor.getLogs();
assertThat(logs).isEmpty();
}

}

0 comments on commit 1b2c822

Please sign in to comment.