You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by li...@apache.org on 2010/01/29 03:22:13 UTC

svn commit: r904351 - /incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java

Author: lindner
Date: Fri Jan 29 02:22:13 2010
New Revision: 904351

URL: http://svn.apache.org/viewvc?rev=904351&view=rev
Log:
Patch from zhoresh [http://codereview.appspot.com/186278]

Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java?rev=904351&r1=904350&r2=904351&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java Fri Jan 29 02:22:13 2010
@@ -31,6 +31,7 @@
 import org.apache.shindig.gadgets.FeedProcessor;
 import org.apache.shindig.gadgets.FetchResponseUtils;
 import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.GadgetException.Code;
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.http.RequestPipeline;
@@ -39,6 +40,7 @@
 import org.apache.shindig.gadgets.rewrite.RewritingException;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.util.Collections;
 import java.util.Map;
 
@@ -112,18 +114,14 @@
    * @throws GadgetException
    */
   protected HttpRequest buildHttpRequest(HttpServletRequest request) throws GadgetException {
-    String encoding = request.getCharacterEncoding();
-    if (encoding == null) {
-      encoding = "UTF-8";
-    }
-
     Uri url = validateUrl(request.getParameter(URL_PARAM));
 
     HttpRequest req = new HttpRequest(url)
         .setMethod(getParameter(request, METHOD_PARAM, "GET"))
-        .setPostBody(getParameter(request, POST_DATA_PARAM, "").getBytes())
         .setContainer(getContainer(request));
 
+    setPostData(request,req);
+    
     String headerData = getParameter(request, HEADERS_PARAM, "");
     if (headerData.length() > 0) {
       String[] headerList = StringUtils.split(headerData, '&');
@@ -179,6 +177,25 @@
   }
 
   /**
+   * Set http request post data according to servlet request.
+   * It uses header encoding if available, and defaulted to utf8
+   * Override the function if different behavior is needed.
+   */
+  protected void setPostData(HttpServletRequest request, HttpRequest req)
+      throws GadgetException {
+    String encoding = request.getCharacterEncoding();
+    if (encoding == null) {
+      encoding = "UTF-8";
+    }
+    try {
+      req.setPostBody(getParameter(request, POST_DATA_PARAM, "")
+          .getBytes(encoding.toUpperCase()));
+    } catch (UnsupportedEncodingException e) {
+      throw new GadgetException(Code.HTML_PARSE_ERROR, e);
+    }    
+  }
+
+  /**
    * Format a response as JSON, including additional JSON inserted by
    * chained content fetchers.
    */