Skip to content
Permalink
Browse files
[CXF-5450] Fix some issues with SAAJ + Provider/Dispatch + Headers ca…
…using out of memory errors

git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1549694 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Dec 9, 2013
1 parent 9e27007 commit 10d86cfe1b44ea471a6ddade1ef97b0090b29fa5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
@@ -27,6 +27,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import org.apache.cxf.common.util.StringUtils;

@@ -40,6 +41,7 @@ public class OverlayW3CDOMStreamWriter extends W3CDOMStreamWriter {

List<Boolean> isOverlaidStack = new LinkedList<Boolean>();
boolean isOverlaid = true;
boolean textOverlay = false;

public OverlayW3CDOMStreamWriter(Document document) {
super(document);
@@ -80,6 +82,7 @@ public void writeEndElement() throws XMLStreamException {
}
isOverlaid = isOverlaidStack.remove(0);
super.writeEndElement();
textOverlay = false;
}
public void writeStartElement(String local) throws XMLStreamException {
isOverlaidStack.add(0, isOverlaid);
@@ -198,4 +201,21 @@ public void writeStartElement(String prefix, String local, String namespace) thr
}
}

public void writeCharacters(String text) throws XMLStreamException {
if (!isOverlaid) {
super.writeCharacters(text);
} else if (!textOverlay) {
Element nd = getCurrentNode();
Node txt = nd.getFirstChild();
if (txt instanceof Text
&& ((Text)txt).getTextContent().startsWith(text)) {
textOverlay = true;
return;
}
super.writeCharacters(text);
}

}


}
@@ -60,6 +60,7 @@
import org.apache.cxf.service.model.ServiceModelUtil;
import org.apache.cxf.staxutils.DelegatingXMLStreamWriter;
import org.apache.cxf.staxutils.StaxUtils;
import org.apache.cxf.staxutils.W3CDOMStreamWriter;
import org.apache.cxf.ws.addressing.EndpointReferenceUtils;

public class SoapOutInterceptor extends AbstractSoapInterceptor {
@@ -142,6 +143,13 @@ private void writeSoapEnvelopeStart(final SoapMessage message) {
List<Header> hdrList = message.getHeaders();
for (Header header : hdrList) {
XMLStreamWriter writer = xtw;
if (xtw instanceof W3CDOMStreamWriter) {
Element nd = ((W3CDOMStreamWriter)xtw).getCurrentNode();
if (header.getObject() instanceof Element
&& nd.isSameNode(((Element)header.getObject()).getParentNode())) {
continue;
}
}
if (header instanceof SoapHeader) {
SoapHeader soapHeader = (SoapHeader)header;
writer = new SOAPHeaderWriter(xtw, soapHeader, soapVersion, soapPrefix);
@@ -0,0 +1,81 @@
/**
* 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.binding.soap.saaj;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Element;

import org.apache.cxf.BusFactory;
import org.apache.cxf.binding.soap.Soap11;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.TestBase;
import org.apache.cxf.binding.soap.TestUtil;
import org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.staxutils.W3CDOMStreamWriter;

import org.junit.Before;
import org.junit.Test;


public class SAAJOutInterceptorTest extends TestBase {

private SoapOutInterceptor soi;

@Before
public void setUp() throws Exception {
super.setUp();

soi = new SoapOutInterceptor(BusFactory.getDefaultBus());
}

@Test
public void testHandleHeader() throws Exception {
soapMessage = TestUtil.createEmptySoapMessage(Soap11.getInstance(), chain);
soapMessage.setContent(OutputStream.class, new ByteArrayOutputStream());

SOAPMessage m = SAAJFactoryResolver.createMessageFactory(soapMessage.getVersion()).createMessage();

InputStream ins = getClass().getResourceAsStream("../test-soap-header.xml");
m.getSOAPPart().setContent(new StreamSource(ins));

Element el = DOMUtils.getFirstElement(m.getSOAPPart().getEnvelope().getHeader());
List<Header> h = soapMessage.getHeaders();
while (el != null) {
h.add(new SoapHeader(DOMUtils.getElementQName(el), el));
el = DOMUtils.getNextElement(el);
}
soapMessage.setContent(SOAPMessage.class, m);
W3CDOMStreamWriter writer = new SAAJStreamWriter(m.getSOAPPart());
soapMessage.setContent(XMLStreamWriter.class, writer);
soi.handleMessage(soapMessage);

m.writeTo(System.out);
}
}

0 comments on commit 10d86cf

Please sign in to comment.