Skip to content

Commit

Permalink
Merge branch 'master' into #1238-keyalias-support-for-ssl-context
Browse files Browse the repository at this point in the history
  • Loading branch information
t-burch authored Aug 26, 2024
2 parents 96a1009 + 4be4b30 commit 3907b42
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 203 deletions.
2 changes: 1 addition & 1 deletion annot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<groupId>org.membrane-soa</groupId>
<artifactId>service-proxy-parent</artifactId>
<relativePath>../pom.xml</relativePath>
<version>5.7.1-SNAPSHOT</version>
<version>5.7.2-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion core/.factorypath
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><factorypath>
<factorypathentry enabled="true" id="M2_REPO/org/membrane-soa/service-proxy-annot/5.7.1-SNAPSHOT/service-proxy-annot-5.7.1-SNAPSHOT.jar" kind="VARJAR" runInBatchMode="false"/>
<factorypathentry enabled="true" id="M2_REPO/org/membrane-soa/service-proxy-annot/5.7.2-SNAPSHOT/service-proxy-annot-5.7.2-SNAPSHOT.jar" kind="VARJAR" runInBatchMode="false"/>
</factorypath>
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<groupId>org.membrane-soa</groupId>
<artifactId>service-proxy-parent</artifactId>
<relativePath>../pom.xml</relativePath>
<version>5.7.1-SNAPSHOT</version>
<version>5.7.2-SNAPSHOT</version>
</parent>

<!-- Open Telemetry -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.predic8.membrane.core.http.Header;
import com.predic8.membrane.core.http.HeaderField;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.InterceptorFlowController;
import com.predic8.membrane.core.interceptor.Outcome;
import com.predic8.membrane.core.interceptor.opentelemetry.exporter.OtelExporter;
import com.predic8.membrane.core.interceptor.opentelemetry.exporter.OtlpExporter;
Expand All @@ -33,6 +34,8 @@
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;
import static com.predic8.membrane.core.interceptor.opentelemetry.HTTPTraceContextUtil.getContextFromRequestHeader;
Expand All @@ -55,6 +58,8 @@ public class OpenTelemetryInterceptor extends AbstractInterceptor {

private boolean logBody = false;

private static final Logger log = LoggerFactory.getLogger(OpenTelemetryInterceptor.class);

@Override
public void init() throws Exception {
otel = OpenTelemetryConfigurator.openTelemetry("Membrane", exporter, getSampleRate());
Expand Down Expand Up @@ -127,7 +132,13 @@ private static void setSpanHttpHeaderAttributes(Header header, Span span) {
}

private void setSpanOpenAPIAttributes(Exchange exc, Span span) {
OpenAPIRecord record = exc.getProperty(OPENAPI_RECORD, OpenAPIRecord.class);
OpenAPIRecord record;
try {
record = exc.getProperty(OPENAPI_RECORD, OpenAPIRecord.class);
} catch (ClassCastException e) {
log.debug("No OpenAPI to report to OpenTelemetry.");
return;
}
if (record != null) {
span.setAttribute("openapi.title", record.getApi().getInfo().getTitle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ public class OtlpExporter implements OtelExporter {
private static final int TIMEOUT_SECONDS = 30;
private String host = "localhost";
private Integer port;
private String path = "";
private OtlpType transport = GRPC;
private final List<addHeader> headers = new ArrayList<>();

private boolean secured = false;

public String getEndpointUrl() {
String endpoint = format("%s://%s:%d", isSecured() ? "https" : "http", host, getProtocolPort(port, transport));
if (transport == HTTP) {
endpoint += "/v1/traces";
return format("%s://%s:%d%s", isSecured() ? "https" : "http", host, getProtocolPort(port, transport), getPathExtension());
}

@SuppressWarnings("StringEquality")
private String getPathExtension() {
if (path == "" && transport == HTTP) {
return "/v1/traces";
}
return endpoint;
return path;
}

private int getProtocolPort(Integer port, OtlpType trans) {
Expand Down Expand Up @@ -114,6 +119,9 @@ public void setSecured(boolean secured) {
this.secured = secured;
}

@MCAttribute
public void setPath(String path) { this.path = path; }

@MCAttribute
public void setHost(String host) {
this.host = host;
Expand All @@ -124,6 +132,8 @@ public void setPort(int port) {
this.port = port;
}

public String getPath() { return path; }

@Override
public String getHost() {
return host;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.predic8.membrane.core.interceptor.templating;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.MCTextContent;
import com.predic8.membrane.core.beautifier.JSONBeautifier;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Message;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;
import com.predic8.membrane.core.util.TextUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringReader;

import static com.predic8.membrane.core.http.MimeType.*;
import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;
import static com.predic8.membrane.core.util.TextUtil.unifyIndent;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.text.StringEscapeUtils.escapeHtml4;

@MCElement(name = "static", mixed = true)
public class StaticInterceptor extends AbstractInterceptor {

protected String location;

protected String textTemplate;

protected String contentType = TEXT_PLAIN;

protected Boolean pretty = false;

protected final JSONBeautifier jsonBeautifier = new JSONBeautifier();

protected static final Logger log = LoggerFactory.getLogger("StaticInterceptor");

public StaticInterceptor() {
name = "Static";
}

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
return handleInternal(exc.getRequest());
}

@Override
public Outcome handleResponse(Exchange exc) throws Exception {
return handleInternal(exc.getResponse());
}

private Outcome handleInternal(Message msg) {
msg.setBodyContent(getTemplateBytes());
msg.getHeader().setContentType(getContentType());
return CONTINUE;
}

private byte @NotNull [] getTemplateBytes() {
if (!pretty)
return textTemplate.getBytes(UTF_8);

return switch (contentType) {
case APPLICATION_JSON -> prettifyJson(textTemplate).getBytes(UTF_8);
case APPLICATION_XML, APPLICATION_SOAP, TEXT_HTML, TEXT_XML, TEXT_HTML_UTF8, TEXT_XML_UTF8 -> prettifyXML(textTemplate).getBytes(UTF_8);
default -> unifyIndent(textTemplate).getBytes(UTF_8);
};
}

private String prettifyXML(String text) {
try {
return TextUtil.formatXML(new StringReader(text));
} catch (Exception e) {
log.warn("Failed to format XML", e);
return text;
}
}

String prettifyJson(String text) {
try {
return jsonBeautifier.beautify(text);
} catch (IOException e) {
log.warn("Failed to format JSON", e);
return text;
}
}

@Override
public void init() throws Exception {
if (this.getLocation() != null && (getTextTemplate() != null && !getTextTemplate().isBlank())) {
throw new IllegalStateException("On <" + getName() + ">, ./text() and ./@location cannot be set at the same time.");
}
}

public String getLocation() {
return location;
}

@MCAttribute
public void setLocation(String location){
this.location = location;
}

public String getTextTemplate() {
return textTemplate;
}

@MCTextContent
public void setTextTemplate(String textTemplate) {
this.textTemplate = textTemplate;
}

protected String getName() {
return getClass().getAnnotation(MCElement.class).name();
}

public String getContentType() {
return contentType;
}

@MCAttribute
public void setContentType(String contentType) {
this.contentType = contentType;
}

public Boolean getPretty() {
return pretty;
}

@MCAttribute
public void setPretty(String pretty) {
this.pretty = Boolean.valueOf(pretty);
}

private String formatAsHtml(String plaintext) {
return String.join("<br />", escapeHtml4(plaintext).split("\n"));
}

@Override
public String getShortDescription() {
return formatAsHtml(textTemplate);
}
}
Loading

0 comments on commit 3907b42

Please sign in to comment.