Repository: knox
Updated Branches:
  refs/heads/master ff03a0090 -> 2bd260120


KNOX-1532 - Only decompress/recompress when there are rewrite rules

Signed-off-by: Kevin Risden <kris...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/2bd26012
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/2bd26012
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/2bd26012

Branch: refs/heads/master
Commit: 2bd2601206a26b586be11f720beeaf626af8d6b4
Parents: ff03a00
Author: Kevin Risden <kris...@apache.org>
Authored: Fri Oct 19 12:21:35 2018 -0400
Committer: Kevin Risden <kris...@apache.org>
Committed: Tue Oct 23 08:47:51 2018 -0400

----------------------------------------------------------------------
 .../api/UrlRewriteStreamFilterFactory.java      | 24 ++++----
 .../filter/rewrite/impl/UrlRewriteRequest.java  | 12 +++-
 .../filter/rewrite/impl/UrlRewriteResponse.java | 65 ++++++++++----------
 .../impl/noop/NoOpUrlRewriteStreamFilter.java   | 55 -----------------
 ...ay.filter.rewrite.spi.UrlRewriteStreamFilter |  1 -
 5 files changed, 57 insertions(+), 100 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/2bd26012/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
----------------------------------------------------------------------
diff --git 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
index 2d83e54..af8fd86 100644
--- 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
+++ 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteStreamFilterFactory.java
@@ -25,6 +25,7 @@ import javax.activation.MimeType;
 import javax.activation.MimeTypeParseException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -32,13 +33,12 @@ import java.util.ServiceLoader;
 
 public abstract class UrlRewriteStreamFilterFactory {
 
-  private static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
-
   private static final Map<String,Map<String,UrlRewriteStreamFilter>> MAP = 
loadFactories();
 
   private UrlRewriteStreamFilterFactory() {
   }
 
+  @Deprecated
   public static InputStream create(
       MimeType type,
       String name,
@@ -48,27 +48,29 @@ public abstract class UrlRewriteStreamFilterFactory {
       UrlRewriter.Direction direction,
       UrlRewriteFilterContentDescriptor config )
           throws IOException {
-    InputStream filteredStream = null;
-    Map<String,UrlRewriteStreamFilter> nameMap = getNameMap( type );
-    UrlRewriteStreamFilter filter = getFilter( nameMap, name );
-    String charset = MimeTypes.getCharset( type, DEFAULT_CHARACTER_ENCODING );
+    UrlRewriteStreamFilter filter = create(type, name);
+    String charset = MimeTypes.getCharset( type, 
StandardCharsets.ISO_8859_1.name() );
+    final InputStream filteredStream;
     if( filter != null ) {
       filteredStream = filter.filter( stream, charset, rewriter, resolver, 
direction, config );
+    } else {
+      filteredStream = stream;
     }
     return filteredStream;
   }
 
+  public static UrlRewriteStreamFilter create(MimeType type, String name) {
+    Map<String,UrlRewriteStreamFilter> nameMap = getNameMap( type );
+    return getFilter( nameMap, name );
+  }
+
   private static Map<String,Map<String,UrlRewriteStreamFilter>> 
loadFactories() {
     Map<String,Map<String,UrlRewriteStreamFilter>> typeMap = new HashMap<>();
     ServiceLoader<UrlRewriteStreamFilter> filters = ServiceLoader.load( 
UrlRewriteStreamFilter.class );
     for( UrlRewriteStreamFilter filter : filters ) {
       String[] types = filter.getTypes();
       for( String type: types ) {
-        Map<String,UrlRewriteStreamFilter> nameMap = typeMap.get( type );
-        if( nameMap == null ) {
-          nameMap = new LinkedHashMap<String,UrlRewriteStreamFilter>();
-          typeMap.put( type, nameMap );
-        }
+        Map<String, UrlRewriteStreamFilter> nameMap = 
typeMap.computeIfAbsent(type, k -> new LinkedHashMap<>());
         for( String name: filter.getNames() ) {
           nameMap.put( name, filter );
         }

http://git-wip-us.apache.org/repos/asf/knox/blob/2bd26012/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteRequest.java
----------------------------------------------------------------------
diff --git 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteRequest.java
 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteRequest.java
index 6bd0e34..b274bd2 100644
--- 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteRequest.java
+++ 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteRequest.java
@@ -28,6 +28,7 @@ import 
org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter;
 import 
org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStreamFilterFactory;
 import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter;
 import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteMessages;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter;
 import org.apache.knox.gateway.i18n.messages.MessagesFactory;
 import org.apache.knox.gateway.util.MimeTypes;
 import org.apache.knox.gateway.util.urltemplate.Parser;
@@ -48,6 +49,7 @@ import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.List;
@@ -265,7 +267,15 @@ public class UrlRewriteRequest extends 
GatewayRequestWrapper implements Resolver
           mimeType = MimeTypes.create(asType, getCharacterEncoding());
         }
       }
-      InputStream stream = UrlRewriteStreamFilterFactory.create( mimeType, 
null, input, rewriter, this, UrlRewriter.Direction.IN, filterContentConfig );
+
+      final InputStream stream;
+      UrlRewriteStreamFilter filter = 
UrlRewriteStreamFilterFactory.create(mimeType, null);
+      if(filter != null) {
+        String charset = MimeTypes.getCharset( mimeType, 
StandardCharsets.ISO_8859_1.name() );
+        stream = filter.filter(input, charset, rewriter, this, 
UrlRewriter.Direction.IN, filterContentConfig );
+      } else {
+        stream = input;
+      }
       input = new UrlRewriteRequestStream( stream );
     }
     return input;

http://git-wip-us.apache.org/repos/asf/knox/blob/2bd26012/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
----------------------------------------------------------------------
diff --git 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
index 8d8f401..6c1ee3d 100644
--- 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
+++ 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
@@ -25,6 +25,7 @@ import 
org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter;
 import 
org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStreamFilterFactory;
 import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter;
 import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteMessages;
+import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter;
 import org.apache.knox.gateway.i18n.messages.MessagesFactory;
 import org.apache.knox.gateway.util.MimeTypes;
 import org.apache.knox.gateway.util.Urls;
@@ -46,6 +47,7 @@ import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.URISyntaxException;
 import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
@@ -92,8 +94,7 @@ public class UrlRewriteResponse extends 
GatewayResponseWrapper implements Params
   private String xForwardedPort;
   private String xForwardedScheme;
 
-  public UrlRewriteResponse( FilterConfig config, HttpServletRequest request, 
HttpServletResponse response )
-      throws IOException {
+  public UrlRewriteResponse( FilterConfig config, HttpServletRequest request, 
HttpServletResponse response ) {
     super( response );
     this.rewriter = UrlRewriteServletContextListener.getUrlRewriter( 
config.getServletContext() );
     this.config = config;
@@ -150,25 +151,9 @@ public class UrlRewriteResponse extends 
GatewayResponseWrapper implements Params
 
   @Override
   public void streamResponse( InputStream input, OutputStream output ) throws 
IOException {
-    InputStream  inStream;
-    OutputStream outStream;
-
-
-    // Use this way to check whether the input stream is gzip compressed, in 
case
-    // the content encoding header is unknown, as it could be unset in inbound 
response
-    boolean isGzip = false;
-    BufferedInputStream inBuffer = new BufferedInputStream(input, 
STREAM_BUFFER_SIZE);
-    inBuffer.mark(2);
-    byte [] signature = new byte[2];
-    int len = inBuffer.read( signature ); //read the signature
-    if( len == 2 && signature[ 0 ] == (byte) 0x1f && signature[ 1 ] == (byte) 
0x8b ) {
-      isGzip = true;
-    }
-    inBuffer.reset();
-
     MimeType mimeType = getMimeType();
     UrlRewriteFilterContentDescriptor filterContentConfig =
-                                                
getRewriteFilterConfig(rewriter.getConfig(), bodyFilterName, mimeType);
+        getRewriteFilterConfig(rewriter.getConfig(), bodyFilterName, mimeType);
     if (filterContentConfig != null) {
       String asType = filterContentConfig.asType();
       if ( asType != null && asType.trim().length() > 0 ) {
@@ -176,24 +161,40 @@ public class UrlRewriteResponse extends 
GatewayResponseWrapper implements Params
       }
     }
 
-    if(isGzip) {
-      inStream = new GZIPInputStream(new 
GZIPInputStreamHelperInputStream(inBuffer), STREAM_BUFFER_SIZE);
+    UrlRewriteStreamFilter filter = 
UrlRewriteStreamFilterFactory.create(mimeType, null);
+
+    final InputStream inStream;
+    final OutputStream outStream;
+    if( filter != null ) {
+      // Use this way to check whether the input stream is gzip compressed, in 
case
+      // the content encoding header is unknown, as it could be unset in 
inbound response
+      boolean isGzip = false;
+      final BufferedInputStream inBuffer = new BufferedInputStream(input, 
STREAM_BUFFER_SIZE);
+      inBuffer.mark(2);
+      byte [] signature = new byte[2];
+      int len = inBuffer.read(signature);
+      if( len == 2 && signature[ 0 ] == (byte) 0x1f && signature[ 1 ] == 
(byte) 0x8b ) {
+        isGzip = true;
+      }
+      inBuffer.reset();
+
+      final InputStream unFilteredStream;
+      if(isGzip) {
+        unFilteredStream = new GZIPInputStream(new 
GZIPInputStreamHelperInputStream(inBuffer), STREAM_BUFFER_SIZE);
+      } else {
+        unFilteredStream = inBuffer;
+      }
+      String charset = MimeTypes.getCharset( mimeType, 
StandardCharsets.ISO_8859_1.name() );
+      inStream = filter.filter( unFilteredStream, charset, rewriter, this, 
UrlRewriter.Direction.OUT, filterContentConfig );
+      outStream = (isGzip) ? new GZIPOutputStream(output, STREAM_BUFFER_SIZE) 
: output;
     } else {
-      inStream = inBuffer;
+      inStream = input;
+      outStream = output;
     }
 
-    InputStream filteredInput = UrlRewriteStreamFilterFactory.create(mimeType,
-                                                                     null,
-                                                                     inStream,
-                                                                     rewriter,
-                                                                     this,
-                                                                     
UrlRewriter.Direction.OUT,
-                                                                     
filterContentConfig);
-    outStream = (isGzip) ? new GZIPOutputStream(output, STREAM_BUFFER_SIZE) : 
output;
     try {
-      IOUtils.copyLarge(filteredInput, outStream, new 
byte[STREAM_BUFFER_SIZE]);
+      IOUtils.copy(inStream, outStream, STREAM_BUFFER_SIZE);
     } finally {
-      //KNOX-685: outStream.flush();
       outStream.close();
     }
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/2bd26012/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java
----------------------------------------------------------------------
diff --git 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java
 
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java
deleted file mode 100644
index 6141e0d..0000000
--- 
a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/noop/NoOpUrlRewriteStreamFilter.java
+++ /dev/null
@@ -1,55 +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.knox.gateway.filter.rewrite.impl.noop;
-
-import 
org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
-import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter;
-import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter;
-import org.apache.knox.gateway.util.urltemplate.Resolver;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-public class NoOpUrlRewriteStreamFilter implements UrlRewriteStreamFilter {
-
-  private static String[] TYPES = new String[]{ null };
-  private static String[] NAMES = new String[]{ null };
-
-  @Override
-  public String[] getTypes() {
-    return TYPES;
-  }
-
-  @Override
-  public String[] getNames() {
-    return NAMES;
-  }
-
-  @Override
-  public InputStream filter(
-      InputStream stream,
-      String encoding,
-      UrlRewriter rewriter,
-      Resolver resolver,
-      UrlRewriter.Direction direction,
-      UrlRewriteFilterContentDescriptor config )
-          throws IOException {
-    return stream;
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/knox/blob/2bd26012/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter
----------------------------------------------------------------------
diff --git 
a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter
 
b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter
index 0d347e1..87c3030 100644
--- 
a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter
+++ 
b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStreamFilter
@@ -20,5 +20,4 @@ 
org.apache.knox.gateway.filter.rewrite.impl.json.JsonUrlRewriteStreamFilter
 org.apache.knox.gateway.filter.rewrite.impl.xml.XmlUrlRewriteStreamFilter
 org.apache.knox.gateway.filter.rewrite.impl.html.HtmlUrlRewriteStreamFilter
 org.apache.knox.gateway.filter.rewrite.impl.form.FormUrlRewriteStreamFilter
-org.apache.knox.gateway.filter.rewrite.impl.noop.NoOpUrlRewriteStreamFilter
 
org.apache.knox.gateway.filter.rewrite.impl.javascript.JavaScriptUrlRewriteStreamFilter
\ No newline at end of file

Reply via email to