Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static com.predic8.membrane.core.interceptor.Outcome.ABORT;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static com.predic8.membrane.core.resolver.ResolverMap.*;
import static com.predic8.membrane.core.util.StringUtil.*;
import static com.predic8.membrane.core.util.text.StringUtil.*;
import static java.nio.charset.StandardCharsets.*;
import static org.apache.commons.text.StringEscapeUtils.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static com.predic8.membrane.core.lang.ScriptingUtils.*;
import static com.predic8.membrane.core.util.FileUtil.*;
import static com.predic8.membrane.core.util.StringUtil.addLineNumbers;
import static com.predic8.membrane.core.util.text.StringUtil.addLineNumbers;
import static java.nio.charset.StandardCharsets.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.predic8.membrane.core.http.MimeType.*;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static com.predic8.membrane.core.util.StringUtil.*;
import static com.predic8.membrane.core.util.text.StringUtil.*;
import static java.nio.charset.StandardCharsets.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,111 +19,127 @@
import com.predic8.membrane.core.interceptor.*;
import com.predic8.membrane.core.multipart.*;
import com.predic8.membrane.core.util.*;
import com.predic8.membrane.core.util.text.*;
import org.jetbrains.annotations.*;
import org.slf4j.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.util.*;

import static com.predic8.membrane.core.exceptions.ProblemDetails.*;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static com.predic8.membrane.core.interceptor.Outcome.ABORT;
import static com.predic8.membrane.core.util.text.StringUtil.*;
import static com.predic8.membrane.core.util.text.TextUtil.*;

/**
* @description <p>
* The transform feature applies an XSLT transformation to the content in the body of a message. After the
* transformation the body content is replaced with the result of the transformation.
* </p>
* The transform feature applies an XSLT transformation to the content in the body of a message. After the
* transformation the body content is replaced with the result of the transformation.
* </p>
* @topic 2. Enterprise Integration Patterns
*/
@MCElement(name="transform")
@MCElement(name = "transform")
public class XSLTInterceptor extends AbstractInterceptor {

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

private String xslt;
private volatile XSLTTransformer xsltTransformer;
private final XOPReconstitutor xopr = new XOPReconstitutor();
private String xslt;
private volatile XSLTTransformer xsltTransformer;
private final XOPReconstitutor xopr = new XOPReconstitutor();

public XSLTInterceptor() {
name = "xslt transformer";
}
public XSLTInterceptor() {
name = "xslt transformer";
}

@Override
public Outcome handleRequest(Exchange exc) {
try {
transformMsg(exc.getRequest(), xslt, exc.getStringProperties());
} catch (Exception e) {
user(router.getConfiguration().isProduction(),getDisplayName())
.detail("Error transforming request!")
.exception(e)
.buildAndSetResponse(exc);
return ABORT;
}
return CONTINUE;
}
@Override
public Outcome handleRequest(Exchange exc) {
return handleInternal(exc, REQUEST);
}

@Override
public Outcome handleResponse(Exchange exc) {
return handleInternal(exc, RESPONSE);
}

private Outcome handleInternal(Exchange exc, Flow flow) {
var msg = exc.getMessage(flow);

@Override
public Outcome handleResponse(Exchange exc) {
try {
transformMsg(exc.getResponse(), xslt, exc.getStringProperties());
transformMsg(msg, exc.getStringProperties());
} catch (TransformerException e) {
log.debug("", e);
if (e.getMessage() != null && e.getMessage().contains("not allowed in prolog")) {
user(router.getConfiguration().isProduction(), getDisplayName())
.title("Content not allowed in prolog of XML input.")
.detail("Check for extra characters before the XML declaration <?xml ... ?>")
.internal("offendingInput", truncateAfter(msg.getBodyAsStringDecoded() + "...", 50))
.buildAndSetResponse(exc);
return ABORT;
Comment thread
predic8 marked this conversation as resolved.
}
return createErrorResponse(exc,e,flow);
} catch (Exception e) {
log.error("Error transforming response!", e);
user(router.getConfiguration().isProduction(),getDisplayName())
.detail("Error transforming response!")
.exception(e)
.buildAndSetResponse(exc);
return ABORT;
log.info("", e);
return createErrorResponse(exc,e,flow);
}
return CONTINUE;
}

private void transformMsg(Message msg, String ss, Map<String, String> parameter) throws Exception {
if (msg.isBodyEmpty())
return;
msg.setBodyContent(xsltTransformer.transform(
new StreamSource(xopr.reconstituteIfNecessary(msg)), parameter));
}

@Override
public void init() {
super.init();
}

private @NotNull Outcome createErrorResponse(Exchange exc, Exception e, Flow flow) {
user(router.getConfiguration().isProduction(), getDisplayName())
.detail("Error transforming message!")
.exception(e)
.internal("flow", flow.toString())
.buildAndSetResponse(exc);
return ABORT;
}

private void transformMsg(Message msg, Map<String, String> parameter) throws Exception {
if (msg.isBodyEmpty())
return;
msg.setBodyContent(xsltTransformer.transform(
new StreamSource(xopr.reconstituteIfNecessary(msg)), parameter));
}

@Override
public void init() {
super.init();
try {
xsltTransformer = new XSLTTransformer(xslt, router, getConcurrency());
} catch (Exception e) {
log.debug("",e);
log.debug("",e);
throw new ConfigurationException("Could not create XSLT transformer",e);

}
}

private static int getConcurrency() {
return Runtime.getRuntime().availableProcessors() * 2;
}

public String getXslt() {
return xslt;
}

/**
* @description Location of the XSLT stylesheet that will be applied to request and response.
* @example strip.xslt
*/
@MCAttribute
public void setXslt(String xslt) {
this.xslt = xslt;
this.xsltTransformer = null;
}

@Override
public String getShortDescription() {
return "Applies an XSLT transformation.";
}

@Override
public String getLongDescription() {
return TextUtil.removeFinalChar(getShortDescription()) +
" using the stylesheet at " +
TextUtil.linkURL(xslt) +
" .";
}
private static int getConcurrency() {
return Runtime.getRuntime().availableProcessors() * 2;
}

public String getXslt() {
return xslt;
}

/**
* @description Location of the XSLT stylesheet that will be applied to request and response.
* @example strip.xslt
*/
@MCAttribute
public void setXslt(String xslt) {
this.xslt = xslt;
this.xsltTransformer = null;
}

@Override
public String getShortDescription() {
return "Applies an XSLT transformation.";
}

@Override
public String getLongDescription() {
return "%s using the stylesheet at %s .".formatted(removeFinalChar(getShortDescription()), linkURL(xslt));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.io.*;
import java.util.*;

import static com.predic8.membrane.core.util.StringUtil.*;
import static com.predic8.membrane.core.util.text.StringUtil.*;
import static java.lang.Boolean.*;
import static java.nio.charset.StandardCharsets.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import static com.predic8.membrane.core.lang.ExchangeExpression.Language.SPEL;
import static com.predic8.membrane.core.lang.ExchangeExpression.expression;
import static com.predic8.membrane.core.util.StringUtil.maskNonPrintableCharacters;
import static com.predic8.membrane.core.util.text.StringUtil.maskNonPrintableCharacters;

/**
* @description The api proxy extends the serviceProxy with API related functions like OpenAPI support and path parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import static com.predic8.membrane.core.transport.http.ByteStreamLogging.wrapConnectionOutputStream;
import static com.predic8.membrane.core.transport.http.HttpServerHandler.RequestProcessingResult.*;
import static com.predic8.membrane.core.transport.http.HttpServerThreadFactory.DEFAULT_THREAD_NAME;
import static com.predic8.membrane.core.util.StringUtil.maskNonPrintableCharacters;
import static com.predic8.membrane.core.util.StringUtil.truncateAfter;
import static com.predic8.membrane.core.util.text.StringUtil.maskNonPrintableCharacters;
import static com.predic8.membrane.core.util.text.StringUtil.truncateAfter;
import static java.lang.Thread.currentThread;

public class HttpServerHandler extends AbstractHttpHandler implements Runnable, TwoWayStreaming {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License. */

package com.predic8.membrane.core.util;
package com.predic8.membrane.core.util.text;

import java.util.*;

Expand Down
Loading
Loading