-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CXF-7491] Fork Stax & XSLT interceptors to make them Charset-aware
- Loading branch information
Cyrille Chépélov
committed
Aug 31, 2017
1 parent
0b5f511
commit 56dd9e3
Showing
22 changed files
with
1,251 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
core/src/main/java/org/apache/cxf/feature/transform/CharsetAwareXSLTInInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.cxf.feature.transform; | ||
|
||
|
||
import org.apache.cxf.common.logging.LogUtils; | ||
import org.apache.cxf.interceptor.Fault; | ||
import org.apache.cxf.interceptor.StaxInInterceptor; | ||
import org.apache.cxf.io.CachedOutputStream; | ||
import org.apache.cxf.message.Message; | ||
import org.apache.cxf.phase.Phase; | ||
import org.apache.cxf.staxutils.StaxUtils; | ||
import sun.awt.CharsetString; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.logging.Logger; | ||
|
||
|
||
/** Class provides XSLT transformation of incoming message. | ||
* Actually it breaks streaming (can be fixed in further versions when XSLT engine supports XML stream) | ||
*/ | ||
public class CharsetAwareXSLTInInterceptor extends AbstractXSLTInterceptor { | ||
private static final Logger LOG = LogUtils.getL7dLogger(CharsetAwareXSLTInInterceptor.class); | ||
|
||
public CharsetAwareXSLTInInterceptor(String xsltPath) { | ||
super(Phase.POST_STREAM, StaxInInterceptor.class, null, xsltPath); | ||
} | ||
|
||
public CharsetAwareXSLTInInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { | ||
super(phase, before, after, xsltPath); | ||
} | ||
|
||
@Override | ||
public void handleMessage(Message message) { | ||
if (!isRequestor(message) && isGET(message) || checkContextProperty(message)) { | ||
return; | ||
} | ||
|
||
String encoding = (String)message.getContextualProperty(Message.ENCODING); | ||
if (encoding == null) { | ||
encoding = StandardCharsets.UTF_8.name(); | ||
} | ||
|
||
// 1. Try to get and transform XMLStreamReader message content | ||
XMLStreamReader xReader = message.getContent(XMLStreamReader.class); | ||
if (xReader != null) { | ||
transformXReader(message, xReader, encoding); | ||
} else { | ||
// 2. Try to get and transform InputStream message content | ||
InputStream is = message.getContent(InputStream.class); | ||
if (is != null) { | ||
transformIS(message, is, encoding); | ||
} else { | ||
// 3. Try to get and transform Reader message content (actually used for JMS TextMessage) | ||
Reader reader = message.getContent(Reader.class); | ||
if (reader != null) { | ||
transformReader(message, reader); // reader already encapsulates encoding | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected void transformXReader(Message message, XMLStreamReader xReader, String encoding) { | ||
CachedOutputStream cachedOS = new CachedOutputStream(); | ||
try { | ||
StaxUtils.copy(xReader, cachedOS, encoding); | ||
InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), cachedOS.getInputStream(), encoding); | ||
XMLStreamReader transformedReader = StaxUtils.createXMLStreamReader(transformedIS, encoding); | ||
message.setContent(XMLStreamReader.class, transformedReader); | ||
} catch (XMLStreamException e) { | ||
throw new Fault("STAX_COPY", LOG, e, e.getMessage()); | ||
} catch (IOException e) { | ||
throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); | ||
} finally { | ||
try { | ||
StaxUtils.close(xReader); | ||
} catch (XMLStreamException ex) { | ||
throw new Fault(ex); | ||
} | ||
try { | ||
cachedOS.close(); | ||
} catch (IOException e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
protected void transformIS(Message message, InputStream is, String encoding) { | ||
try { | ||
InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), is, encoding); | ||
message.setContent(InputStream.class, transformedIS); | ||
} finally { | ||
try { | ||
is.close(); | ||
} catch (IOException e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
protected void transformReader(Message message, Reader reader) { | ||
try { | ||
Reader transformedReader = XSLTUtils.transform(getXSLTTemplate(), reader); // reader already encapsulates encoding | ||
message.setContent(Reader.class, transformedReader); | ||
} finally { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
208 changes: 208 additions & 0 deletions
208
core/src/main/java/org/apache/cxf/feature/transform/CharsetAwareXSLTOutInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.cxf.feature.transform; | ||
|
||
import org.apache.cxf.common.logging.LogUtils; | ||
import org.apache.cxf.helpers.IOUtils; | ||
import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor; | ||
import org.apache.cxf.interceptor.Fault; | ||
import org.apache.cxf.interceptor.StaxOutInterceptor; | ||
import org.apache.cxf.io.CachedOutputStream; | ||
import org.apache.cxf.io.CachedOutputStreamCallback; | ||
import org.apache.cxf.io.CachedWriter; | ||
import org.apache.cxf.message.Message; | ||
import org.apache.cxf.phase.Phase; | ||
import org.apache.cxf.staxutils.DelegatingXMLStreamWriter; | ||
import org.apache.cxf.staxutils.StaxUtils; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamWriter; | ||
import javax.xml.transform.Templates; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.logging.Logger; | ||
|
||
/** Class provides XSLT transformation of outgoing message. | ||
* Actually it breaks streaming (can be fixed in further versions when XSLT engine supports XML stream) | ||
*/ | ||
public class CharsetAwareXSLTOutInterceptor extends AbstractXSLTInterceptor { | ||
private static final Logger LOG = LogUtils.getL7dLogger(CharsetAwareXSLTOutInterceptor.class); | ||
|
||
public CharsetAwareXSLTOutInterceptor(String xsltPath) { | ||
super(Phase.PRE_STREAM, StaxOutInterceptor.class, null, xsltPath); | ||
} | ||
|
||
public CharsetAwareXSLTOutInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { | ||
super(phase, before, after, xsltPath); | ||
} | ||
|
||
@Override | ||
public void handleMessage(Message message) { | ||
if (checkContextProperty(message)) { | ||
return; | ||
} | ||
|
||
String encoding = (String)message.getContextualProperty(Message.ENCODING); | ||
if (encoding == null) { | ||
encoding = StandardCharsets.UTF_8.name(); | ||
} | ||
|
||
// 1. Try to get and transform XMLStreamWriter message content | ||
XMLStreamWriter xWriter = message.getContent(XMLStreamWriter.class); | ||
if (xWriter != null) { | ||
transformXWriter(message, xWriter); /* The effective output encoding depends on the encoding of the xWriter */ | ||
} else { | ||
// 2. Try to get and transform OutputStream message content | ||
OutputStream out = message.getContent(OutputStream.class); | ||
if (out != null) { | ||
transformOS(message, out, encoding); | ||
} else { | ||
// 3. Try to get and transform Writer message content (actually used for JMS TextMessage) | ||
Writer writer = message.getContent(Writer.class); | ||
if (writer != null) { | ||
transformWriter(message, writer); /* The effective output encoding depends on the encoding of the xWriter */ | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected void transformXWriter(Message message, XMLStreamWriter xWriter) { | ||
CachedWriter writer = new CachedWriter(); | ||
XMLStreamWriter delegate = StaxUtils.createXMLStreamWriter(writer); | ||
XSLTStreamWriter wrapper = new XSLTStreamWriter(getXSLTTemplate(), writer, delegate, xWriter); | ||
message.setContent(XMLStreamWriter.class, wrapper); | ||
message.put(AbstractOutDatabindingInterceptor.DISABLE_OUTPUTSTREAM_OPTIMIZATION, | ||
Boolean.TRUE); | ||
} | ||
|
||
protected void transformOS(Message message, OutputStream out, String encoding) { | ||
CachedOutputStream wrapper = new CachedOutputStream(); | ||
CachedOutputStreamCallback callback = new XSLTCachedOutputStreamCallback(getXSLTTemplate(), out, encoding); | ||
wrapper.registerCallback(callback); | ||
message.setContent(OutputStream.class, wrapper); | ||
} | ||
|
||
protected void transformWriter(Message message, Writer writer) { | ||
XSLTCachedWriter wrapper = new XSLTCachedWriter(getXSLTTemplate(), writer); | ||
message.setContent(Writer.class, wrapper); | ||
} | ||
|
||
|
||
public static class XSLTStreamWriter extends DelegatingXMLStreamWriter { | ||
private final Templates xsltTemplate; | ||
private final CachedWriter cachedWriter; | ||
private final XMLStreamWriter origXWriter; | ||
|
||
public XSLTStreamWriter(Templates xsltTemplate, CachedWriter cachedWriter, | ||
XMLStreamWriter delegateXWriter, XMLStreamWriter origXWriter) { | ||
super(delegateXWriter); | ||
this.xsltTemplate = xsltTemplate; | ||
this.cachedWriter = cachedWriter; | ||
this.origXWriter = origXWriter; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
Reader transformedReader = null; | ||
try { | ||
super.flush(); | ||
transformedReader = XSLTUtils.transform(xsltTemplate, cachedWriter.getReader()); | ||
StaxUtils.copy(new StreamSource(transformedReader), origXWriter); | ||
} catch (XMLStreamException e) { | ||
throw new Fault("STAX_COPY", LOG, e, e.getMessage()); | ||
} catch (IOException e) { | ||
throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); | ||
} finally { | ||
try { | ||
if (transformedReader != null) { | ||
transformedReader.close(); | ||
} | ||
cachedWriter.close(); | ||
StaxUtils.close(origXWriter); | ||
super.close(); | ||
} catch (Exception e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class XSLTCachedOutputStreamCallback implements CachedOutputStreamCallback { | ||
private final Templates xsltTemplate; | ||
private final OutputStream origStream; | ||
private final String encoding; | ||
|
||
public XSLTCachedOutputStreamCallback(Templates xsltTemplate, OutputStream origStream, String encoding) { | ||
this.xsltTemplate = xsltTemplate; | ||
this.origStream = origStream; | ||
this.encoding = encoding; | ||
} | ||
|
||
@Override | ||
public void onFlush(CachedOutputStream wrapper) { | ||
} | ||
|
||
@Override | ||
public void onClose(CachedOutputStream wrapper) { | ||
InputStream transformedStream = null; | ||
try { | ||
transformedStream = XSLTUtils.transform(xsltTemplate, wrapper.getInputStream(), encoding); | ||
IOUtils.copyAndCloseInput(transformedStream, origStream); | ||
} catch (IOException e) { | ||
throw new Fault("STREAM_COPY", LOG, e, e.getMessage()); | ||
} finally { | ||
try { | ||
origStream.close(); | ||
} catch (IOException e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class XSLTCachedWriter extends CachedWriter { | ||
private final Templates xsltTemplate; | ||
private final Writer origWriter; | ||
|
||
public XSLTCachedWriter(Templates xsltTemplate, Writer origWriter) { | ||
this.xsltTemplate = xsltTemplate; | ||
this.origWriter = origWriter; | ||
} | ||
|
||
@Override | ||
protected void doClose() { | ||
Reader transformedReader = null; | ||
try { | ||
transformedReader = XSLTUtils.transform(xsltTemplate, getReader()); | ||
IOUtils.copyAndCloseInput(transformedReader, origWriter, IOUtils.DEFAULT_BUFFER_SIZE); | ||
} catch (IOException e) { | ||
throw new Fault("READER_COPY", LOG, e, e.getMessage()); | ||
} finally { | ||
try { | ||
origWriter.close(); | ||
} catch (IOException e) { | ||
LOG.warning("Cannot close stream after transformation: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.