Author: johnh
Date: Fri Mar 20 22:19:16 2009
New Revision: 756784

URL: http://svn.apache.org/viewvc?rev=756784&view=rev
Log:
Refactoring making it possible to override default Uri parsing behavior. This 
allows implementations to choose to accept URIs with technically (per RFC 2396) 
illegal characters in them, for instance (^, |, and others) that are 
nevertheless accepted in many contexts.

Those choosing to override this behavior can choose static injection or simply 
call Uri.setUriParser(UriParser) depending on the preferred syntax. By default, 
the existing implementation (now in DefaultUriParser) is used. Static injection 
is used due to the large number of Uri.parse(...) callers at the moment.

A logical extension of this work is to clean up all direct uses of URI in the 
Shindig code base and replace them with Uri to avoid potential inconsistencies. 
As well, Uri.resolve(Uri) should be implemented manually to better support 
consistent leniency.



Added:
    
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/DefaultUriParser.java
    
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriParser.java
Modified:
    
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java

Added: 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/DefaultUriParser.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/DefaultUriParser.java?rev=756784&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/DefaultUriParser.java
 (added)
+++ 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/DefaultUriParser.java
 Fri Mar 20 22:19:16 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.uri;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+/**
+ * Uri parser using java.net.URI as its basis, enforcing RFC 2396 restrictions.
+ */
+public class DefaultUriParser implements UriParser {
+  /**
+   * Produces a new Uri from a text representation.
+   *
+   * @param text The text uri.
+   * @return A new Uri, parsed into components.
+   */
+  public Uri parse(String text) {
+    try {
+      return Uri.fromJavaUri(new URI(text));
+    } catch (URISyntaxException e) {
+      throw new IllegalArgumentException(e);
+    }
+  }
+}

Modified: 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java?rev=756784&r1=756783&r2=756784&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
 (original)
+++ 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
 Fri Mar 20 22:19:16 2009
@@ -20,6 +20,7 @@
 
 import com.google.common.base.Objects;
 import com.google.common.collect.Maps;
+import com.google.inject.Inject;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -44,6 +45,12 @@
 
   private final Map<String, List<String>> queryParameters;
 
+  private static UriParser parser = new DefaultUriParser();
+
+  @Inject(optional = true)
+  public static void setUriParser(UriParser uriParser) {
+    parser = uriParser;
+  }
 
   Uri(UriBuilder builder) {
     scheme = builder.getScheme();
@@ -81,11 +88,7 @@
    * @return A new Uri, parsed into components.
    */
   public static Uri parse(String text) {
-    try {
-      return fromJavaUri(new URI(text));
-    } catch (URISyntaxException e) {
-      throw new IllegalArgumentException(e);
-    }
+    return parser.parse(text);
   }
 
   /**

Added: 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriParser.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriParser.java?rev=756784&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriParser.java
 (added)
+++ 
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriParser.java
 Fri Mar 20 22:19:16 2009
@@ -0,0 +1,32 @@
+/*
+ * 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.uri;
+
+/**
+ * An injectable interface for parsing Uris out of String text.
+ */
+public interface UriParser {
+  /**
+   * Produces a new Uri from a text representation.
+   *
+   * @param text The text uri.
+   * @return A new Uri, parsed into components.
+   */
+  public Uri parse(String text);
+}


Reply via email to