Hi,
We have a requirement to apply XSLT to incoming request XML and outgoing
response XML within CXF interceptors (to avoid changes to the endpoint code).
We have used the following approach to modify the response XML and would
appreciate your comments to see whether this is an acceptable approach or not.
Note that the interceptor is registered at "pre-stream".
try (OutputStream outputStream = message.getContent(OutputStream.class);
CachedOutputStream cachedStream = new CachedOutputStream();
InputStream resourceInputStream = resource.getInputStream()) {
message.setContent(OutputStream.class, cachedStream);
//Allow other interceptors execution so that finally output stream will be
populated with outgoing message.
message.getInterceptorChain().doIntercept(message);
//flush the stream so that we can see/extract the message.
cachedStream.flush();
//Get the xml message on which you want to apply transformation.
Source inputSource = getInputSource(message);
Source xslt = new StreamSource(resourceInputStream);
Transformer transformer = transformerFactory.newTransformer(xslt);
StreamResult streamResult = new StreamResult(outputStream);
transformer.transform(inputSource, streamResult);
outputStream.flush();
message.setContent(OutputStream.class, outputStream);
if(LOGGER.isDebugEnabled()){
//TODO response needs to be logged
LOGGER.debug("Transformed response: "+new
String(((CachedOutputStream)outputStream).getBytes()));
}
}
It seems to work but we're worried about any side effects.
Many thanks
Mandy
Sent from a mobile device