com.predic8.membrane.core.interceptor.xslt.XSLTInterceptor Maven / Gradle / Ivy
/* Copyright 2011, 2012 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.predic8.membrane.core.interceptor.xslt;
import java.util.Map;
import javax.xml.transform.stream.StreamSource;
import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
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.multipart.XOPReconstitutor;
import com.predic8.membrane.core.util.TextUtil;
/**
* @description
* 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.
*
* @topic 3. Enterprise Integration Patterns
*/
@MCElement(name="transform")
public class XSLTInterceptor extends AbstractInterceptor {
private String xslt;
private volatile XSLTTransformer xsltTransformer;
private final XOPReconstitutor xopr = new XOPReconstitutor();
public XSLTInterceptor() {
name = "XSLT Transformer";
}
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
transformMsg(exc.getRequest(), xslt, exc.getStringProperties());
return Outcome.CONTINUE;
}
@Override
public Outcome handleResponse(Exchange exc) throws Exception {
transformMsg(exc.getResponse(), xslt, exc.getStringProperties());
return Outcome.CONTINUE;
}
private void transformMsg(Message msg, String ss, Map parameter) throws Exception {
if (msg.isBodyEmpty())
return;
msg.setBodyContent(xsltTransformer.transform(
new StreamSource(xopr.reconstituteIfNecessary(msg)), parameter));
}
@Override
public void init() throws Exception {
xsltTransformer = new XSLTTransformer(xslt, router, getConcurrency());
}
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() {
String sb = TextUtil.removeFinalChar(getShortDescription()) +
" using the stylesheet at " +
TextUtil.linkURL(xslt) +
" .";
return sb;
}
}