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

svn commit: r663803 - in /incubator/shindig/trunk/java: gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ server/src/main/webapp/WEB-INF/

Author: zhen
Date: Thu Jun  5 19:28:19 2008
New Revision: 663803

URL: http://svn.apache.org/viewvc?rev=663803&view=rev
Log:
SHINDIG-349

Added support for chained proxy request syntax.


Added:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServletRequest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletRequestTest.java
Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServlet.java
    incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml
    incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.gadgets.xml
    incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.xml

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServlet.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServlet.java?rev=663803&r1=663802&r2=663803&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServlet.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServlet.java Thu Jun  5 19:28:19 2008
@@ -20,6 +20,7 @@
 
 import org.apache.shindig.common.servlet.InjectedServlet;
 import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.servlet.ProxyServletRequest;
 
 import com.google.inject.Inject;
 
@@ -46,12 +47,15 @@
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws IOException {
-    String output = request.getParameter("output");
+    ProxyServletRequest proxyRequest = new ProxyServletRequest(request);    
+    logger.log(Level.INFO, "isUsingChainedSyntax " + (proxyRequest.isUsingChainedSyntax()? "YES": "NO"));
+    logger.log(Level.INFO, "url = " + proxyRequest.getParameter("url"));
+    String output = proxyRequest.getParameter("output");
     try {
       if ("js".equals(output)) {
-        proxyHandler.fetchJson(request, response);
+        proxyHandler.fetchJson(proxyRequest, response);
       } else {
-        proxyHandler.fetch(request, response);
+        proxyHandler.fetch(proxyRequest, response);
       }
     } catch (GadgetException ge) {
       outputError(ge, response);

Added: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServletRequest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServletRequest.java?rev=663803&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServletRequest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyServletRequest.java Thu Jun  5 19:28:19 2008
@@ -0,0 +1,74 @@
+/*
+ * 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.gadgets.servlet;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+/**
+ * A proxy request wrapper that supports chained request syntax (e.g.
+ * "http://shindig/proxy/additional=parameters/http://remotehost/file").
+ */
+public class ProxyServletRequest extends HttpServletRequestWrapper {
+  protected static Pattern chainedSyntaxPattern = Pattern.compile("^[^?]+/proxy/([^?/]*)/(.*)$");
+  protected static Pattern parameterPairPattern = Pattern.compile("([^&=]+)=([^&=]*)");
+
+  protected boolean usingChainedSyntax;
+  protected Map<String, String> extractedParameters;
+
+  public ProxyServletRequest(HttpServletRequest request) {
+    super(request);
+    Matcher chainedSyntaxMatcher = chainedSyntaxPattern.matcher(request.getRequestURI());
+    usingChainedSyntax = chainedSyntaxMatcher.matches();
+    if (usingChainedSyntax) {
+      extractedParameters = new HashMap<String, String>();
+
+      Matcher parameterPairMatcher = parameterPairPattern.matcher(chainedSyntaxMatcher.group(1));
+      while (parameterPairMatcher.find()) {
+        try {
+          extractedParameters.put(URLDecoder.decode(parameterPairMatcher.group(1), "UTF-8"),
+                                  URLDecoder.decode(parameterPairMatcher.group(2), "UTF-8"));
+        } catch (UnsupportedEncodingException e) {
+        }
+      }
+
+      extractedParameters.put(ProxyHandler.URL_PARAM, chainedSyntaxMatcher.group(2));
+    }
+  }
+
+  public boolean isUsingChainedSyntax() {
+    return usingChainedSyntax;
+  }
+
+  @Override
+  public String getParameter(String name) {
+    if (usingChainedSyntax) {
+      return extractedParameters.get(name);
+    } else {
+      return super.getParameter(name);
+    }
+  }
+}

Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletRequestTest.java?rev=663803&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletRequestTest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletRequestTest.java Thu Jun  5 19:28:19 2008
@@ -0,0 +1,74 @@
+/*
+ * 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.gadgets.servlet;
+
+import static org.easymock.EasyMock.expect;
+
+import org.apache.shindig.gadgets.EasyMockTestCase;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class ProxyServletRequestTest extends EasyMockTestCase {
+  public final HttpServletRequest request = mock(HttpServletRequest.class);
+
+  public ProxyServletRequest setupMockRequest(String url) {
+    expect(request.getRequestURI()).andReturn(url).atLeastOnce();
+    replay();
+    return new ProxyServletRequest(request);
+  }
+
+  public void testOldRequestSyntax() throws Exception {
+    ProxyServletRequest req = setupMockRequest(
+      "http://localhost/gadgets/proxy?url=http://proxy/url"
+    );
+    assertFalse(req.isUsingChainedSyntax());
+    verify();
+  }
+
+  public void testChainedSyntaxWithNoParameters() throws Exception {
+    ProxyServletRequest req = setupMockRequest(
+      "http://localhost/gadgets/proxy//http://remote/proxy?query"
+    );
+    assertTrue(req.isUsingChainedSyntax());
+    assertEquals("http://remote/proxy?query", req.getParameter("url"));
+    assertNull(req.getParameter("nocache"));
+    verify();
+  }
+
+  public void testChainedSyntaxWithOneParameter() throws Exception {
+    ProxyServletRequest req = setupMockRequest(
+      "http://localhost/gadgets/proxy/nocache=1/http://remote/proxy?nocache=0"
+    );
+    assertTrue(req.isUsingChainedSyntax());
+    assertEquals("http://remote/proxy?nocache=0", req.getParameter("url"));
+    assertEquals("1", req.getParameter("nocache"));
+    verify();
+  }
+
+  public void testChainedSyntaxWithParameters() throws Exception {
+    ProxyServletRequest req = setupMockRequest(
+      "http://u:p@127.0.0.1:80/g/proxy/a=b%20+c&url=u/http://r/p?a=d+e"
+    );
+    assertTrue(req.isUsingChainedSyntax());
+    assertEquals("http://r/p?a=d+e", req.getParameter("url"));
+    assertEquals("b  c", req.getParameter("a"));
+    verify();
+  }
+}
+

Modified: incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml?rev=663803&r1=663802&r2=663803&view=diff
==============================================================================
--- incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml (original)
+++ incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml Thu Jun  5 19:28:19 2008
@@ -98,7 +98,7 @@
 
   <servlet-mapping>
     <servlet-name>proxy</servlet-name>
-    <url-pattern>/gadgets/proxy</url-pattern>
+    <url-pattern>/gadgets/proxy/*</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>

Modified: incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.gadgets.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.gadgets.xml?rev=663803&r1=663802&r2=663803&view=diff
==============================================================================
--- incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.gadgets.xml (original)
+++ incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.gadgets.xml Thu Jun  5 19:28:19 2008
@@ -77,7 +77,7 @@
 
   <servlet-mapping>
     <servlet-name>proxy</servlet-name>
-    <url-pattern>/gadgets/proxy</url-pattern>
+    <url-pattern>/gadgets/proxy/*</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>

Modified: incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.xml?rev=663803&r1=663802&r2=663803&view=diff
==============================================================================
--- incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.xml (original)
+++ incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.xml Thu Jun  5 19:28:19 2008
@@ -98,7 +98,7 @@
 
   <servlet-mapping>
     <servlet-name>proxy</servlet-name>
-    <url-pattern>/gadgets/proxy</url-pattern>
+    <url-pattern>/gadgets/proxy/*</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>