Author: dkulp Date: Thu Sep 27 17:33:35 2012 New Revision: 1391123 URL: http://svn.apache.org/viewvc?rev=1391123&view=rev Log: Merged revisions 1391117 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes
........ r1391117 | dkulp | 2012-09-27 13:29:47 -0400 (Thu, 27 Sep 2012) | 11 lines Merged revisions 1391068 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/trunk ........ r1391068 | dkulp | 2012-09-27 11:55:28 -0400 (Thu, 27 Sep 2012) | 3 lines [CXF-4523] Close the XMLStreamReaders Modified patch from Ivan/xuhaihong applied ........ ........ Added: cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/interceptor/StaxInEndingInterceptor.java Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutEndingInterceptor.java cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java Added: cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/interceptor/StaxInEndingInterceptor.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/interceptor/StaxInEndingInterceptor.java?rev=1391123&view=auto ============================================================================== --- cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/interceptor/StaxInEndingInterceptor.java (added) +++ cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/interceptor/StaxInEndingInterceptor.java Thu Sep 27 17:33:35 2012 @@ -0,0 +1,48 @@ +/** + * 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.interceptor; + +import javax.xml.stream.XMLStreamReader; + +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageUtils; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; +import org.apache.cxf.staxutils.StaxUtils; + +public class StaxInEndingInterceptor extends AbstractPhaseInterceptor<Message> { + //certain usages of CXF may require the Stax stream to remain open (example: streaming the stax stuff + //directly to the client applications). Provide a flag to turn off. + public static final String STAX_IN_NOCLOSE = StaxInEndingInterceptor.class.getName() + ".dontClose"; + + public static final StaxInEndingInterceptor INSTANCE = new StaxInEndingInterceptor(); + + public StaxInEndingInterceptor() { + super(Phase.POST_INVOKE); + } + + public void handleMessage(Message message) throws Fault { + XMLStreamReader xtr = message.getContent(XMLStreamReader.class); + if (xtr != null && !MessageUtils.getContextualBoolean(message, STAX_IN_NOCLOSE, false)) { + StaxUtils.close(xtr); + message.removeContent(XMLStreamReader.class); + } + } +} Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original) +++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Thu Sep 27 17:33:35 2012 @@ -1588,5 +1588,25 @@ public final class StaxUtils { writer.flush(); return sw.toString(); } + + public static void close(XMLStreamReader reader) { + if (reader != null) { + try { + reader.close(); + } catch (Exception e) { + //ignore + } + } + } + + public static void close(XMLStreamWriter writer) { + if (writer != null) { + try { + writer.close(); + } catch (Exception e) { + //ignore + } + } + } } Modified: cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java (original) +++ cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java Thu Sep 27 17:33:35 2012 @@ -37,7 +37,7 @@ import org.apache.cxf.phase.Phase; */ public class FIStaxInInterceptor extends AbstractPhaseInterceptor<Message> { public static final String FI_GET_SUPPORTED = "org.apache.cxf.fastinfoset.get.supported"; - + public FIStaxInInterceptor() { this(Phase.POST_STREAM); } @@ -70,6 +70,8 @@ public class FIStaxInInterceptor extends && message.getContent(InputStream.class) != null && message.getContent(XMLStreamReader.class) == null) { message.setContent(XMLStreamReader.class, getParser(message.getContent(InputStream.class))); + //add the StaxInEndingInterceptor which will close the reader + message.getInterceptorChain().add(StaxInEndingInterceptor.INSTANCE); ct = ct.replace("fastinfoset", "xml"); if (ct.contains("application/xml")) { Modified: cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java (original) +++ cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java Thu Sep 27 17:33:35 2012 @@ -45,9 +45,10 @@ import org.apache.cxf.staxutils.StaxUtil * Creates an XMLStreamReader from the InputStream on the Message. */ public class StaxInInterceptor extends AbstractPhaseInterceptor<Message> { + private static final Logger LOG = LogUtils.getL7dLogger(StaxInInterceptor.class); - private static Map<Object, XMLInputFactory> factories = new HashMap<Object, XMLInputFactory>(); + private static Map<Object, XMLInputFactory> factories = new HashMap<Object, XMLInputFactory>(); public StaxInInterceptor() { super(Phase.POST_STREAM); @@ -114,6 +115,7 @@ public class StaxInInterceptor extends A } message.setContent(XMLStreamReader.class, reader); + message.getInterceptorChain().add(StaxInEndingInterceptor.INSTANCE); } public static XMLInputFactory getXMLInputFactory(Message m) throws Fault { Modified: cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutEndingInterceptor.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutEndingInterceptor.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutEndingInterceptor.java (original) +++ cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutEndingInterceptor.java Thu Sep 27 17:33:35 2012 @@ -28,6 +28,7 @@ import org.apache.cxf.common.i18n.Bundle import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; +import org.apache.cxf.staxutils.StaxUtils; public class StaxOutEndingInterceptor extends AbstractPhaseInterceptor<Message> { @@ -45,9 +46,12 @@ public class StaxOutEndingInterceptor ex try { XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class); if (xtw != null) { - xtw.writeEndDocument(); - xtw.flush(); - xtw.close(); + try { + xtw.writeEndDocument(); + xtw.flush(); + } finally { + StaxUtils.close(xtw); + } } OutputStream os = (OutputStream)message.get(outStreamHolder); Modified: cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java (original) +++ cxf/branches/2.5.x-fixes/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java Thu Sep 27 17:33:35 2012 @@ -43,6 +43,7 @@ import javax.wsdl.factory.WSDLFactory; import javax.wsdl.xml.WSDLReader; import javax.xml.bind.JAXBException; import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -227,8 +228,10 @@ public class WSDLManagerImpl implements Definition def = null; if (src.getByteStream() != null || src.getCharacterStream() != null) { Document doc; + XMLStreamReader xmlReader = null; try { - doc = StaxUtils.read(StaxUtils.createXMLStreamReader(src), true); + xmlReader = StaxUtils.createXMLStreamReader(src); + doc = StaxUtils.read(xmlReader, true); if (src.getSystemId() != null) { try { doc.setDocumentURI(new String(src.getSystemId())); @@ -238,6 +241,8 @@ public class WSDLManagerImpl implements } } catch (Exception e) { throw new WSDLException(WSDLException.PARSER_ERROR, e.getMessage(), e); + } finally { + StaxUtils.close(xmlReader); } def = reader.readWSDL(wsdlLocator, doc.getDocumentElement()); } else { Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1391123&r1=1391122&r2=1391123&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java (original) +++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java Thu Sep 27 17:33:35 2012 @@ -58,6 +58,7 @@ import org.apache.cxf.endpoint.Retryable import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.interceptor.Interceptor; +import org.apache.cxf.interceptor.StaxInEndingInterceptor; import org.apache.cxf.jaxrs.impl.MetadataMap; import org.apache.cxf.jaxrs.impl.UriBuilderImpl; import org.apache.cxf.jaxrs.model.ParameterType; @@ -791,6 +792,8 @@ public abstract class AbstractClient imp exchange.put(MessageObserver.class, new ClientMessageObserver(cfg)); exchange.put(Endpoint.class, cfg.getConduitSelector().getEndpoint()); exchange.put("org.apache.cxf.http.no_io_exceptions", true); + //REVISIT - when response handling is actually put onto the in chain, this will likely not be needed + exchange.put(StaxInEndingInterceptor.STAX_IN_NOCLOSE, Boolean.TRUE); m.setExchange(exchange); return exchange; }
