Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Nov 22, 2024
1 parent 9b66249 commit c1e4644
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
// set its common attributes such as tracing etc
processAuditStatus(logMediator,elem);

boolean containMessageTemplate = false;
OMElement messageElement = elem.getFirstChildWithName(ELEMENT_MESSAGE_Q);
if (messageElement != null && messageElement.getText() != null) {
logMediator.setMessageTemplate(messageElement.getText());
logMediator.setLogLevel(LogMediator.MESSAGE_TEMPLATE);
containMessageTemplate = true;
}

// Set the high level set of properties to be logged (i.e. log level)
Expand All @@ -90,6 +91,10 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
handleException("Invalid log level. Level has to be one of the following : "
+ "simple, headers, full, custom");
}
} else {
if (containMessageTemplate) {
logMediator.setLogLevel(LogMediator.MESSAGE_TEMPLATE);
}
}

// Set the log statement category (i.e. INFO, DEBUG, etc..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OMElement serializeSpecificMediator(Mediator m) {
OMElement log = fac.createOMElement("log", synNS);
saveTracingState(log,mediator);

if (StringUtils.isBlank(mediator.getMessageTemplate()) && mediator.getLogLevel() != LogMediator.SIMPLE) {
if (mediator.getLogLevel() != LogMediator.MESSAGE_TEMPLATE) {
log.addAttribute(fac.createOMAttribute(
"level", nullNS,
mediator.getLogLevel() == LogMediator.HEADERS ? "headers" :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class LogMediator extends AbstractMediator {
/** The holder for the custom properties */
private final List<MediatorProperty> properties = new ArrayList<MediatorProperty>();

private String messageTemplate;
private String messageTemplate = "";

/**
* Logs the current message according to the supplied semantics
Expand Down Expand Up @@ -142,28 +142,33 @@ public boolean mediate(MessageContext synCtx) {
private String getLogMessage(MessageContext synCtx) {
switch (logLevel) {
case CUSTOM:
case MESSAGE_TEMPLATE:
return getCustomLogMessage(synCtx);
case SIMPLE:
return getSimpleLogMessage(synCtx);
case HEADERS:
return getHeadersLogMessage(synCtx);
case FULL:
return getFullLogMessage(synCtx);
case MESSAGE_TEMPLATE:
return processMessageTemplate(synCtx, messageTemplate);
default:
return "Invalid log level specified";
}
}

private String getCustomLogMessage(MessageContext synCtx) {
StringBuffer sb = new StringBuffer();
processMessageTemplate(sb, synCtx, messageTemplate);
setCustomProperties(sb, synCtx);
return trimLeadingSeparator(sb);
}

private String getSimpleLogMessage(MessageContext synCtx) {
StringBuffer sb = new StringBuffer();
processMessageTemplate(sb, synCtx, messageTemplate);
// append separator if the message template is not empty
if (sb.length() > 0) {
sb.append(separator);
}
if (synCtx.getTo() != null)
sb.append("To: ").append(synCtx.getTo().getAddress());
else
Expand All @@ -188,6 +193,7 @@ private String getSimpleLogMessage(MessageContext synCtx) {

private String getHeadersLogMessage(MessageContext synCtx) {
StringBuffer sb = new StringBuffer();
processMessageTemplate(sb, synCtx, messageTemplate);
if (synCtx.getEnvelope() != null) {
SOAPHeader header = synCtx.getEnvelope().getHeader();
if (getCorrelationId(synCtx) != null)
Expand Down Expand Up @@ -303,7 +309,7 @@ public String getMessageTemplate() {

public void setMessageTemplate(String messageTemplate) {

this.messageTemplate = messageTemplate;
this.messageTemplate = messageTemplate.replace("\\n", "\n").replace("\\t", "\t");
}

private String trimLeadingSeparator(StringBuffer sb) {
Expand All @@ -315,35 +321,24 @@ private String trimLeadingSeparator(StringBuffer sb) {
}
}

private String processMessageTemplate(MessageContext synCtx, String template) {
StringBuffer result = new StringBuffer();
private void processMessageTemplate(StringBuffer stringBuffer, MessageContext synCtx, String template) {
try {
result.append(InlineExpressionUtil.processInLineSynapseExpressionTemplate(synCtx, template));
stringBuffer.append(InlineExpressionUtil.processInLineSynapseExpressionTemplate(synCtx, template));
} catch (JaxenException e) {
handleException("Failed to process the message template : " + template, e, synCtx);
}
setCustomProperties(result, synCtx);
return result.toString();
}

@Override
public boolean isContentAware() {
if (logLevel == MESSAGE_TEMPLATE) {
if (logLevel == MESSAGE_TEMPLATE || logLevel == CUSTOM) {
for (MediatorProperty property : properties) {
if (property.getExpression() != null && property.getExpression().isContentAware()) {
return true;
}
}
return InlineExpressionUtil.isInlineSynapseExpressionsContentAware(messageTemplate);
}
if (logLevel == CUSTOM) {
for (MediatorProperty property : properties) {
if (property.getExpression() != null && property.getExpression().isContentAware()) {
return true;
}
}
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ private static boolean isValidXML(String stringToValidate) {
}

/**
* Checks whether inline text contains synapse expressions
* Checks whether inline template contains content aware synapse expressions.
* Inline expressions will be denoted inside ${}
* e.g.: ${var.var1}, ${payload.element.id}
*
* @param inlineText Inline text string
* @return true if the string contains inline synapse expressions, false otherwise
* @return true if the string contains content aware inline synapse expressions, false otherwise
*/
public static boolean isInlineSynapseExpressionsContentAware(String inlineText) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private String getXmlOfMediatorScenarioTwo(String level, String seperator) {
}

private String getXmlOfMediatorScenarioThree(String category) {
return "<log xmlns=\"http://ws.apache.org/ns/synapse\" category=\"" +
return "<log xmlns=\"http://ws.apache.org/ns/synapse\" level=\"simple\" category=\"" +
category + "\"><property name=\"Text\" value=\"Sending quote request\"/></log>";

}
Expand Down

0 comments on commit c1e4644

Please sign in to comment.