You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by be...@apache.org on 2008/10/17 00:31:48 UTC

svn commit: r705381 - in /incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth: OAuthFetcher.java OAuthProtocolException.java OAuthUtil.java

Author: beaton
Date: Thu Oct 16 15:31:47 2008
New Revision: 705381

URL: http://svn.apache.org/viewvc?rev=705381&view=rev
Log:
Turn some annoying checked exceptions into runtime exceptions, since
a) they are unlikely to be thrown
b) we can't do anything about them
c) catch blocks are too cluttered already.


Added:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java   (with props)
Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java?rev=705381&r1=705380&r2=705381&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java Thu Oct 16 15:31:47 2008
@@ -19,6 +19,7 @@
 import org.apache.shindig.auth.SecurityToken;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.common.uri.UriBuilder;
+import org.apache.shindig.common.util.CharsetUtil;
 import org.apache.shindig.gadgets.ChainedContentFetcher;
 import org.apache.shindig.gadgets.GadgetException;
 import org.apache.shindig.gadgets.RequestSigningException;
@@ -38,9 +39,6 @@
 import net.oauth.OAuthProblemException;
 import net.oauth.OAuth.Parameter;
 
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -283,8 +281,7 @@
     }
   }
 
-  private void fetchRequestToken()
-      throws GadgetException, OAuthProtocolException {
+  private void fetchRequestToken() throws GadgetException, OAuthProtocolException {
     try {
       OAuthAccessor accessor = accessorInfo.getAccessor();
       HttpRequest request = new HttpRequest(
@@ -298,13 +295,11 @@
  
       OAuthMessage reply = sendOAuthMessage(signed);
       
-      reply.requireParameters(OAuth.OAUTH_TOKEN, OAuth.OAUTH_TOKEN_SECRET);
-      accessor.requestToken = reply.getParameter(OAuth.OAUTH_TOKEN);
-      accessor.tokenSecret = reply.getParameter(OAuth.OAUTH_TOKEN_SECRET);
+      OAuthUtil.requireParameters(reply, OAuth.OAUTH_TOKEN, OAuth.OAUTH_TOKEN_SECRET);
+      accessor.requestToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN);
+      accessor.tokenSecret = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN_SECRET);
     } catch (OAuthException e) {
       throw new UserVisibleOAuthException(e.getMessage(), e);
-    } catch (IOException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
     }
   }
 
@@ -421,23 +416,19 @@
     addSignatureParams(params);
 
     try {
-      OAuthMessage signed = accessorInfo.getAccessor().newRequestMessage(
+      OAuthMessage signed = OAuthUtil.newRequestMessage(accessorInfo.getAccessor(), 
           base.getMethod(), target.toString(), params);
       HttpRequest oauthHttpRequest = createHttpRequest(base, selectOAuthParams(signed));
       // Following 302s on OAuth responses is unlikely to be productive.
       oauthHttpRequest.setFollowRedirects(false);
       return oauthHttpRequest;
-    } catch (IOException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
     } catch (OAuthException e) {
       throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
-    } catch (URISyntaxException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
     }
   }
 
   private HttpRequest createHttpRequest(HttpRequest base,
-      List<Map.Entry<String, String>> oauthParams) throws IOException, GadgetException {
+      List<Map.Entry<String, String>> oauthParams) throws GadgetException {
 
     OAuthParamLocation paramLocation = accessorInfo.getParamLocation();
 
@@ -467,16 +458,16 @@
               "OAuth param location can only be post_body if post body if of " +
               "type x-www-form-urlencoded");
         }
-        String oauthData = OAuth.formEncode(oauthParams);
+        String oauthData = OAuthUtil.formEncode(oauthParams);
         if (result.getPostBodyLength() == 0) {
-          result.setPostBody(oauthData.getBytes("UTF-8"));
+          result.setPostBody(CharsetUtil.getUtf8Bytes(oauthData));
         } else {
           result.setPostBody((result.getPostBodyAsString() + '&' + oauthData).getBytes());
         }
         break;
 
       case URI_QUERY:
-        result.setUri(Uri.parse(OAuth.addParameters(result.getUri().toString(), oauthParams)));
+        result.setUri(Uri.parse(OAuthUtil.addParameters(result.getUri().toString(), oauthParams)));
         break;
     }
 
@@ -486,11 +477,10 @@
   /**
    * Sends OAuth request token and access token messages.
    * @throws GadgetException 
-   * @throws IOException 
    * @throws OAuthProtocolException 
    */
   private OAuthMessage sendOAuthMessage(HttpRequest request)
-      throws GadgetException, OAuthProtocolException, IOException {
+      throws GadgetException, OAuthProtocolException {
     HttpResponse response = nextFetcher.fetch(request);
     checkForProtocolProblem(response);
     OAuthMessage reply = new OAuthMessage(null, null, null);
@@ -592,13 +582,11 @@
       
       OAuthMessage reply = sendOAuthMessage(signed);
       
-      reply.requireParameters(OAuth.OAUTH_TOKEN, OAuth.OAUTH_TOKEN_SECRET);
-      accessor.accessToken = reply.getParameter(OAuth.OAUTH_TOKEN);
-      accessor.tokenSecret = reply.getParameter(OAuth.OAUTH_TOKEN_SECRET);
+      OAuthUtil.requireParameters(reply, OAuth.OAUTH_TOKEN, OAuth.OAUTH_TOKEN_SECRET);
+      accessor.accessToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN);
+      accessor.tokenSecret = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN_SECRET);
     } catch (OAuthException e) {
       throw new UserVisibleOAuthException(e.getMessage(), e);
-    } catch (IOException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
     }
   }
 
@@ -630,37 +618,28 @@
    * @throws OAuthProtocolException if the service provider returns an OAuth
    * related error instead of user data.
    */
-  private HttpResponse fetchData()
-      throws GadgetException, OAuthProtocolException {
-    try {
-      HttpRequest signed = sanitizeAndSign(realRequest, null);
-      
-      HttpResponse response = nextFetcher.fetch(signed);
-      
-      checkForProtocolProblem(response);
+  private HttpResponse fetchData() throws GadgetException, OAuthProtocolException {
+    HttpRequest signed = sanitizeAndSign(realRequest, null);
 
-      // Track metadata on the response
-      HttpResponseBuilder builder = new HttpResponseBuilder(response);
-      responseParams.addToResponse(builder);
-      return builder.create();
-    } catch (UnsupportedEncodingException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
-    } catch (IOException e) {
-      throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
-    }
+    HttpResponse response = nextFetcher.fetch(signed);
+
+    checkForProtocolProblem(response);
+
+    // Track metadata on the response
+    HttpResponseBuilder builder = new HttpResponseBuilder(response);
+    responseParams.addToResponse(builder);
+    return builder.create();
   }
 
   /**
    * Look for an OAuth protocol problem.  For cases where no access token is in play 
    * @param response
    * @throws OAuthProtocolException
-   * @throws IOException
    */
-  private void checkForProtocolProblem(HttpResponse response)
-      throws OAuthProtocolException, IOException {
+  private void checkForProtocolProblem(HttpResponse response) throws OAuthProtocolException {
     if (isFullOAuthError(response)) {
       OAuthMessage message = parseAuthHeader(null, response);
-      if (message.getParameter(OAuthProblemException.OAUTH_PROBLEM) != null) {
+      if (OAuthUtil.getParameter(message, OAuthProblemException.OAUTH_PROBLEM) != null) {
         // SP reported extended error information
         throw new OAuthProtocolException(message);
       }
@@ -706,14 +685,12 @@
    * oauth_timestamp or oauth_signature).
    *
    * @return a list that contains only the oauth_related parameters.
-   *
-   * @throws IOException
    */
   private List<Map.Entry<String, String>>
-      selectOAuthParams(OAuthMessage message) throws IOException {
+      selectOAuthParams(OAuthMessage message) {
     List<Map.Entry<String, String>> result =
         new ArrayList<Map.Entry<String, String>>();
-    for (Map.Entry<String, String> param : message.getParameters()) {
+    for (Map.Entry<String, String> param : OAuthUtil.getParameters(message)) {
       if (isContainerInjectedParameter(param.getKey())) {
         result.add(param);
       }

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java?rev=705381&r1=705380&r2=705381&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java Thu Oct 16 15:31:47 2008
@@ -22,7 +22,6 @@
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.http.HttpResponseBuilder;
 
-import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -85,14 +84,14 @@
     this.startFromScratch = false;
   }
   
-  public OAuthProtocolException(OAuthMessage reply) throws IOException {
-    String problem = reply.getParameter(OAuthProblemException.OAUTH_PROBLEM);
+  public OAuthProtocolException(OAuthMessage reply) {
+    String problem = OAuthUtil.getParameter(reply, OAuthProblemException.OAUTH_PROBLEM);
     if (problem == null) {
       throw new IllegalArgumentException(
           "No problem reported for OAuthProtocolException");
     }
     this.problemCode = problem;
-    this.problemText = reply.getParameter("oauth_problem_advice");
+    this.problemText = OAuthUtil.getParameter(reply, "oauth_problem_advice");
     if (fatalProblems.contains(problem)) {
       startFromScratch = true;
       canRetry = false;

Added: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java?rev=705381&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java Thu Oct 16 15:31:47 2008
@@ -0,0 +1,92 @@
+/*
+ * 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.oauth;
+
+import net.oauth.OAuth;
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthException;
+import net.oauth.OAuthMessage;
+import net.oauth.OAuthProblemException;
+import net.oauth.OAuth.Parameter;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map.Entry;
+
+/**
+ * Wrapper for the OAuth.net utility functions.  Some of them are declared as throwing IOException
+ * for cases that are extremely unlikely to happen.  We turn those IOExceptions in to
+ * RuntimeExceptions since the caller can't do anything about them anyway.
+ */
+public class OAuthUtil {
+
+  public static String getParameter(OAuthMessage message, String name) {
+    try {
+      return message.getParameter(name);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public static List<Entry<String, String>> getParameters(OAuthMessage message) {
+    try {
+      return message.getParameters();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  public static void requireParameters(OAuthMessage message, String... names)
+      throws OAuthProblemException {
+    try {
+      message.requireParameters(names);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  public static String formEncode(Iterable<? extends Entry<String, String>> parameters) {
+    try {
+      return OAuth.formEncode(parameters);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  public static String addParameters(String url, List<Entry<String, String>> parameters) {
+    try {
+      return OAuth.addParameters(url, parameters);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  public static OAuthMessage newRequestMessage(OAuthAccessor accessor, String method, String url,
+      List<Parameter> parameters) throws OAuthException {
+    try {
+      return accessor.newRequestMessage(method, url, parameters);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    } catch (URISyntaxException e) {
+      throw new RuntimeException(e);
+    }
+  }
+}

Propchange: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native