r6907 - in dumbhippo/trunk/server: src/com/dumbhippo/persistence src/com/dumbhippo/server src/com/dumbhippo/server/impl src/com/dumbhippo/web/servlets web web/jsp3



Author: marinaz
Date: 2007-11-13 16:43:18 -0600 (Tue, 13 Nov 2007)
New Revision: 6907

Added:
   dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookAddServlet.java
   dumbhippo/trunk/server/web/jsp3/facebook-welcome.jsp
Removed:
   dumbhippo/trunk/server/web/jsp3/facebook-add.jsp
Modified:
   dumbhippo/trunk/server/src/com/dumbhippo/persistence/FacebookAccount.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/FacebookTracker.java
   dumbhippo/trunk/server/src/com/dumbhippo/server/impl/FacebookTrackerBean.java
   dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookVerifyServlet.java
   dumbhippo/trunk/server/web/servlet-info.xml
Log:
Add a servlet that verifies adding a Facebook application.

Add a new flag to FacebookAccount that indicates whether a Facebook application is enabled.

Modified: dumbhippo/trunk/server/src/com/dumbhippo/persistence/FacebookAccount.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/persistence/FacebookAccount.java	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/src/com/dumbhippo/persistence/FacebookAccount.java	2007-11-13 22:43:18 UTC (rev 6907)
@@ -36,6 +36,7 @@
 	private Set<FacebookAlbumData> albums;
 	// with this timestamp, we don't need to have an albumsPrimed flag
 	private long albumsModifiedTimestamp;
+	private boolean applicationEnabled;
 	
 	protected FacebookAccount() {}
 	
@@ -57,6 +58,7 @@
 		this.taggedPhotosPrimed = false;
 		this.albums = new HashSet<FacebookAlbumData>();	
 		this.albumsModifiedTimestamp = -1;
+		this.applicationEnabled = false;
 	}
 	
 	@Column(nullable=true)
@@ -278,6 +280,15 @@
 		this.albumsModifiedTimestamp = albumsModifiedTimestamp;
 	}
 
+	@Column(nullable=false)
+	public boolean isApplicationEnabled() {
+		return applicationEnabled;
+	}
+
+	public void setApplicationEnabled(boolean applicationEnabled) {
+		this.applicationEnabled = applicationEnabled;
+	}
+	
 	@Transient
 	public FacebookEvent getRecyclableEvent(FacebookEventType eventType) {
 	    for (FacebookEvent event : getFacebookEvents()) {

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/FacebookTracker.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/FacebookTracker.java	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/FacebookTracker.java	2007-11-13 22:43:18 UTC (rev 6907)
@@ -13,6 +13,8 @@
 
 	public void updateOrCreateFacebookAccount(UserViewpoint viewpoint, String facebookAuthToken) throws FacebookSystemException;	
 	
+	public void updateOrCreateFacebookAccount(UserViewpoint viewpoint, String sessionKey, String facebookUserId, boolean applicationEnabled) throws FacebookSystemException;
+
 	public void updateMessageCount(long facebookAccountId);
 	
 	public void updateTaggedPhotos(long facebookAccountId);

Modified: dumbhippo/trunk/server/src/com/dumbhippo/server/impl/FacebookTrackerBean.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/server/impl/FacebookTrackerBean.java	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/src/com/dumbhippo/server/impl/FacebookTrackerBean.java	2007-11-13 22:43:18 UTC (rev 6907)
@@ -98,6 +98,37 @@
 		    notifier.onFacebookEvent(facebookAccount.getExternalAccount().getAccount().getOwner(), loginStatusEvent);
 	}
 
+	public void updateOrCreateFacebookAccount(UserViewpoint viewpoint, String sessionKey, String facebookUserId, boolean applicationEnabled) throws FacebookSystemException {
+		ExternalAccount externalAccount = externalAccounts.getOrCreateExternalAccount(viewpoint, ExternalAccountType.FACEBOOK);
+		
+		FacebookAccount facebookAccount;
+		if (externalAccount.getExtra() == null) {
+		    facebookAccount = new FacebookAccount(externalAccount);
+		    em.persist(facebookAccount);
+		    externalAccount.setExtra(Long.toString(facebookAccount.getId()));
+		    externalAccounts.setSentiment(externalAccount, Sentiment.LOVE);
+		} else {
+			facebookAccount = em.find(FacebookAccount.class, Long.parseLong(externalAccount.getExtra()));
+			if (facebookAccount == null)
+				throw new RuntimeException("Invalid FacebookAccount id " + externalAccount.getExtra() + " is stored in externalAccount " + externalAccount);
+		}
+		
+		if (facebookAccount.getFacebookUserId() != null 
+			&& !facebookAccount.getFacebookUserId().equals(facebookUserId)) {
+				throw new FacebookSystemException("We do not support changing your Facebook account yet.");
+	    }
+			
+	    facebookAccount.setSessionKey(sessionKey);
+		facebookAccount.setFacebookUserId(facebookUserId);
+	    if (sessionKey != null)
+		    facebookAccount.setSessionKeyValid(true);	
+	    facebookAccount.setApplicationEnabled(applicationEnabled);
+			
+		FacebookEvent loginStatusEvent = getLoginStatusEvent(facebookAccount, true);
+		if (loginStatusEvent != null)  
+		    notifier.onFacebookEvent(facebookAccount.getExternalAccount().getAccount().getOwner(), loginStatusEvent);
+	}
+	
 	// FIXME this is calling web services with a transaction open, which holds 
 	// a db connection open so other threads can't use it, and could also 
 	// time out the transaction

Added: dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookAddServlet.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookAddServlet.java	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookAddServlet.java	2007-11-13 22:43:18 UTC (rev 6907)
@@ -0,0 +1,107 @@
+package com.dumbhippo.web.servlets;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+
+import com.facebook.api.FacebookParam;
+import com.facebook.api.FacebookSignatureUtil;
+
+import com.dumbhippo.GlobalSetup;
+import com.dumbhippo.server.Configuration;
+import com.dumbhippo.server.FacebookSystemException;
+import com.dumbhippo.server.FacebookTracker;
+import com.dumbhippo.server.HippoProperty;
+import com.dumbhippo.server.HumanVisibleException;
+import com.dumbhippo.server.Configuration.PropertyNotFoundException;
+import com.dumbhippo.server.applications.ApplicationSystem;
+import com.dumbhippo.tx.RetryException;
+import com.dumbhippo.web.SigninBean;
+import com.dumbhippo.web.UserSigninBean;
+import com.dumbhippo.web.WebEJBUtil;
+
+public class FacebookAddServlet extends AbstractServlet {
+
+	@SuppressWarnings("unused")
+	private static final Logger logger = GlobalSetup.getLogger(FacebookAddServlet.class);
+	
+	static final long serialVersionUID = 1;
+	
+	private Configuration config;
+	
+	@Override
+	public void init() {
+		config = WebEJBUtil.defaultLookup(Configuration.class);
+	}	
+	
+	@Override
+	protected String wrappedDoGet(HttpServletRequest request, HttpServletResponse response) throws IOException, HumanVisibleException, HttpException, ServletException, RetryException {
+		String redirectUrl = "/facebook-welcome";
+		logger.debug("context params are:");
+        for (Object o : request.getParameterMap().entrySet()) {
+            Map.Entry<String, String[]> mapEntry = (Map.Entry<String, String[]>)o;
+            logger.debug("{} = {}", mapEntry.getKey(), mapEntry.getValue()[0]);
+        }
+      
+        Map<String, CharSequence> facebookParams = FacebookSignatureUtil.extractFacebookParamsFromArray(request.getParameterMap());
+        String secret = null;
+        try {
+        	secret = config.getPropertyNoDefault(HippoProperty.FACEBOOK_SECRET).trim();
+			if (secret.length() == 0)
+				secret = null;				
+		} catch (PropertyNotFoundException e) {
+			secret = null;
+		}
+		
+		if (secret == null) {
+			String errorMessage = "We could not verify Facebook information due to a missing secret key we should share with Facebook.";   
+			redirectUrl = redirectUrl + "?error_message=" + errorMessage;
+			logger.warn("Facebook secret is not set, can't verify new  additions of the Facebook application.");
+		} else {        
+	        boolean signatureValid = FacebookSignatureUtil.verifySignature(facebookParams, secret);
+	        if (!signatureValid) {
+				String errorMessage = "We could not verify Facebook information because the signature supplied for Facebook parameters was not valid.";   
+				redirectUrl = redirectUrl + "?error_message=" + errorMessage;	        	
+	        } else {
+	            FacebookTracker facebookTracker = WebEJBUtil.defaultLookup(FacebookTracker.class);
+			    SigninBean signin = SigninBean.getForRequest(request);	
+			    if (signin instanceof UserSigninBean) {
+	    	        try {
+	    	            // set the session key and the user id for the signed in user, set their facebookApplicationEnabled flag to true  
+	    	            String sessionKey = facebookParams.get(FacebookParam.SESSION_KEY.toString()).toString();
+	    	            String facebookUserId = facebookParams.get(FacebookParam.USER.toString()).toString(); 
+	    	            facebookTracker.updateOrCreateFacebookAccount(((UserSigninBean)signin).getViewpoint(), sessionKey, facebookUserId, true);
+	    	        } catch (FacebookSystemException e) {
+	                    redirectUrl = redirectUrl + "?error_message=" + e.getMessage();		
+	    	        }
+		        }
+	        }
+		}
+		response.sendRedirect(redirectUrl);
+		return null;
+	}	
+		
+	@Override
+	protected boolean isReadWrite(HttpServletRequest request) {
+		// The method is GET, since we need links that the user can just click upon,
+		// but they have side effects. This is OK since the links are unique, so 
+		// caching won't happen.
+		
+		return true;
+	}
+	
+
+	@Override
+	protected boolean requiresTransaction(HttpServletRequest request) {
+		return true;
+	}
+}

Modified: dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookVerifyServlet.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookVerifyServlet.java	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/src/com/dumbhippo/web/servlets/FacebookVerifyServlet.java	2007-11-13 22:43:18 UTC (rev 6907)
@@ -21,7 +21,7 @@
 public class FacebookVerifyServlet extends AbstractServlet {
 
 	@SuppressWarnings("unused")
-	private static final Logger logger = GlobalSetup.getLogger(VerifyServlet.class);
+	private static final Logger logger = GlobalSetup.getLogger(FacebookVerifyServlet.class);
 	
 	static final long serialVersionUID = 1;
 	

Deleted: dumbhippo/trunk/server/web/jsp3/facebook-add.jsp
===================================================================
--- dumbhippo/trunk/server/web/jsp3/facebook-add.jsp	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/web/jsp3/facebook-add.jsp	2007-11-13 22:43:18 UTC (rev 6907)
@@ -1,30 +0,0 @@
-<html>
-<%@ page pageEncoding="UTF-8" %>
-<%@ taglib uri="http://java.sun.com/jsp/jstl/core"; prefix="c" %>
-<%@ taglib uri="/jsp/dumbhippo.tld" prefix="dh" %>
-<%@ taglib tagdir="/WEB-INF/tags/2" prefix="dht" %>
-<%@ taglib tagdir="/WEB-INF/tags/3" prefix="dht3" %>
-
-<%-- <dh:bean id="facebookAdd" class="com.dumbhippo.web.pages.FacebookAddPage" scope="page"/> --%>
-
-<c:set var="pageName" value="Mugshot Verification for Facebook" scope="page"/>
-    
-<head>
-    <title><c:out value="${pageName}"/></title>
-	<dht3:stylesheet name="site" iefixes="true"/>			
-	<dht:faviconIncludes/>
-</head>
-<dht3:page currentPageLink="facebook-add">
-    <dht3:shinyBox color="grey">
-        <div class="dh-page-shinybox-title-large">Thank you for trying out Mugshot application for Facebook!</div>
-        <c:choose>
-            <c:when test="${!signin.valid}">
-                <a href="/who-are-you">Log In</a> or <a href="/signup">Sign Up</a>, then try verifying your Mugshot account again on Facebook.   
-            </c:when>
-            <c:otherwise>
-                You are all set! Check your <a href="/account">Mugshot account page</a> to make sure you've added all the services you use. 
-            </c:otherwise>
-        </c:choose>    
-    </dht3:shinyBox>
-</dht3:page>		
-</html>
\ No newline at end of file

Copied: dumbhippo/trunk/server/web/jsp3/facebook-welcome.jsp (from rev 6896, dumbhippo/trunk/server/web/jsp3/facebook-add.jsp)
===================================================================
--- dumbhippo/trunk/server/web/jsp3/facebook-add.jsp	2007-11-12 21:09:43 UTC (rev 6896)
+++ dumbhippo/trunk/server/web/jsp3/facebook-welcome.jsp	2007-11-13 22:43:18 UTC (rev 6907)
@@ -0,0 +1,31 @@
+<html>
+<%@ page pageEncoding="UTF-8" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core"; prefix="c" %>
+<%@ taglib uri="/jsp/dumbhippo.tld" prefix="dh" %>
+<%@ taglib tagdir="/WEB-INF/tags/2" prefix="dht" %>
+<%@ taglib tagdir="/WEB-INF/tags/3" prefix="dht3" %>
+
+<c:set var="pageName" value="Mugshot Verification for Facebook" scope="page"/>
+    
+<head>
+    <title><c:out value="${pageName}"/></title>
+	<dht3:stylesheet name="site" iefixes="true"/>			
+	<dht:faviconIncludes/>
+</head>
+<dht3:page currentPageLink="facebook-add">
+    <dht3:shinyBox color="grey">
+        <div class="dh-page-shinybox-title-large">Thank you for trying out Mugshot application for Facebook!</div>
+        <c:choose>
+            <c:when test="${!empty param['error_message']}">
+                <c:out value="${param['error_message']}"/>
+            </c:when> 
+            <c:when test="${!signin.valid}">
+                <a href="/who-are-you">Log In</a> or <a href="/signup">Sign Up</a>, then try verifying your Mugshot account again on Facebook.   
+            </c:when>
+            <c:otherwise>
+                You are all set! Check your <a href="/account">Mugshot account page</a> to make sure you've added all the services you use. 
+            </c:otherwise>
+        </c:choose>    
+    </dht3:shinyBox>
+</dht3:page>		
+</html>
\ No newline at end of file

Modified: dumbhippo/trunk/server/web/servlet-info.xml
===================================================================
--- dumbhippo/trunk/server/web/servlet-info.xml	2007-11-13 22:17:26 UTC (rev 6906)
+++ dumbhippo/trunk/server/web/servlet-info.xml	2007-11-13 22:43:18 UTC (rev 6907)
@@ -268,8 +268,18 @@
       <servlet-name>FacebookVerifyFromHomeServlet</servlet-name>
       <url-pattern>/facebook-verify-from-home</url-pattern>
    </servlet-mapping>
+
+    <servlet>
+      <servlet-name>FacebookAddServlet</servlet-name>
+      <servlet-class>com.dumbhippo.web.servlets.FacebookAddServlet</servlet-class>
+    </servlet> 
    
    <servlet-mapping>
+      <servlet-name>FacebookAddServlet</servlet-name>
+      <url-pattern>/facebook-add</url-pattern>
+   </servlet-mapping>   
+   
+   <servlet-mapping>
       <servlet-name>AbnormalErrorServlet</servlet-name>
       <url-pattern>/abnormalerror</url-pattern>
    </servlet-mapping>
@@ -299,7 +309,7 @@
            they are listed below for completeness -->
       <init-param>
           <param-name>noSignin</param-name>
-          <param-value>apple-dashboard-content,application,applications,applications-learnmore,badges,busy,buttons,comingsoon,error,facebook,facebook-add,files,flash,google-stacker,google-stacker-spec,google-stacker-content,group-files,licenses,mugshot-eula,public-groups,radar-theme,sharelink,sharelink-inactive,signup,summit,tour,unknownlink,upgrade,user-summary,who-are-you,robots.txt,trademark</param-value>
+          <param-value>apple-dashboard-content,application,applications,applications-learnmore,badges,busy,buttons,comingsoon,error,facebook,facebook-welcome,files,flash,google-stacker,google-stacker-spec,google-stacker-content,group-files,licenses,mugshot-eula,public-groups,radar-theme,sharelink,sharelink-inactive,signup,summit,tour,unknownlink,upgrade,user-summary,who-are-you,robots.txt,trademark</param-value>
       </init-param>
       <init-param>
           <param-name>requiresSigninStealth</param-name>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]