knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmin...@apache.org
Subject [1/3] git commit: Fixed improper handling of form ended params with multiple vlaues.
Date Fri, 15 Mar 2013 17:38:50 GMT
Fixed improper handling of form ended params with multiple vlaues.


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

Branch: refs/heads/master
Commit: fe50e9b2f752db8893682d6707282d19d823425b
Parents: 37b6d21
Author: Kevin Minder <kevin.minder@hortonworks.com>
Authored: Fri Mar 15 13:35:42 2013 -0400
Committer: Kevin Minder <kevin.minder@hortonworks.com>
Committed: Fri Mar 15 13:36:21 2013 -0400

----------------------------------------------------------------------
 .../gateway/PseudoIdentityAsserterMessages.java    |   31 +++++++++
 ...IdentityAssertionHttpServletRequestWrapper.java |   49 +++++++++------
 ...tityAssertionHttpServletRequestWrapperTest.java |   28 ++++++++
 3 files changed, 88 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/fe50e9b2/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/PseudoIdentityAsserterMessages.java
----------------------------------------------------------------------
diff --git a/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/PseudoIdentityAsserterMessages.java
b/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/PseudoIdentityAsserterMessages.java
new file mode 100644
index 0000000..5a34234
--- /dev/null
+++ b/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/PseudoIdentityAsserterMessages.java
@@ -0,0 +1,31 @@
+/**
+ * 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.hadoop.gateway;
+
+import org.apache.hadoop.gateway.i18n.messages.Message;
+import org.apache.hadoop.gateway.i18n.messages.MessageLevel;
+import org.apache.hadoop.gateway.i18n.messages.Messages;
+import org.apache.hadoop.gateway.i18n.messages.StackTrace;
+
+@Messages(logger="org.apache.hadoop.gateway")
+public interface PseudoIdentityAsserterMessages {
+
+  @Message( level = MessageLevel.WARN, text = "Skipping unencodable parameter {0}={1}, {2}:
{3}" )
+  void skippingUnencodableParameter( String name, String value, String encoding, @StackTrace(
level = MessageLevel.DEBUG ) Exception e );
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/fe50e9b2/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapper.java
----------------------------------------------------------------------
diff --git a/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapper.java
b/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapper.java
index 716bc91..526a344 100644
--- a/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapper.java
+++ b/gateway-provider-identity-assertion-pseudo/src/main/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapper.java
@@ -18,6 +18,8 @@
 package org.apache.hadoop.gateway.filter;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.gateway.PseudoIdentityAsserterMessages;
+import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
 
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
@@ -37,6 +39,8 @@ import java.util.Map;
 
 public class IdentityAssertionHttpServletRequestWrapper extends HttpServletRequestWrapper
{
 
+  private static PseudoIdentityAsserterMessages log = MessagesFactory.get( PseudoIdentityAsserterMessages.class
);
+
   private static final String PRINCIPAL_PARAM = "user.name";
   
   String username = null;
@@ -159,31 +163,36 @@ public class IdentityAssertionHttpServletRequestWrapper extends HttpServletReque
     }
   }
 
-  static String urlEncode( Map<String,String[]> map, String encoding ) {
+  static String urlEncode( Map<String, String[]> map, String encoding ) {
     StringBuilder sb = new StringBuilder();
-    for (Map.Entry<?,?> entry : map.entrySet()) {
-        if (sb.length() > 0) {
-            sb.append("&");
-        }
-        String[] values = (String[]) entry.getValue();
-        for (int i = 0; i < values.length; i++) {
-          if (values[i] != null) {
-            try {
-            sb.append(String.format("%s=%s",
-                urlEncode( entry.getKey().toString(), encoding ),
-                urlEncode( values[i], encoding )
-            ));
-            }
-            catch (IllegalArgumentException e) {
-              e.printStackTrace();
-              System.out.println("SKIPPING PARAM: " + entry.getKey().toString() + " with
value: " + values[i]);
+    for( Map.Entry<String,String[]> entry : map.entrySet() ) {
+      String name = entry.getKey();
+      if( name != null && name.length() > 0 ) {
+        String[] values = entry.getValue();
+        if( values == null || values.length == 0 ) {
+          sb.append( entry.getKey() );
+        } else {
+          for( int i = 0; i < values.length; i++ ) {
+            String value = values[ i ];
+            if( value != null ) {
+              if( sb.length() > 0 ) {
+                sb.append( "&" );
+              }
+              try {
+                sb.append( urlEncode( name, encoding ) );
+                sb.append( "=" );
+                sb.append( urlEncode( value, encoding ) );
+              } catch( IllegalArgumentException e ) {
+                log.skippingUnencodableParameter( name, value, encoding, e );
+              }
             }
           }
         }
+      }
     }
-    return sb.toString();       
-}  
-  
+    return sb.toString();
+  }
+
   @SuppressWarnings({ "deprecation", "unchecked" })
   private static Map<String,String[]> parseQueryString( String queryString ) {
     return javax.servlet.http.HttpUtils.parseQueryString( queryString );

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/fe50e9b2/gateway-provider-identity-assertion-pseudo/src/test/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapperTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-identity-assertion-pseudo/src/test/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapperTest.java
b/gateway-provider-identity-assertion-pseudo/src/test/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapperTest.java
index 87751cc..e2f1fca 100644
--- a/gateway-provider-identity-assertion-pseudo/src/test/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapperTest.java
+++ b/gateway-provider-identity-assertion-pseudo/src/test/java/org/apache/hadoop/gateway/filter/IdentityAssertionHttpServletRequestWrapperTest.java
@@ -27,9 +27,11 @@ import org.junit.experimental.categories.Category;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.HashMap;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.not;
 
 @Category( { UnitTests.class, FastTests.class } )
@@ -202,4 +204,30 @@ public class IdentityAssertionHttpServletRequestWrapperTest {
     assertThat( output, not( containsString( "input-user" ) ) );
   }
 
+  @Test
+  public void testUrlEncode() {
+    String s;
+    HashMap<String,String[]> m;
+
+    m = new HashMap<String,String[]>();
+    m.put( "null-values", null );
+    s = IdentityAssertionHttpServletRequestWrapper.urlEncode( m, "UTF-8" );
+    assertThat( s, is( "null-values" ) );
+
+    m = new HashMap<String,String[]>();
+    m.put( "no-values", new String[0] );
+    s = IdentityAssertionHttpServletRequestWrapper.urlEncode( m, "UTF-8" );
+    assertThat( s, is( "no-values" ) );
+
+    m = new HashMap<String,String[]>();
+    m.put( "one-value", new String[]{ "value1" } );
+    s = IdentityAssertionHttpServletRequestWrapper.urlEncode( m, "UTF-8" );
+    assertThat( s, is( "one-value=value1" ) );
+
+    m = new HashMap<String,String[]>();
+    m.put( "two-values", new String[]{ "value1", "value2" } );
+    s = IdentityAssertionHttpServletRequestWrapper.urlEncode( m, "UTF-8" );
+    assertThat( s, is( "two-values=value1&two-values=value2" ) );
+  }
+
 }


Mime
View raw message