You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/05/20 10:10:58 UTC

svn commit: r658152 - in /incubator/shindig/trunk/java/common/src: main/java/org/apache/shindig/common/util/Utf8UrlCoder.java test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java

Author: etnu
Date: Tue May 20 01:10:58 2008
New Revision: 658152

URL: http://svn.apache.org/viewvc?rev=658152&view=rev
Log:
Added Utf8UrlCoder, a utility wrapper around URLEncoder / decoder that handlers boilerplate UTF-8 encoding exceptions.


Added:
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/Utf8UrlCoder.java
    incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java

Added: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/Utf8UrlCoder.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/Utf8UrlCoder.java?rev=658152&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/Utf8UrlCoder.java (added)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/Utf8UrlCoder.java Tue May 20 01:10:58 2008
@@ -0,0 +1,47 @@
+/*
+ * 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.shindig.common.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+
+/**
+ * Performs url encoding / decoding with forced utf-8. Automatically takes care
+ * of boilerplate exception handling.
+ */
+public class Utf8UrlCoder {
+
+  public static String encode(String input) {
+    try {
+      return URLEncoder.encode(input, "UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public static String decode(String input) {
+    try {
+      return URLDecoder.decode(input, "UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+}

Added: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java?rev=658152&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java (added)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/Utf8UrlCoderTest.java Tue May 20 01:10:58 2008
@@ -0,0 +1,50 @@
+/*
+ * 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.shindig.common.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class Utf8UrlCoderTest {
+  private static final String RAW_SIMPLE = "Hello, world!";
+  private static final String ENCODED_SIMPLE = "Hello%2C+world%21";
+  private static final String RAW_COMPLEX = "\u4F60\u597D";
+  private static final String ENCODED_COMPLEX = "%E4%BD%A0%E5%A5%BD";
+
+  @Test
+  public void encodeSimple() {
+    assertEquals(ENCODED_SIMPLE, Utf8UrlCoder.encode(RAW_SIMPLE));
+  }
+
+  @Test
+  public void decodeSimple() {
+    assertEquals(RAW_SIMPLE, Utf8UrlCoder.decode(ENCODED_SIMPLE));
+  }
+
+  @Test
+  public void encodeComplex() {
+    assertEquals(ENCODED_COMPLEX, Utf8UrlCoder.encode(RAW_COMPLEX));
+  }
+
+  @Test
+  public void decodeComplex() {
+    assertEquals(RAW_COMPLEX, Utf8UrlCoder.decode(ENCODED_COMPLEX));
+  }
+}