diff --git a/api/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java b/api/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java index 831230e23d0..5bd58024584 100644 --- a/api/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java +++ b/api/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java @@ -57,6 +57,10 @@ public OutgoingChainInterceptor() { public void handleMessage(Message message) { Exchange ex = message.getExchange(); BindingOperationInfo binding = ex.get(BindingOperationInfo.class); + //if we get this far, we're going to be outputting some valid content, but we COULD + //also be "echoing" some of the content from the input. Thus, we need to + //mark it as requiring the input to be cached. + message.put("cxf.io.cacheinput", Boolean.TRUE); if (null != binding && null != binding.getOperationInfo() && binding.getOperationInfo().isOneWay()) { closeInput(message); return; diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java index 68b7ae5d869..19510d94b0a 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java @@ -523,21 +523,33 @@ private void cacheInput(Message outMessage) { if (inMessage == null) { return; } - Collection atts = inMessage.getAttachments(); - if (atts != null) { - for (Attachment a : atts) { - if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) { - try { - ((AttachmentDataSource)a.getDataHandler().getDataSource()).cache(inMessage); - } catch (IOException e) { - throw new Fault(e); + Object o = inMessage.get("cxf.io.cacheinput"); + DelegatingInputStream in = inMessage.getContent(DelegatingInputStream.class); + if (MessageUtils.isTrue(o)) { + Collection atts = inMessage.getAttachments(); + if (atts != null) { + for (Attachment a : atts) { + if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) { + try { + ((AttachmentDataSource)a.getDataHandler().getDataSource()).cache(inMessage); + } catch (IOException e) { + throw new Fault(e); + } } } } - } - DelegatingInputStream in = inMessage.getContent(DelegatingInputStream.class); - if (in != null) { - in.cacheInput(); + if (in != null) { + in.cacheInput(); + } + } else if (in != null) { + //We don't need to cache it, but we may need to consume it in order for the client + // to be able to receive a response. (could be blocked sending) + //However, also don't want to consume indefinitely. We'll limit to 16M. + try { + IOUtils.consume(in, 16 * 1024 * 1024); + } catch (IOException ioe) { + //ignore + } } }