You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2013/04/24 13:19:21 UTC

svn commit: r1471369 - in /myfaces/tobago/branches/tobago-1.5.x: tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/ tobago-core/src/test/java/org/apache/myfaces/tobago/internal...

Author: lofwyr
Date: Wed Apr 24 11:19:20 2013
New Revision: 1471369

URL: http://svn.apache.org/r1471369
Log:
TOBAGO-1254: Improve logging
- only replace contentType when it's needed: "text/html; charset=UTF-8" should be handle as same as "text/html;charset=UTF-8" (without space)

Added:
    myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/
    myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StringUtilsUnitTest.java
Modified:
    myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
    myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StringUtils.java
    myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java
    myfaces/tobago/branches/tobago-1.5.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java

Modified: myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java?rev=1471369&r1=1471368&r2=1471369&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java (original)
+++ myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java Wed Apr 24 11:19:20 2013
@@ -63,11 +63,11 @@ public class ResponseUtils {
       response.setContentType(contentType);
     } else {
       String responseContentType = response.getContentType();
-      if (!responseContentType.equalsIgnoreCase(contentType)) {
+      if (!StringUtils.equalsIgnoreCaseAndWhitespace(responseContentType, contentType)) {
         response.setContentType(contentType);
-        if (LOG.isInfoEnabled()) {
-          LOG.info("Reponse already contains Header Content-Type '" + responseContentType
-              + "'. Setting Content-Type to '" + contentType + "'");
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Response already contains Header Content-Type '" + responseContentType
+              + "'. Overwriting with '" + contentType + "'");
         }
       }
     }

Modified: myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StringUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StringUtils.java?rev=1471369&r1=1471368&r2=1471369&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StringUtils.java (original)
+++ myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/StringUtils.java Wed Apr 24 11:19:20 2013
@@ -101,4 +101,66 @@ public final class StringUtils {
     }
     return builder.toString();
   }
+
+  /**
+   * Is the same string, by ignoring differences that are only whitespaces.
+   * (null and "" are not equal)
+   */
+  @SuppressWarnings("StringEquality")
+  public static boolean equalsIgnoreCaseAndWhitespace(final String type1, final String type2) {
+
+    // StringEquality
+    if (type1 == type2) {
+      return true;
+    }
+
+    if (type1 == null || type2 == null) {
+      return false;
+    }
+
+    final char[] chars1 = type1.toCharArray();
+    final char[] chars2 = type2.toCharArray();
+    final int length1 = chars1.length;
+    final int length = chars2.length;
+
+    int i = 0;
+    int j = 0;
+
+    while (i < length1 && j < length) {
+      if (chars1[i] == chars2[j] || Character.toUpperCase(chars1[i]) == Character.toUpperCase(chars2[j])) {
+        i++;
+        j++;
+        // okay
+      } else if (Character.isWhitespace(chars1[i])) {
+        i++;
+        // okay, ignore space
+      } else if (Character.isWhitespace(chars2[j])) {
+        j++;
+        // try again
+      } else {
+        return false;
+      }
+    }
+
+    while (i < length1) {
+      if (Character.isWhitespace(chars1[i])) {
+        i++;
+        // okay, ignore space
+      } else {
+        return false;
+      }
+    }
+
+    while (j < length) {
+      if (Character.isWhitespace(chars2[j])) {
+        j++;
+        // okay, ignore space
+      } else {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
 }

Modified: myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java?rev=1471369&r1=1471368&r2=1471369&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java (original)
+++ myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java Wed Apr 24 11:19:20 2013
@@ -231,7 +231,7 @@ public abstract class TobagoResponseWrit
     }
 
     StringBuilder builder = new StringBuilder(contentType);
-    builder.append(";charset=");
+    builder.append("; charset=");
     builder.append(characterEncoding);
     return builder.toString();
 

Added: myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StringUtilsUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StringUtilsUnitTest.java?rev=1471369&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StringUtilsUnitTest.java (added)
+++ myfaces/tobago/branches/tobago-1.5.x/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/StringUtilsUnitTest.java Wed Apr 24 11:19:20 2013
@@ -0,0 +1,40 @@
+/*
+ * 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.myfaces.tobago.internal.util;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class StringUtilsUnitTest {
+
+  @Test
+  public void testEqualsIgnoreCaseAndWhitespace() {
+    Assert.assertTrue(StringUtils.equalsIgnoreCaseAndWhitespace(null, null));
+    Assert.assertTrue(StringUtils.equalsIgnoreCaseAndWhitespace("", ""));
+    Assert.assertTrue(StringUtils.equalsIgnoreCaseAndWhitespace("", " "));
+    Assert.assertTrue(StringUtils.equalsIgnoreCaseAndWhitespace(" ", " "));
+    Assert.assertTrue(StringUtils.equalsIgnoreCaseAndWhitespace(
+        "text/html;charset=utf-8", "  text/HTML;  charset=UTF-8  "));
+
+    Assert.assertFalse(StringUtils.equalsIgnoreCaseAndWhitespace(";", ""));
+    Assert.assertFalse(StringUtils.equalsIgnoreCaseAndWhitespace(";", ";;"));
+    Assert.assertFalse(StringUtils.equalsIgnoreCaseAndWhitespace(" a ", " ä "));
+  }
+}

Modified: myfaces/tobago/branches/tobago-1.5.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.5.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java?rev=1471369&r1=1471368&r2=1471369&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.5.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java (original)
+++ myfaces/tobago/branches/tobago-1.5.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java Wed Apr 24 11:19:20 2013
@@ -156,7 +156,7 @@ public class NavigationTree implements S
     ExternalContext externalContext = facesContext.getExternalContext();
     String viewId = facesContext.getViewRoot().getViewId();
     HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
-    response.setContentType("text/html;charset=UTF-8");
+    response.setContentType("text/html; charset=UTF-8");
 
     try {
       InputStream resourceAsStream = externalContext.getResourceAsStream(viewId);