You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2019/07/24 12:19:16 UTC

[cxf] branch master updated: CXF-8032 - Adding LoggingFeature enables chunking response

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new a8b17dd  CXF-8032 - Adding LoggingFeature enables chunking response
a8b17dd is described below

commit a8b17dd9fa263564f29f4e2b254d1e9e9bdf5a6f
Author: kkrisz1 <kr...@gmail.com>
AuthorDate: Mon Jul 22 16:19:41 2019 +0200

    CXF-8032 - Adding LoggingFeature enables chunking response
---
 .../cxf/ext/logging/LoggingOutInterceptor.java     |  2 +-
 .../cxf/ext/logging/LoggingOutputStream.java       | 74 ++++++++++++++++++++++
 .../cxf/systest/jaxrs/logging/LoggingServer.java   | 55 ++++++++++++++++
 .../cxf/systest/jaxrs/logging/LoggingTest.java     | 68 ++++++++++++++++++++
 4 files changed, 198 insertions(+), 1 deletion(-)

diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutInterceptor.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutInterceptor.java
index 627c8fa..bacaf00 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutInterceptor.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutInterceptor.java
@@ -76,7 +76,7 @@ public class LoggingOutInterceptor extends AbstractLoggingInterceptor {
     }
 
     private OutputStream createCachingOut(Message message, final OutputStream os, CachedOutputStreamCallback callback) {
-        final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
+        final CacheAndWriteOutputStream newOut = new LoggingOutputStream(os);
         if (threshold > 0) {
             newOut.setThreshold(threshold);
         }
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java
new file mode 100644
index 0000000..b61d029
--- /dev/null
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java
@@ -0,0 +1,74 @@
+/**
+ * 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.ext.logging;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.cxf.io.CacheAndWriteOutputStream;
+
+public class LoggingOutputStream extends CacheAndWriteOutputStream {
+    private boolean skipFlushingFlowThroughStream;
+
+    LoggingOutputStream(OutputStream stream) {
+        super(stream);
+    }
+
+    /**
+     * Override, because there is no need to flush the flow-through stream.
+     * Flushing will be done by the underlying OutputStream.
+     *
+     * @see org.apache.cxf.io.AbstractThresholdOutputStream#close()
+     */
+    @Override
+    public void closeFlowthroughStream() throws IOException {
+        getFlowThroughStream().close();
+    }
+
+    /**
+     * Override, because there is no need to flush the flow-through stream.
+     * Flushing will be done by the underlying OutputStream.
+     *
+     * @see org.apache.cxf.io.AbstractThresholdOutputStream#close()
+     */
+    @Override
+    protected void postClose() throws IOException {
+        getFlowThroughStream().close();
+    }
+
+    /**
+     * Flush the flow-through stream if the current stream is also flushed.
+     */
+    @Override
+    protected void doFlush() throws IOException {
+        if (skipFlushingFlowThroughStream) {
+            return;
+        }
+
+        getFlowThroughStream().flush();
+    }
+
+    @Override
+    public void writeCacheTo(StringBuilder out, String charsetName, long limit) throws IOException {
+        skipFlushingFlowThroughStream = true;
+        super.writeCacheTo(out, charsetName, limit);
+        skipFlushingFlowThroughStream = false;
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingServer.java
new file mode 100644
index 0000000..4930e13
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingServer.java
@@ -0,0 +1,55 @@
+/**
+ * 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.systest.jaxrs.logging;
+
+
+import org.apache.cxf.ext.logging.LoggingFeature;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.systest.jaxrs.BookStore;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+
+public class LoggingServer extends AbstractBusTestServerBase {
+    static final String PORT = allocatePort(LoggingServer.class);
+
+    protected void run() {
+        final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+
+        sf.setResourceClasses(BookStore.class);
+        sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), false));
+        sf.setAddress("http://localhost:" + PORT + "/");
+
+        sf.getFeatures().add(new LoggingFeature());
+
+        sf.create();
+    }
+
+    public static void main(String[] args) {
+        try {
+            final LoggingServer s = new LoggingServer();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingTest.java
new file mode 100644
index 0000000..c45f9f7
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/logging/LoggingTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.systest.jaxrs.logging;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.systest.jaxrs.Book;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+
+public class LoggingTest extends AbstractBusClientServerTestBase {
+    @BeforeClass
+    public static void startServers() {
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly", launchServer(LoggingServer.class, true));
+    }
+
+    @Test
+    public void testEchoBookElement() {
+        final Response response = createWebClient("/bookstore/books/element/echo", MediaType.APPLICATION_XML)
+                .post(new Book("CXF", 123L));
+        assertEquals(200, response.getStatus());
+        assertEquals(96, response.getLength());
+
+        final Book book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+        assertEquals("CXF", book.getName());
+    }
+
+    protected WebClient createWebClient(final String url, final String mediaType) {
+        final List<?> providers = Collections.singletonList(new JacksonJsonProvider());
+
+        return WebClient
+                .create("http://localhost:" + LoggingServer.PORT + url, providers)
+                .accept(mediaType);
+    }
+}