Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cxf 8387 #776

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 2880
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Cache SonarCloud packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B -fn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -DskipTests
5 changes: 5 additions & 0 deletions core/src/main/java/org/apache/cxf/annotations/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
*/
boolean pretty() default false;

/**
* For XML content, turn on regex printing in the logs
*/
boolean regex() default false;

/**
* Ignore binary payloads by default
*/
Expand Down
49 changes: 44 additions & 5 deletions core/src/main/java/org/apache/cxf/feature/LoggingFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ public LoggingFeature(String in, String out, int lim, boolean p) {
super(new Portable(in, out, lim, p));
}

public LoggingFeature(String in, String out, int lim, boolean p, boolean showBinary) {
super(new Portable(in, out, lim, p, showBinary));
public LoggingFeature(String in, String out, int lim, boolean p, boolean r) {
super(new Portable(in, out, lim, p, r));
}

public LoggingFeature(String in, String out, int lim, boolean p, boolean r, boolean showBinary) {
super(new Portable(in, out, lim, p, r, showBinary));
}

public LoggingFeature(Logging annotation) {
Expand All @@ -87,10 +91,18 @@ public boolean isPrettyLogging() {
return delegate.isPrettyLogging();
}

public boolean isRegexLogging() {
return delegate.isRegexLogging();
}

public void setPrettyLogging(boolean prettyLogging) {
delegate.setPrettyLogging(prettyLogging);
}

public void setRegexLogging(boolean regexLogging) {
delegate.setRegexLogging(regexLogging);
}

public static class Portable implements AbstractPortableFeature {
private static final int DEFAULT_LIMIT = AbstractLoggingInterceptor.DEFAULT_LIMIT;
private static final LoggingInInterceptor IN = new LoggingInInterceptor(DEFAULT_LIMIT);
Expand All @@ -100,6 +112,7 @@ public static class Portable implements AbstractPortableFeature {
String inLocation;
String outLocation;
boolean prettyLogging;
boolean regexLogging;
boolean showBinary;

int limit = DEFAULT_LIMIT;
Expand Down Expand Up @@ -127,8 +140,16 @@ public Portable(String in, String out, int lim, boolean p) {
prettyLogging = p;
}

public Portable(String in, String out, int lim, boolean p, boolean showBinary) {
this(in, out, lim, p);
public Portable(String in, String out, int lim, boolean p, boolean r) {
inLocation = in;
outLocation = out;
limit = lim;
prettyLogging = p;
regexLogging = r;
}

public Portable(String in, String out, int lim, boolean p, boolean r, boolean showBinary) {
this(in, out, lim, p, r);
this.showBinary = showBinary;
}

Expand All @@ -137,13 +158,14 @@ public Portable(Logging annotation) {
outLocation = annotation.outLocation();
limit = annotation.limit();
prettyLogging = annotation.pretty();
regexLogging = annotation.regex();
showBinary = annotation.showBinary();
}

@Override
public void doInitializeProvider(InterceptorProvider provider, Bus bus) {
if (limit == DEFAULT_LIMIT && inLocation == null
&& outLocation == null && !prettyLogging) {
&& outLocation == null && !prettyLogging && !regexLogging) {
provider.getInInterceptors().add(IN);
provider.getInFaultInterceptors().add(IN);
provider.getOutInterceptors().add(OUT);
Expand All @@ -152,10 +174,12 @@ public void doInitializeProvider(InterceptorProvider provider, Bus bus) {
LoggingInInterceptor in = new LoggingInInterceptor(limit);
in.setOutputLocation(inLocation);
in.setPrettyLogging(prettyLogging);
in.setRegexLogging(regexLogging);
in.setShowBinaryContent(showBinary);
LoggingOutInterceptor out = new LoggingOutInterceptor(limit);
out.setOutputLocation(outLocation);
out.setPrettyLogging(prettyLogging);
out.setRegexLogging(regexLogging);
out.setShowBinaryContent(showBinary);

provider.getInInterceptors().add(in);
Expand Down Expand Up @@ -185,12 +209,27 @@ public int getLimit() {
public boolean isPrettyLogging() {
return prettyLogging;
}

/**
*/
public boolean isRegexLogging() {
return regexLogging;
}

/**
* Turn pretty logging of XML content on/off
* @param prettyLogging
*/
public void setPrettyLogging(boolean prettyLogging) {
this.prettyLogging = prettyLogging;
}

/**
* Turn regex logging on/off
* @param regexLogging
*/
public void setRegexLogging(boolean regexLogging) {
this.regexLogging = regexLogging;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public abstract class AbstractLoggingInterceptor extends AbstractPhaseIntercepto
protected long threshold = -1;
protected PrintWriter writer;
protected boolean prettyLogging;
protected boolean regexLogging;
private boolean showBinaryContent;
private boolean showMultipartContent = true;
private List<String> binaryContentMediaTypes = BINARY_CONTENT_MEDIA_TYPES;
Expand Down Expand Up @@ -148,10 +149,18 @@ public void setPrettyLogging(boolean flag) {
prettyLogging = flag;
}

public void setRegexLogging(boolean flag) {
regexLogging = flag;
}

public boolean isPrettyLogging() {
return prettyLogging;
}

public boolean isRegexLogging() {
return regexLogging;
}

public void setInMemThreshold(long t) {
threshold = t;
}
Expand All @@ -164,7 +173,7 @@ protected void writePayload(StringBuilder builder, CachedOutputStream cos,
String encoding, String contentType, boolean truncated)
throws Exception {
// Just transform the XML message when the cos has content
if (!truncated && isPrettyLogging() && contentType != null && contentType.contains("xml")
if (!truncated && isPrettyLogging() && isRegexLogging() && contentType != null && contentType.contains("xml")
&& !contentType.toLowerCase().contains("multipart/related") && cos.size() > 0) {

StringWriter swriter = new StringWriter();
Expand Down Expand Up @@ -205,6 +214,7 @@ protected void writePayload(StringBuilder builder,
String contentType)
throws Exception {
if (isPrettyLogging()
&& isRegexLogging()
&& contentType != null
&& contentType.contains("xml")
&& stringWriter.getBuffer().length() > 0) {
Expand Down
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<site.deploy.url>scp://people.apache.org/www/cxf.apache.org/maven-site</site.deploy.url>
<maven-owasp-plugin-version>6.0.4</maven-owasp-plugin-version>
<sonar.projectKey>ayojava_Software-Engineering-Methodologies</sonar.projectKey>
<sonar.organization>software-engrg-methodologies</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>
<distributionManagement>
<repository>
Expand Down Expand Up @@ -477,6 +480,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -543,11 +549,6 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
Expand Down Expand Up @@ -738,4 +739,4 @@
</plugin>
</plugins>
</reporting>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.cxf.ext.logging.event.LogEvent;
import org.apache.cxf.ext.logging.event.LogEventSender;
import org.apache.cxf.ext.logging.event.PrettyLoggingFilter;
import org.apache.cxf.ext.logging.event.RegexLoggingFilter;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
Expand Down Expand Up @@ -93,6 +94,12 @@ public void setPrettyLogging(boolean prettyLogging) {
}
}

public void setRegexLogging(boolean regexLogging) {
if (sender instanceof RegexLoggingFilter) {
((RegexLoggingFilter)this.sender).setRegexLogging(regexLogging);
}
}

protected boolean shouldLogContent(LogEvent event) {
return event.isBinaryContent() && logBinary
|| event.isMultipartContent() && logMultipart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
*/
boolean pretty() default false;

/**
* For XML content, turn on regex printing in the logs
*/
boolean regex() default false;

/**
* Log binary payloads by default
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ public class LoggingBusLifecycleListener implements BusLifeCycleListener {

static final boolean FORCE_LOGGING;
static final boolean FORCE_PRETTY;
static final boolean FORCE_REGEX;
static {
boolean b = false;
boolean pretty = false;
boolean reg = false;
try {
String prop = System.getProperty("org.apache.cxf.logging.enabled", "false");
if ("pretty".equals(prop)) {
if (("pretty".equals(prop)) && ("regex".equals(prop))) {
b = true;
pretty = true;
reg = true;
} else if ("pretty".equals(prop)) {
b = true;
pretty = true;
} else if ("regex".equals(prop)) {
b = true;
reg = true;
} else {
b = Boolean.parseBoolean(prop);
//treat these all the same
Expand All @@ -51,6 +60,7 @@ public class LoggingBusLifecycleListener implements BusLifeCycleListener {
}
FORCE_LOGGING = b;
FORCE_PRETTY = pretty;
FORCE_REGEX = reg;
}

private final Bus bus;
Expand All @@ -65,6 +75,7 @@ public void initComplete() {
if (FORCE_LOGGING) {
LoggingFeature feature = new LoggingFeature();
feature.setPrettyLogging(FORCE_PRETTY);
feature.setRegexLogging(FORCE_REGEX);
bus.getFeatures().add(feature);
feature.initialize(bus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private void addLoggingSupport(Endpoint endpoint, Bus bus, Logging annotation) {
LoggingFeature lf = new LoggingFeature();
lf.setInMemThreshold(annotation.inMemThresHold());
lf.setPrettyLogging(annotation.pretty());
lf.setRegexLogging(annotation.regex());
lf.setLimit(annotation.limit());
lf.setLogBinary(annotation.logBinary());
lf.setLogMultipart(annotation.logMultipart());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.cxf.common.injection.NoJSR250Annotations;
import org.apache.cxf.ext.logging.event.LogEventSender;
import org.apache.cxf.ext.logging.event.PrettyLoggingFilter;
import org.apache.cxf.ext.logging.event.RegexLoggingFilter;
import org.apache.cxf.ext.logging.slf4j.Slf4jEventSender;
import org.apache.cxf.ext.logging.slf4j.Slf4jVerboseEventSender;
import org.apache.cxf.feature.AbstractPortableFeature;
Expand Down Expand Up @@ -78,6 +79,10 @@ public void setPrettyLogging(boolean prettyLogging) {
delegate.setPrettyLogging(prettyLogging);
}

public void setRegexLogging(boolean regexLogging) {
delegate.setRegexLogging(regexLogging);
}

public void setLogBinary(boolean logBinary) {
delegate.setLogBinary(logBinary);
}
Expand Down Expand Up @@ -172,13 +177,18 @@ public static class Portable implements AbstractPortableFeature {
private LoggingOutInterceptor out;
private PrettyLoggingFilter inPrettyFilter;
private PrettyLoggingFilter outPrettyFilter;
private RegexLoggingFilter inRegexFilter;
private RegexLoggingFilter outRegexFilter;


public Portable() {
LogEventSender sender = new Slf4jVerboseEventSender();
inPrettyFilter = new PrettyLoggingFilter(sender);
outPrettyFilter = new PrettyLoggingFilter(sender);
in = new LoggingInInterceptor(inPrettyFilter);
out = new LoggingOutInterceptor(outPrettyFilter);
inRegexFilter = new RegexLoggingFilter(inPrettyFilter);
outRegexFilter = new RegexLoggingFilter(outPrettyFilter);
in = new LoggingInInterceptor(inRegexFilter);
out = new LoggingOutInterceptor(outRegexFilter);
}

@Override
Expand Down Expand Up @@ -217,6 +227,11 @@ public void setPrettyLogging(boolean prettyLogging) {
this.outPrettyFilter.setPrettyLogging(prettyLogging);
}

public void setRegexLogging(boolean regexLogging) {
this.inRegexFilter.setRegexLogging(regexLogging);
this.outRegexFilter.setRegexLogging(regexLogging);
}

/**
* Log binary content?
* @param logBinary defaults to false
Expand Down
Loading