Repository: cxf Updated Branches: refs/heads/3.0.x-fixes e826987a6 -> 8170b3ecb
remove DOS linebreaks Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8170b3ec Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8170b3ec Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8170b3ec Branch: refs/heads/3.0.x-fixes Commit: 8170b3ecbc0b9e0cbb5bd2a3ac6e7656c6b5d248 Parents: e826987 Author: Akitoshi Yoshida <a...@apache.org> Authored: Mon Feb 2 17:28:16 2015 +0100 Committer: Akitoshi Yoshida <a...@apache.org> Committed: Mon Feb 2 17:49:19 2015 +0100 ---------------------------------------------------------------------- .../transform/AbstractXSLTInterceptor.java | 176 ++++---- .../cxf/feature/transform/Messages.properties | 50 +-- .../cxf/feature/transform/XSLTFeature.java | 120 +++--- .../feature/transform/XSLTInInterceptor.java | 258 ++++++------ .../feature/transform/XSLTOutInterceptor.java | 410 +++++++++---------- .../apache/cxf/feature/transform/XSLTUtils.java | 202 ++++----- 6 files changed, 608 insertions(+), 608 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/AbstractXSLTInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/AbstractXSLTInterceptor.java b/core/src/main/java/org/apache/cxf/feature/transform/AbstractXSLTInterceptor.java index 2b92505..b18ffcf 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/AbstractXSLTInterceptor.java +++ b/core/src/main/java/org/apache/cxf/feature/transform/AbstractXSLTInterceptor.java @@ -1,88 +1,88 @@ -/** - * 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 java.io.InputStream; - -import javax.xml.stream.XMLStreamException; -import javax.xml.transform.Templates; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; - -import org.w3c.dom.Document; - -import org.apache.cxf.common.classloader.ClassLoaderUtils; -import org.apache.cxf.message.Message; -import org.apache.cxf.message.MessageUtils; -import org.apache.cxf.phase.AbstractPhaseInterceptor; -import org.apache.cxf.staxutils.StaxUtils; - - -/** - * Creates an XMLStreamReader from the InputStream on the Message. - */ -public abstract class AbstractXSLTInterceptor extends AbstractPhaseInterceptor<Message> { - private static final TransformerFactory TRANSFORM_FACTORIY = TransformerFactory.newInstance(); - - private String contextPropertyName; - private final Templates xsltTemplate; - - public AbstractXSLTInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { - super(phase); - if (before != null) { - addBefore(before.getName()); - } - if (after != null) { - addAfter(after.getName()); - } - - try { - InputStream xsltStream = ClassLoaderUtils.getResourceAsStream(xsltPath, this.getClass()); - if (xsltStream == null) { - throw new IllegalArgumentException("Cannot load XSLT from path: " + xsltPath); - } - Document doc = StaxUtils.read(xsltStream); - xsltTemplate = TRANSFORM_FACTORIY.newTemplates(new DOMSource(doc)); - } catch (TransformerConfigurationException e) { - throw new IllegalArgumentException( - String.format("Cannot create XSLT template from path: %s, error: ", - xsltPath, e.getException()), e); - } catch (XMLStreamException e) { - throw new IllegalArgumentException( - String.format("Cannot create XSLT template from path: %s, error: ", - xsltPath, e.getNestedException()), e); - } - } - - public void setContextPropertyName(String propertyName) { - contextPropertyName = propertyName; - } - - protected boolean checkContextProperty(Message message) { - return contextPropertyName != null - && !MessageUtils.getContextualBoolean(message, contextPropertyName, false); - } - - protected Templates getXSLTTemplate() { - return xsltTemplate; - } -} +/** + * 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 java.io.InputStream; + +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Templates; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; + +import org.w3c.dom.Document; + +import org.apache.cxf.common.classloader.ClassLoaderUtils; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.staxutils.StaxUtils; + + +/** + * Creates an XMLStreamReader from the InputStream on the Message. + */ +public abstract class AbstractXSLTInterceptor extends AbstractPhaseInterceptor<Message> { + private static final TransformerFactory TRANSFORM_FACTORIY = TransformerFactory.newInstance(); + + private String contextPropertyName; + private final Templates xsltTemplate; + + public AbstractXSLTInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { + super(phase); + if (before != null) { + addBefore(before.getName()); + } + if (after != null) { + addAfter(after.getName()); + } + + try { + InputStream xsltStream = ClassLoaderUtils.getResourceAsStream(xsltPath, this.getClass()); + if (xsltStream == null) { + throw new IllegalArgumentException("Cannot load XSLT from path: " + xsltPath); + } + Document doc = StaxUtils.read(xsltStream); + xsltTemplate = TRANSFORM_FACTORIY.newTemplates(new DOMSource(doc)); + } catch (TransformerConfigurationException e) { + throw new IllegalArgumentException( + String.format("Cannot create XSLT template from path: %s, error: ", + xsltPath, e.getException()), e); + } catch (XMLStreamException e) { + throw new IllegalArgumentException( + String.format("Cannot create XSLT template from path: %s, error: ", + xsltPath, e.getNestedException()), e); + } + } + + public void setContextPropertyName(String propertyName) { + contextPropertyName = propertyName; + } + + protected boolean checkContextProperty(Message message) { + return contextPropertyName != null + && !MessageUtils.getContextualBoolean(message, contextPropertyName, false); + } + + protected Templates getXSLTTemplate() { + return xsltTemplate; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/Messages.properties ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/Messages.properties b/core/src/main/java/org/apache/cxf/feature/transform/Messages.properties index a473717..e6e31a9 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/Messages.properties +++ b/core/src/main/java/org/apache/cxf/feature/transform/Messages.properties @@ -1,25 +1,25 @@ -# -# -# 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. -# -# -STAX_COPY=Could not copy XML stream: {0}. -STREAM_COPY=Could not copy stream: {0}. -READER_COPY=Could not copy reader: {0}. -GET_CACHED_INPUT_STREAM=Could not get cached input stream: {0}. -XML_TRANSFORM=XSL transformation error: {0}. +# +# +# 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. +# +# +STAX_COPY=Could not copy XML stream: {0}. +STREAM_COPY=Could not copy stream: {0}. +READER_COPY=Could not copy reader: {0}. +GET_CACHED_INPUT_STREAM=Could not get cached input stream: {0}. +XML_TRANSFORM=XSL transformation error: {0}. http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/XSLTFeature.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/XSLTFeature.java b/core/src/main/java/org/apache/cxf/feature/transform/XSLTFeature.java index 8c913ac..511a61c 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/XSLTFeature.java +++ b/core/src/main/java/org/apache/cxf/feature/transform/XSLTFeature.java @@ -1,60 +1,60 @@ -/** - * 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.Bus; -import org.apache.cxf.common.injection.NoJSR250Annotations; -import org.apache.cxf.feature.AbstractFeature; -import org.apache.cxf.interceptor.InterceptorProvider; - -/** - * This class defines a feature is used to transform message using XSLT script. - * If this feature is present and inXSLTPath/outXLSTPath are initialised, - * client and endpoint will transform incoming and outgoing messages correspondingly. - * Attention: actually the feature breaks streaming - * (can be fixed in further versions when XSLT engine supports XML stream). - */ -@NoJSR250Annotations -public class XSLTFeature extends AbstractFeature { - private String inXSLTPath; - private String outXSLTPath; - - @Override - protected void initializeProvider(InterceptorProvider provider, Bus bus) { - if (inXSLTPath != null) { - XSLTInInterceptor in = new XSLTInInterceptor(inXSLTPath); - provider.getInInterceptors().add(in); - } - - if (outXSLTPath != null) { - XSLTOutInterceptor out = new XSLTOutInterceptor(outXSLTPath); - provider.getOutInterceptors().add(out); - provider.getOutFaultInterceptors().add(out); - } - } - - public void setInXSLTPath(String inXSLTPath) { - this.inXSLTPath = inXSLTPath; - } - - public void setOutXSLTPath(String outXSLTPath) { - this.outXSLTPath = outXSLTPath; - } - -} +/** + * 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.Bus; +import org.apache.cxf.common.injection.NoJSR250Annotations; +import org.apache.cxf.feature.AbstractFeature; +import org.apache.cxf.interceptor.InterceptorProvider; + +/** + * This class defines a feature is used to transform message using XSLT script. + * If this feature is present and inXSLTPath/outXLSTPath are initialised, + * client and endpoint will transform incoming and outgoing messages correspondingly. + * Attention: actually the feature breaks streaming + * (can be fixed in further versions when XSLT engine supports XML stream). + */ +@NoJSR250Annotations +public class XSLTFeature extends AbstractFeature { + private String inXSLTPath; + private String outXSLTPath; + + @Override + protected void initializeProvider(InterceptorProvider provider, Bus bus) { + if (inXSLTPath != null) { + XSLTInInterceptor in = new XSLTInInterceptor(inXSLTPath); + provider.getInInterceptors().add(in); + } + + if (outXSLTPath != null) { + XSLTOutInterceptor out = new XSLTOutInterceptor(outXSLTPath); + provider.getOutInterceptors().add(out); + provider.getOutFaultInterceptors().add(out); + } + } + + public void setInXSLTPath(String inXSLTPath) { + this.inXSLTPath = inXSLTPath; + } + + public void setOutXSLTPath(String outXSLTPath) { + this.outXSLTPath = outXSLTPath; + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/XSLTInInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/XSLTInInterceptor.java b/core/src/main/java/org/apache/cxf/feature/transform/XSLTInInterceptor.java index 4596009..5430055 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/XSLTInInterceptor.java +++ b/core/src/main/java/org/apache/cxf/feature/transform/XSLTInInterceptor.java @@ -1,129 +1,129 @@ -/** - * 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 java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.util.logging.Logger; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -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; - - -/** 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 XSLTInInterceptor extends AbstractXSLTInterceptor { - private static final Logger LOG = LogUtils.getL7dLogger(XSLTInInterceptor.class); - - public XSLTInInterceptor(String xsltPath) { - super(Phase.POST_STREAM, StaxInInterceptor.class, null, xsltPath); - } - - public XSLTInInterceptor(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; - } - - // 1. Try to get and transform XMLStreamReader message content - XMLStreamReader xReader = message.getContent(XMLStreamReader.class); - if (xReader != null) { - transformXReader(message, xReader); - } else { - // 2. Try to get and transform InputStream message content - InputStream is = message.getContent(InputStream.class); - if (is != null) { - transformIS(message, is); - } 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); - } - } - } - } - - protected void transformXReader(Message message, XMLStreamReader xReader) { - CachedOutputStream cachedOS = new CachedOutputStream(); - try { - StaxUtils.copy(xReader, cachedOS); - InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), cachedOS.getInputStream()); - XMLStreamReader transformedReader = StaxUtils.createXMLStreamReader(transformedIS); - 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) { - try { - InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), is); - 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); - message.setContent(Reader.class, transformedReader); - } finally { - try { - reader.close(); - } catch (IOException e) { - LOG.warning("Cannot close stream after transformation: " + e.getMessage()); - } - } - } -} +/** + * 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 java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.logging.Logger; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +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; + + +/** 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 XSLTInInterceptor extends AbstractXSLTInterceptor { + private static final Logger LOG = LogUtils.getL7dLogger(XSLTInInterceptor.class); + + public XSLTInInterceptor(String xsltPath) { + super(Phase.POST_STREAM, StaxInInterceptor.class, null, xsltPath); + } + + public XSLTInInterceptor(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; + } + + // 1. Try to get and transform XMLStreamReader message content + XMLStreamReader xReader = message.getContent(XMLStreamReader.class); + if (xReader != null) { + transformXReader(message, xReader); + } else { + // 2. Try to get and transform InputStream message content + InputStream is = message.getContent(InputStream.class); + if (is != null) { + transformIS(message, is); + } 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); + } + } + } + } + + protected void transformXReader(Message message, XMLStreamReader xReader) { + CachedOutputStream cachedOS = new CachedOutputStream(); + try { + StaxUtils.copy(xReader, cachedOS); + InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), cachedOS.getInputStream()); + XMLStreamReader transformedReader = StaxUtils.createXMLStreamReader(transformedIS); + 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) { + try { + InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), is); + 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); + message.setContent(Reader.class, transformedReader); + } finally { + try { + reader.close(); + } catch (IOException e) { + LOG.warning("Cannot close stream after transformation: " + e.getMessage()); + } + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/XSLTOutInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/XSLTOutInterceptor.java b/core/src/main/java/org/apache/cxf/feature/transform/XSLTOutInterceptor.java index 3a12c3c..6044498 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/XSLTOutInterceptor.java +++ b/core/src/main/java/org/apache/cxf/feature/transform/XSLTOutInterceptor.java @@ -1,205 +1,205 @@ -/** - * 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 java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.Writer; -import java.util.logging.Logger; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import javax.xml.transform.Templates; -import javax.xml.transform.stream.StreamSource; - -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; - -/** 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 XSLTOutInterceptor extends AbstractXSLTInterceptor { - private static final Logger LOG = LogUtils.getL7dLogger(XSLTOutInterceptor.class); - - public XSLTOutInterceptor(String xsltPath) { - super(Phase.PRE_STREAM, StaxOutInterceptor.class, null, xsltPath); - } - - public XSLTOutInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { - super(phase, before, after, xsltPath); - } - - @Override - public void handleMessage(Message message) { - if (checkContextProperty(message)) { - return; - } - - // 1. Try to get and transform XMLStreamWriter message content - XMLStreamWriter xWriter = message.getContent(XMLStreamWriter.class); - if (xWriter != null) { - transformXWriter(message, xWriter); - } else { - // 2. Try to get and transform OutputStream message content - OutputStream out = message.getContent(OutputStream.class); - if (out != null) { - transformOS(message, out); - } 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); - } - } - } - } - - 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) { - CachedOutputStream wrapper = new CachedOutputStream(); - CachedOutputStreamCallback callback = new XSLTCachedOutputStreamCallback(getXSLTTemplate(), out); - 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; - - public XSLTCachedOutputStreamCallback(Templates xsltTemplate, OutputStream origStream) { - this.xsltTemplate = xsltTemplate; - this.origStream = origStream; - } - - @Override - public void onFlush(CachedOutputStream wrapper) { - } - - @Override - public void onClose(CachedOutputStream wrapper) { - InputStream transformedStream = null; - try { - transformedStream = XSLTUtils.transform(xsltTemplate, wrapper.getInputStream()); - 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()); - } - } - } - } - -} +/** + * 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 java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.util.logging.Logger; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.transform.Templates; +import javax.xml.transform.stream.StreamSource; + +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; + +/** 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 XSLTOutInterceptor extends AbstractXSLTInterceptor { + private static final Logger LOG = LogUtils.getL7dLogger(XSLTOutInterceptor.class); + + public XSLTOutInterceptor(String xsltPath) { + super(Phase.PRE_STREAM, StaxOutInterceptor.class, null, xsltPath); + } + + public XSLTOutInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) { + super(phase, before, after, xsltPath); + } + + @Override + public void handleMessage(Message message) { + if (checkContextProperty(message)) { + return; + } + + // 1. Try to get and transform XMLStreamWriter message content + XMLStreamWriter xWriter = message.getContent(XMLStreamWriter.class); + if (xWriter != null) { + transformXWriter(message, xWriter); + } else { + // 2. Try to get and transform OutputStream message content + OutputStream out = message.getContent(OutputStream.class); + if (out != null) { + transformOS(message, out); + } 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); + } + } + } + } + + 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) { + CachedOutputStream wrapper = new CachedOutputStream(); + CachedOutputStreamCallback callback = new XSLTCachedOutputStreamCallback(getXSLTTemplate(), out); + 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; + + public XSLTCachedOutputStreamCallback(Templates xsltTemplate, OutputStream origStream) { + this.xsltTemplate = xsltTemplate; + this.origStream = origStream; + } + + @Override + public void onFlush(CachedOutputStream wrapper) { + } + + @Override + public void onClose(CachedOutputStream wrapper) { + InputStream transformedStream = null; + try { + transformedStream = XSLTUtils.transform(xsltTemplate, wrapper.getInputStream()); + 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()); + } + } + } + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8170b3ec/core/src/main/java/org/apache/cxf/feature/transform/XSLTUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/feature/transform/XSLTUtils.java b/core/src/main/java/org/apache/cxf/feature/transform/XSLTUtils.java index 24078cf..1488b5d 100644 --- a/core/src/main/java/org/apache/cxf/feature/transform/XSLTUtils.java +++ b/core/src/main/java/org/apache/cxf/feature/transform/XSLTUtils.java @@ -1,101 +1,101 @@ -/** - * 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 java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.util.logging.Logger; - -import javax.xml.stream.XMLStreamReader; -import javax.xml.transform.Source; -import javax.xml.transform.Templates; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.dom.DOMResult; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.Document; - -import org.apache.cxf.common.logging.LogUtils; -import org.apache.cxf.helpers.DOMUtils; -import org.apache.cxf.interceptor.Fault; -import org.apache.cxf.io.CachedOutputStream; -import org.apache.cxf.io.CachedWriter; -import org.apache.cxf.staxutils.StaxSource; -import org.apache.cxf.staxutils.StaxUtils; - -public final class XSLTUtils { - private static final Logger LOG = LogUtils.getL7dLogger(XSLTUtils.class); - - private XSLTUtils() { - - } - - public static InputStream transform(Templates xsltTemplate, InputStream in) { - try { - XMLStreamReader reader = StaxUtils.createXMLStreamReader(in); - Source beforeSource = new StaxSource(reader); - CachedOutputStream out = new CachedOutputStream(); - - Transformer trans = xsltTemplate.newTransformer(); - trans.transform(beforeSource, new StreamResult(out)); - - return out.getInputStream(); - } catch (IOException e) { - throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); - } catch (TransformerException e) { - throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); - } - } - - public static Reader transform(Templates xsltTemplate, Reader inReader) { - try { - XMLStreamReader reader = StaxUtils.createXMLStreamReader(inReader); - Source beforeSource = new StaxSource(reader); - CachedWriter outWriter = new CachedWriter(); - - Transformer trans = xsltTemplate.newTransformer(); - trans.transform(beforeSource, new StreamResult(outWriter)); - - return outWriter.getReader(); - } catch (IOException e) { - throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); - } catch (TransformerException e) { - throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); - } - } - - public static Document transform(Templates xsltTemplate, Document in) { - try { - DOMSource beforeSource = new DOMSource(in); - - Document out = DOMUtils.createDocument(); - - Transformer trans = xsltTemplate.newTransformer(); - trans.transform(beforeSource, new DOMResult(out)); - - return out; - } catch (TransformerException e) { - throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); - } - } -} +/** + * 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 java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.logging.Logger; + +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; + +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.helpers.DOMUtils; +import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.io.CachedOutputStream; +import org.apache.cxf.io.CachedWriter; +import org.apache.cxf.staxutils.StaxSource; +import org.apache.cxf.staxutils.StaxUtils; + +public final class XSLTUtils { + private static final Logger LOG = LogUtils.getL7dLogger(XSLTUtils.class); + + private XSLTUtils() { + + } + + public static InputStream transform(Templates xsltTemplate, InputStream in) { + try { + XMLStreamReader reader = StaxUtils.createXMLStreamReader(in); + Source beforeSource = new StaxSource(reader); + CachedOutputStream out = new CachedOutputStream(); + + Transformer trans = xsltTemplate.newTransformer(); + trans.transform(beforeSource, new StreamResult(out)); + + return out.getInputStream(); + } catch (IOException e) { + throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); + } catch (TransformerException e) { + throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); + } + } + + public static Reader transform(Templates xsltTemplate, Reader inReader) { + try { + XMLStreamReader reader = StaxUtils.createXMLStreamReader(inReader); + Source beforeSource = new StaxSource(reader); + CachedWriter outWriter = new CachedWriter(); + + Transformer trans = xsltTemplate.newTransformer(); + trans.transform(beforeSource, new StreamResult(outWriter)); + + return outWriter.getReader(); + } catch (IOException e) { + throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage()); + } catch (TransformerException e) { + throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); + } + } + + public static Document transform(Templates xsltTemplate, Document in) { + try { + DOMSource beforeSource = new DOMSource(in); + + Document out = DOMUtils.createDocument(); + + Transformer trans = xsltTemplate.newTransformer(); + trans.transform(beforeSource, new DOMResult(out)); + + return out; + } catch (TransformerException e) { + throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage()); + } + } +}