-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
228 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
vpro-shared-rs/src/main/java/nl/vpro/rs/interceptors/AccessLogInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package nl.vpro.rs.interceptors; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.regex.Pattern; | ||
|
||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import org.slf4j.MDC; | ||
import org.springframework.jmx.export.annotation.ManagedAttribute; | ||
import org.springframework.jmx.export.annotation.ManagedResource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import nl.vpro.logging.mdc.MDCConstants; | ||
import nl.vpro.logging.simple.Slf4jSimpleLogger; | ||
import nl.vpro.util.*; | ||
|
||
|
||
/** | ||
* TODO: seems a more generic utility. Move it to shared. | ||
*/ | ||
@Provider | ||
@Component | ||
@Slf4j | ||
@ManagedResource | ||
public class AccessLogInterceptor implements ContainerRequestFilter { | ||
|
||
private Pattern forUser = Pattern.compile("^(rcrs|npo-sourcing-service.*|functional-tests.*|nep-backend)$"); | ||
|
||
private Pattern forContentType = Pattern.compile("^application/(xml|json)"); | ||
|
||
private int truncateAfter = 2048; | ||
|
||
private final Map<String, AtomicLong> counters = new ConcurrentHashMap<>(); | ||
|
||
private Path filesPath = null; | ||
|
||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
|
||
if ("POST".equals(requestContext.getMethod())) { | ||
String contentType = requestContext.getHeaderString("content-type"); | ||
String user = MDC.get(MDCConstants.USER_NAME); | ||
if ((user != null && forUser.matcher(user).matches()) && | ||
(contentType != null && forContentType.matcher(contentType).matches()) | ||
) { | ||
long count = counters.computeIfAbsent(user, k -> new AtomicLong()).incrementAndGet(); | ||
MDC.put(MDCConstants.USER_COUNT, String.valueOf(count)); | ||
TruncatedObservableInputStream inputStream; | ||
if (filesPath == null) { | ||
inputStream = new LoggingInputStream(Slf4jSimpleLogger.slf4j(log), requestContext.getEntityStream()); | ||
} else { | ||
Path file = filesPath.resolve(user + "-" + count + ".log"); | ||
inputStream = new FileInputStreamTee(Files.newOutputStream(file), requestContext.getEntityStream()); | ||
} | ||
inputStream.setTruncateAfter(truncateAfter); | ||
requestContext.setEntityStream(inputStream); | ||
} else { | ||
log.trace("Not logging body for {} {}", user, contentType); | ||
} | ||
} | ||
} | ||
|
||
@ManagedAttribute | ||
public String getForUser() { | ||
return forUser.pattern(); | ||
} | ||
|
||
@ManagedAttribute | ||
public void setForUser(String pattern) { | ||
this.forUser = Pattern.compile(pattern); | ||
} | ||
|
||
@ManagedAttribute | ||
public String getForContentType() { | ||
return forContentType.pattern(); | ||
} | ||
|
||
@ManagedAttribute | ||
public void setForContentType(String forContentType) { | ||
this.forContentType = Pattern.compile(forContentType); | ||
} | ||
|
||
@ManagedAttribute | ||
public int getTruncateAfter() { | ||
return truncateAfter; | ||
} | ||
|
||
@ManagedAttribute | ||
public void setTruncateAfter(int truncateAfter) { | ||
this.truncateAfter = truncateAfter; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
vpro-shared-util/src/main/java/nl/vpro/util/FileInputStreamTee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package nl.vpro.util; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.io.*; | ||
|
||
|
||
/** | ||
* A wrapper for an {@link InputStream} that logs it's first bytes. | ||
*/ | ||
@Setter | ||
@Getter | ||
public class FileInputStreamTee extends TruncatedObservableInputStream { | ||
|
||
private final OutputStream fileOutputStream; | ||
|
||
public FileInputStreamTee(OutputStream fileOutputStream, InputStream wrapped) { | ||
super(wrapped); | ||
this.fileOutputStream = fileOutputStream; | ||
} | ||
|
||
@Override | ||
void write(byte[] buffer, int offset, int effectiveLength) throws IOException { | ||
fileOutputStream.write(buffer, offset, effectiveLength); | ||
|
||
} | ||
|
||
@Override | ||
void write(int value) throws IOException { | ||
fileOutputStream.write(value); | ||
} | ||
|
||
@Override | ||
void closed(long count, boolean truncated) throws IOException { | ||
fileOutputStream.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
vpro-shared-util/src/main/java/nl/vpro/util/TruncatedObservableInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nl.vpro.util; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.io.input.ObservableInputStream; | ||
|
||
|
||
/** | ||
* A wrapper for an {@link InputStream} that observes it's first bytes. | ||
* @since 5.5 | ||
* @author Michiel Meeuwissen | ||
*/ | ||
@Setter | ||
@Getter | ||
public abstract class TruncatedObservableInputStream extends ObservableInputStream { | ||
|
||
private int truncateAfter = 2048; | ||
protected TruncatedObservableInputStream(InputStream wrapped) { | ||
super(wrapped); | ||
add(new Observer() { | ||
private boolean truncated = false; | ||
private long count = 0; | ||
|
||
@Override | ||
public void data(final byte[] buffer, final int offset, final int length) throws IOException{ | ||
if (count < truncateAfter) { | ||
int effectiveLength = Math.min(truncateAfter - (int) count, length); | ||
truncated = effectiveLength < length; | ||
write(buffer, offset, effectiveLength); | ||
} | ||
count += length; | ||
} | ||
|
||
@Override | ||
public void data(final int value) throws IOException{ | ||
if (count < truncateAfter) { | ||
write(value); | ||
} else { | ||
truncated = true; | ||
} | ||
count++; | ||
} | ||
|
||
@Override | ||
public void closed() throws IOException { | ||
TruncatedObservableInputStream.this.closed(count, true); | ||
} | ||
}); | ||
} | ||
|
||
abstract void write(byte[] buffer, int offset, int length) throws IOException; | ||
abstract void write(int value) throws IOException; | ||
|
||
void closed(long count, boolean truncated) throws IOException { | ||
|
||
} | ||
} |