Author: beaton
Date: Thu Oct 16 16:23:54 2008
New Revision: 705402

URL: http://svn.apache.org/viewvc?rev=705402&view=rev
Log:
More logging for OAuth service provider reported errors.


Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpRequest.java
    
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/testing/FakeOAuthServiceProvider.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpRequest.java?rev=705402&r1=705401&r2=705402&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpRequest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpRequest.java
 Thu Oct 16 16:23:54 2008
@@ -417,7 +417,7 @@
   public String toString() {
     StringBuilder buf = new StringBuilder(method);
     buf.append(' ').append(uri.getPath())
-       .append(uri.getQuery() == null ? "" : uri.getQuery()).append("\n\n");
+       .append(uri.getQuery() == null ? "" : "?" + 
uri.getQuery()).append("\n\n");
     buf.append("Host: ").append(uri.getAuthority()).append('\n');
     buf.append("X-Shindig-AuthType: ").append(authType).append('\n');
     for (Map.Entry<String, List<String>> entry : headers.entrySet()) {

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=705402&r1=705401&r2=705402&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 16:23:54 2008
@@ -295,7 +295,6 @@
  
       OAuthMessage reply = sendOAuthMessage(signed);
       
-      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) {
@@ -476,18 +475,25 @@
 
   /**
    * Sends OAuth request token and access token messages.
-   * @throws GadgetException 
-   * @throws OAuthProtocolException 
    */
   private OAuthMessage sendOAuthMessage(HttpRequest request)
-      throws GadgetException, OAuthProtocolException {
+      throws GadgetException, OAuthProtocolException, OAuthProblemException {
     HttpResponse response = nextFetcher.fetch(request);
-    checkForProtocolProblem(response);
-    OAuthMessage reply = new OAuthMessage(null, null, null);
+    boolean done = false;
+    try {
+      checkForProtocolProblem(response);
+      OAuthMessage reply = new OAuthMessage(null, null, null);
 
-    reply.addParameters(OAuth.decodeForm(response.getResponseAsString()));
-    reply = parseAuthHeader(reply, response);
-    return reply;
+      reply.addParameters(OAuth.decodeForm(response.getResponseAsString()));
+      reply = parseAuthHeader(reply, response);
+      OAuthUtil.requireParameters(reply, OAuth.OAUTH_TOKEN, 
OAuth.OAUTH_TOKEN_SECRET);
+      done = true;
+      return reply;
+    } finally {
+      if (!done) {
+        logServiceProviderError(request, response);
+      }
+    }
   }
 
   /**
@@ -582,7 +588,6 @@
       
       OAuthMessage reply = sendOAuthMessage(signed);
       
-      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) {
@@ -623,7 +628,12 @@
 
     HttpResponse response = nextFetcher.fetch(signed);
 
-    checkForProtocolProblem(response);
+    try {
+      checkForProtocolProblem(response);
+    } catch (OAuthProtocolException e) {
+      logServiceProviderError(signed, response);
+      throw e;
+    }
 
     // Track metadata on the response
     HttpResponseBuilder builder = new HttpResponseBuilder(response);
@@ -702,4 +712,10 @@
     key = key.toLowerCase();
     return key.startsWith("oauth") || key.startsWith("xoauth") || 
key.startsWith("opensocial");
   }
+  
+  
+  /** Logging for errors that service providers return to us, useful for 
integration problems */
+  private void logServiceProviderError(HttpRequest request, HttpResponse 
response) {
+    logger.log(Level.INFO, "OAuth request failed:\n" + request + 
"\nresponse:\n" + response);
+  }
 }

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java?rev=705402&r1=705401&r2=705402&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
 Thu Oct 16 16:23:54 2008
@@ -254,7 +254,10 @@
       if ("consumer_key_unknown".equals(code)) {
         rc = HttpResponse.SC_FORBIDDEN;
       }
-      return new HttpResponseBuilder().setHttpStatusCode(rc).create();
+      return new HttpResponseBuilder()
+          .setHttpStatusCode(rc)
+          .setResponseString("some vague error")
+          .create();
     }
     OAuthMessage msg = new OAuthMessage(null, null, null);
     msg.addParameter("oauth_problem", code);

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java?rev=705402&r1=705401&r2=705402&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
 Thu Oct 16 16:23:54 2008
@@ -24,6 +24,8 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import com.google.common.collect.Lists;
+
 import net.oauth.OAuth;
 import net.oauth.OAuth.Parameter;
 
@@ -51,6 +53,9 @@
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
 
 /**
  * Tests for signing requests.
@@ -60,6 +65,7 @@
   private OAuthFetcherConfig fetcherConfig;
   private FakeOAuthServiceProvider serviceProvider;
   private BasicOAuthStore base;
+  private List<LogRecord> logRecords = Lists.newArrayList();
 
   public static final String GADGET_URL = "http://www.example.com/gadget.xml";;
   public static final String GADGET_URL_NO_KEY = 
"http://www.example.com/nokey.xml";;
@@ -74,6 +80,22 @@
         new BasicBlobCrypter("abcdefghijklmnop".getBytes()),
         getOAuthStore(base),
         new BasicHttpCache(new DefaultCacheProvider(),10));
+    
+    Logger logger = Logger.getLogger(OAuthFetcher.class.getName());
+    logger.addHandler(new Handler() {
+      @Override
+      public void close() throws SecurityException {
+      }
+
+      @Override
+      public void flush() {
+      }
+
+      @Override
+      public void publish(LogRecord arg0) {
+        logRecords.add(arg0);
+      }
+    });
   }
 
   /**
@@ -223,6 +245,7 @@
     
     response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL);
     assertEquals("User data is hello-oauth", response.getResponseAsString());
+    checkEmptyLog();
   }
   
   @Test
@@ -381,6 +404,8 @@
     serviceProvider.revokeAllAccessTokens();
     
     response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL + 
"?cachebust=2");
+    checkLogContains("GET /data?cachebust=2");
+    checkLogContains("HTTP/1.1 401");
     assertEquals("", response.getResponseAsString());
     assertNotNull(response.getMetadata().get("oauthApprovalUrl"));
     
@@ -420,6 +445,9 @@
     assertNotNull(metadata);
     assertEquals("403", metadata.get("oauthError"));
     assertNull(metadata.get("oauthErrorText"));
+    checkLogContains("HTTP/1.1 403");
+    checkLogContains("GET /request");
+    checkLogContains("some vague error");
   }
   
   @Test
@@ -903,4 +931,23 @@
     }
     return false;
   }
-}
+
+  private String getLogText() {
+    StringBuilder logText = new StringBuilder();
+    for (LogRecord record : logRecords) {
+      logText.append(record.getMessage());
+    }
+    return logText.toString();
+  }
+  
+  private void checkLogContains(String text) {
+    String logText = getLogText();
+    if (!logText.contains(text)) {
+      fail("Should have logged '" + text + "', instead got " + logText);
+    }
+  }
+  
+  private void checkEmptyLog() {
+    assertEquals("", getLogText());
+  }
+} 


Reply via email to