Repository: cxf Updated Branches: refs/heads/master eb59896da -> 03f0d1b79
[CXF-5866] Replace DelegatingXMLStreamReader with StAX StreamReaderDelegate; turn SysPropExpandingStreamReader into a more generic PropertiesExpandingStreamReader Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/e76e09aa Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e76e09aa Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e76e09aa Branch: refs/heads/master Commit: e76e09aa7eb1011599e1137e88257e122494c116 Parents: 3163a36 Author: Alessio Soldano <[email protected]> Authored: Fri Jul 11 11:42:55 2014 +0200 Committer: Alessio Soldano <[email protected]> Committed: Fri Jul 11 11:42:55 2014 +0200 ---------------------------------------------------------------------- .../staxutils/DelegatingXMLStreamReader.java | 264 ------------------- .../PropertiesExpandingStreamReader.java | 99 +++++++ .../staxutils/SysPropExpandingStreamReader.java | 93 ------- .../PropertiesExpandingStreamReaderTest.java | 57 ++++ .../SysPropExpandingStreamReaderTest.java | 67 ----- .../apache/cxf/wsdl11/WSDLManagerImplTest.java | 36 ++- 6 files changed, 171 insertions(+), 445 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/core/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamReader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamReader.java b/core/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamReader.java deleted file mode 100644 index 56564a7..0000000 --- a/core/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamReader.java +++ /dev/null @@ -1,264 +0,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. - */ -package org.apache.cxf.staxutils; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -/** - * A XMLStreamReader that delegates to a provided XMLStreamReader instance. - * - */ -public class DelegatingXMLStreamReader implements XMLStreamReader { - - private final XMLStreamReader delegate; - - public DelegatingXMLStreamReader(XMLStreamReader reader) { - this.delegate = reader; - } - - @Override - public Object getProperty(String name) throws IllegalArgumentException { - return delegate.getProperty(name); - } - - @Override - public int next() throws XMLStreamException { - return delegate.next(); - } - - @Override - public void require(int type, String namespaceURI, String localName) throws XMLStreamException { - delegate.require(type, namespaceURI, localName); - } - - @Override - public String getElementText() throws XMLStreamException { - return delegate.getElementText(); - } - - @Override - public int nextTag() throws XMLStreamException { - return delegate.nextTag(); - } - - @Override - public boolean hasNext() throws XMLStreamException { - return delegate.hasNext(); - } - - @Override - public void close() throws XMLStreamException { - delegate.close(); - } - - @Override - public String getNamespaceURI(String prefix) { - return delegate.getNamespaceURI(prefix); - } - - @Override - public boolean isStartElement() { - return delegate.isStartElement(); - } - - @Override - public boolean isEndElement() { - return delegate.isEndElement(); - } - - @Override - public boolean isCharacters() { - return delegate.isCharacters(); - } - - @Override - public boolean isWhiteSpace() { - return delegate.isWhiteSpace(); - } - - @Override - public String getAttributeValue(String namespaceURI, String localName) { - return delegate.getAttributeValue(namespaceURI, localName); - } - - @Override - public int getAttributeCount() { - return delegate.getAttributeCount(); - } - - @Override - public QName getAttributeName(int index) { - return delegate.getAttributeName(index); - } - - @Override - public String getAttributeNamespace(int index) { - return delegate.getAttributeNamespace(index); - } - - @Override - public String getAttributeLocalName(int index) { - return delegate.getAttributeLocalName(index); - } - - @Override - public String getAttributePrefix(int index) { - return delegate.getAttributePrefix(index); - } - - @Override - public String getAttributeType(int index) { - return delegate.getAttributeType(index); - } - - @Override - public String getAttributeValue(int index) { - return delegate.getAttributeValue(index); - } - - @Override - public boolean isAttributeSpecified(int index) { - return delegate.isAttributeSpecified(index); - } - - @Override - public int getNamespaceCount() { - return delegate.getNamespaceCount(); - } - - @Override - public String getNamespacePrefix(int index) { - return delegate.getNamespacePrefix(index); - } - - @Override - public String getNamespaceURI(int index) { - return delegate.getNamespaceURI(index); - } - - @Override - public NamespaceContext getNamespaceContext() { - return delegate.getNamespaceContext(); - } - - @Override - public int getEventType() { - return delegate.getEventType(); - } - - @Override - public String getText() { - return delegate.getText(); - } - - @Override - public char[] getTextCharacters() { - return delegate.getTextCharacters(); - } - - @Override - public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) - throws XMLStreamException { - return delegate.getTextCharacters(sourceStart, target, targetStart, length); - } - - @Override - public int getTextStart() { - return delegate.getTextStart(); - } - - @Override - public int getTextLength() { - return delegate.getTextLength(); - } - - @Override - public String getEncoding() { - return delegate.getEncoding(); - } - - @Override - public boolean hasText() { - return delegate.hasText(); - } - - @Override - public Location getLocation() { - return delegate.getLocation(); - } - - @Override - public QName getName() { - return delegate.getName(); - } - - @Override - public String getLocalName() { - return delegate.getLocalName(); - } - - @Override - public boolean hasName() { - return delegate.hasName(); - } - - @Override - public String getNamespaceURI() { - return delegate.getNamespaceURI(); - } - - @Override - public String getPrefix() { - return delegate.getPrefix(); - } - - @Override - public String getVersion() { - return delegate.getVersion(); - } - - @Override - public boolean isStandalone() { - return delegate.isStandalone(); - } - - @Override - public boolean standaloneSet() { - return delegate.standaloneSet(); - } - - @Override - public String getCharacterEncodingScheme() { - return delegate.getCharacterEncodingScheme(); - } - - @Override - public String getPITarget() { - return delegate.getPITarget(); - } - - @Override - public String getPIData() { - return delegate.getPIData(); - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/core/src/main/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReader.java b/core/src/main/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReader.java new file mode 100644 index 0000000..0e89237 --- /dev/null +++ b/core/src/main/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReader.java @@ -0,0 +1,99 @@ +/** + * 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.staxutils; + +import java.util.Map; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.util.StreamReaderDelegate; + +/** + * A StreamReaderDelegate that expands property references in element and attribute values. + * + */ +public class PropertiesExpandingStreamReader extends StreamReaderDelegate { + + public static final String DELIMITER = "@"; + + private final Map<String, String> props; + + public PropertiesExpandingStreamReader(XMLStreamReader reader, Map<String, String> props) { + super(reader); + this.props = props; + } + + protected String expandProperty(String value) { + if (isEmpty(value)) { + return value; + } + + final int startIndx = value.indexOf(DELIMITER); + if (startIndx > -1) { + final int endIndx = value.lastIndexOf(DELIMITER); + if (endIndx > -1 && startIndx + 1 < endIndx) { + final String propName = value.substring(startIndx + 1, endIndx); + if (!isEmpty(propName)) { + final String envValue = props.get(propName); + if (!isEmpty(envValue)) { + StringBuilder sb = new StringBuilder(); + sb.append(value.substring(0, startIndx)); + sb.append(envValue); + sb.append(value.substring(endIndx + 1)); + value = sb.toString(); + } + } + } + } + + return value; + } + + private static boolean isEmpty(String str) { + if (str != null) { + int len = str.length(); + for (int x = 0; x < len; ++x) { + if (str.charAt(x) > ' ') { + return false; + } + } + } + return true; + } + + @Override + public String getElementText() throws XMLStreamException { + return expandProperty(super.getElementText()); + } + + @Override + public String getAttributeValue(String namespaceURI, String localName) { + return expandProperty(super.getAttributeValue(namespaceURI, localName)); + } + + @Override + public String getAttributeValue(int index) { + return expandProperty(super.getAttributeValue(index)); + } + + @Override + public String getText() { + return expandProperty(super.getText()); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/core/src/main/java/org/apache/cxf/staxutils/SysPropExpandingStreamReader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/staxutils/SysPropExpandingStreamReader.java b/core/src/main/java/org/apache/cxf/staxutils/SysPropExpandingStreamReader.java deleted file mode 100644 index 04a2882..0000000 --- a/core/src/main/java/org/apache/cxf/staxutils/SysPropExpandingStreamReader.java +++ /dev/null @@ -1,93 +0,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. - */ -package org.apache.cxf.staxutils; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -/** - * A wrapper around XMLStreamReader that expands system property references in element and attribute values. - * - */ -public class SysPropExpandingStreamReader extends DelegatingXMLStreamReader { - - public static final String DELIMITER = "@"; - - public SysPropExpandingStreamReader(XMLStreamReader reader) { - super(reader); - } - - protected String expandSystemProperty(String value) { - if (isEmpty(value)) { - return value; - } - - final int startIndx = value.indexOf(DELIMITER); - if (startIndx > -1) { - final int endIndx = value.lastIndexOf(DELIMITER); - if (endIndx > -1 && startIndx + 1 < endIndx) { - final String propName = value.substring(startIndx + 1, endIndx); - if (!isEmpty(propName)) { - final String envValue = System.getProperty(propName); - if (!isEmpty(envValue)) { - StringBuilder sb = new StringBuilder(); - sb.append(value.substring(0, startIndx)); - sb.append(envValue); - sb.append(value.substring(endIndx + 1)); - value = sb.toString(); - } - } - } - } - - return value; - } - - private static boolean isEmpty(String str) { - if (str != null) { - int len = str.length(); - for (int x = 0; x < len; ++x) { - if (str.charAt(x) > ' ') { - return false; - } - } - } - return true; - } - - @Override - public String getElementText() throws XMLStreamException { - return expandSystemProperty(super.getElementText()); - } - - @Override - public String getAttributeValue(String namespaceURI, String localName) { - return expandSystemProperty(super.getAttributeValue(namespaceURI, localName)); - } - - @Override - public String getAttributeValue(int index) { - return expandSystemProperty(super.getAttributeValue(index)); - } - - @Override - public String getText() { - return expandSystemProperty(super.getText()); - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/core/src/test/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReaderTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReaderTest.java b/core/src/test/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReaderTest.java new file mode 100644 index 0000000..c975895 --- /dev/null +++ b/core/src/test/java/org/apache/cxf/staxutils/PropertiesExpandingStreamReaderTest.java @@ -0,0 +1,57 @@ +/** + * 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.staxutils; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.stream.XMLStreamReader; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.apache.cxf.helpers.DOMUtils; +import org.junit.Assert; +import org.junit.Test; + +public class PropertiesExpandingStreamReaderTest extends Assert { + + @Test + public void testSystemPropertyExpansion() throws Exception { + Map<String, String> map = new HashMap<String, String>(); + map.put("bar", "BAR-VALUE"); + map.put("blah", "BLAH-VALUE"); + XMLStreamReader reader = new PropertiesExpandingStreamReader( + StaxUtils.createXMLStreamReader(getTestStream("./resources/sysprops.xml")), map); + Document doc = StaxUtils.read(reader); + Element abc = DOMUtils.getChildrenWithName(doc.getDocumentElement(), + "http://foo/bar", "abc").iterator().next(); + assertEquals("fooBAR-VALUEfoo", abc.getTextContent()); + Element def = DOMUtils.getChildrenWithName(doc.getDocumentElement(), + "http://foo/bar", "def").iterator().next(); + assertEquals("ggggg", def.getTextContent()); + assertEquals("BLAH-VALUE2", def.getAttribute("myAttr")); + } + + private InputStream getTestStream(String resource) { + return getClass().getResourceAsStream(resource); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/core/src/test/java/org/apache/cxf/staxutils/SysPropExpandingStreamReaderTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/staxutils/SysPropExpandingStreamReaderTest.java b/core/src/test/java/org/apache/cxf/staxutils/SysPropExpandingStreamReaderTest.java deleted file mode 100644 index 4b363c7..0000000 --- a/core/src/test/java/org/apache/cxf/staxutils/SysPropExpandingStreamReaderTest.java +++ /dev/null @@ -1,67 +0,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. - */ - -package org.apache.cxf.staxutils; - -import java.io.InputStream; - -import javax.xml.stream.XMLStreamReader; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import org.apache.cxf.helpers.DOMUtils; -import org.junit.Assert; -import org.junit.Test; - -public class SysPropExpandingStreamReaderTest extends Assert { - - @Test - public void testSystemPropertyExpansion() throws Exception { - final String barProp = System.setProperty("bar", "BAR-VALUE"); - final String blahProp = System.setProperty("blah", "BLAH-VALUE"); - try { - XMLStreamReader reader = new SysPropExpandingStreamReader( - StaxUtils.createXMLStreamReader(getTestStream("./resources/sysprops.xml"))); - Document doc = StaxUtils.read(reader); - Element abc = DOMUtils.getChildrenWithName(doc.getDocumentElement(), - "http://foo/bar", "abc").iterator().next(); - assertEquals("fooBAR-VALUEfoo", abc.getTextContent()); - Element def = DOMUtils.getChildrenWithName(doc.getDocumentElement(), - "http://foo/bar", "def").iterator().next(); - assertEquals("ggggg", def.getTextContent()); - assertEquals("BLAH-VALUE2", def.getAttribute("myAttr")); - } finally { - if (barProp != null) { - System.setProperty("bar", barProp); - } else { - System.clearProperty("bar"); - } - if (blahProp != null) { - System.setProperty("blah", blahProp); - } else { - System.clearProperty("blah"); - } - } - } - - private InputStream getTestStream(String resource) { - return getClass().getResourceAsStream(resource); - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/e76e09aa/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java ---------------------------------------------------------------------- diff --git a/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java index bcafef1..6559a3d 100644 --- a/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java +++ b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java @@ -19,6 +19,7 @@ package org.apache.cxf.wsdl11; +import java.util.HashMap; import java.util.Map; import javax.wsdl.Binding; @@ -31,7 +32,7 @@ import javax.wsdl.Service; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamReader; -import org.apache.cxf.staxutils.SysPropExpandingStreamReader; +import org.apache.cxf.staxutils.PropertiesExpandingStreamReader; import org.apache.cxf.staxutils.XMLStreamReaderWrapper; import org.junit.Assert; import org.junit.Test; @@ -112,26 +113,19 @@ public class WSDLManagerImplTest extends Assert { @Test public void testXMLStreamReaderWrapper() throws Exception { - final String testProp = System.setProperty("org.apache.cxf.test.wsdl11.port", "99999"); - try { - String wsdlUrl = getClass().getResource("hello_world_wrap.wsdl").toString(); - WSDLManagerImpl builder = new WSDLManagerImpl(); - builder.setXMLStreamReaderWrapper(new XMLStreamReaderWrapper() { - @Override - public XMLStreamReader wrap(XMLStreamReader reader) { - return new SysPropExpandingStreamReader(reader); - } - }); - Definition def = builder.getDefinition(wsdlUrl); - java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); - builder.getWSDLFactory().newWSDLWriter().writeWSDL(def, bos); - assertTrue(bos.toString().contains("http://localhost:99999/SoapContext/SoapPort")); - } finally { - if (testProp != null) { - System.setProperty("org.apache.cxf.test.wsdl11.port", testProp); - } else { - System.clearProperty("org.apache.cxf.test.wsdl11.port"); + final Map<String, String> map = new HashMap<String, String>(); + map.put("org.apache.cxf.test.wsdl11.port", "99999"); + String wsdlUrl = getClass().getResource("hello_world_wrap.wsdl").toString(); + WSDLManagerImpl builder = new WSDLManagerImpl(); + builder.setXMLStreamReaderWrapper(new XMLStreamReaderWrapper() { + @Override + public XMLStreamReader wrap(XMLStreamReader reader) { + return new PropertiesExpandingStreamReader(reader, map); } - } + }); + Definition def = builder.getDefinition(wsdlUrl); + java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); + builder.getWSDLFactory().newWSDLWriter().writeWSDL(def, bos); + assertTrue(bos.toString().contains("http://localhost:99999/SoapContext/SoapPort")); } }
