Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import static com.emobus.stuff.LoggerConstants.facebookUserId; import static com.emobus.stuff.LoggerConstants.ipAddress; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.w3c.dom.Document; import com.google.code.facebookapi.FacebookException; import com.google.code.facebookapi.FacebookWebappHelper; import com.google.code.facebookapi.FacebookXmlRestClient; import com.google.code.facebookapi.IFacebookRestClient; /** * The Facebook User Filter ensures that a Facebook client that pertains to * the logged in user is available in the session object named "facebook.user.client". * * The session ID is stored as "facebook.user.session". It's important to get * the session ID only when the application actually needs it. The user has to * authorise to give the application a session key. * * @author Dave */ public class FacebookUserFilter implements Filter { private static final Logger logger = LoggerFactory.getLogger(FacebookUserFilter.class); private String api_key; private String secret; private static final String FACEBOOK_USER_CLIENT = "facebook.user.client"; public void init(FilterConfig filterConfig) throws ServletException { api_key = filterConfig.getServletContext().getInitParameter("facebook_api_key"); secret = filterConfig.getServletContext().getInitParameter("facebook_secret"); if(api_key == null || secret == null) { throw new ServletException("Cannot initialise Facebook User Filter because the " + "facebook_api_key or facebook_secret context init " + "params have not been set. Check that they're there " + "in your servlet context descriptor."); } else { logger.info("Using facebook API key: " + api_key); } } public void destroy() { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { MDC.put(ipAddress, req.getRemoteAddr()); HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; HttpSession session = request.getSession(true); IFacebookRestClient<Document> userClient = getUserClient(session); if(userClient == null) { logger.debug("User session doesn't have a Facebook API client setup yet. Creating one and storing it in the user's session."); userClient = new FacebookXmlRestClient(api_key, secret); session.setAttribute(FACEBOOK_USER_CLIENT, userClient); } logger.trace("Creating a FacebookWebappHelper, which copies fb_ request param data into the userClient"); FacebookWebappHelper<Document> facebook = new FacebookWebappHelper<Document>(request, response, api_key, secret, userClient); String nextPage = request.getRequestURI(); nextPage = nextPage.substring(nextPage.indexOf("/", 1) + 1); //cut out the first /, the context path and the 2nd / logger.trace(nextPage); boolean redirectOccurred = facebook.requireLogin(nextPage); if(redirectOccurred) { return; } redirectOccurred = facebook.requireFrame(nextPage); if(redirectOccurred) { return; } long facebookUserID; try { facebookUserID = userClient.users_getLoggedInUser(); } catch(FacebookException ex) { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error while fetching user's facebook ID"); logger.error("Error while getting cached (supplied by request params) value " + "of the user's facebook ID or while fetching it from the Facebook service " + "if the cached value was not present for some reason. Cached value = {}", userClient.getCacheUserId()); return; } MDC.put(facebookUserId, String.valueOf(facebookUserID)); chain.doFilter(request, response); } finally { MDC.remove(ipAddress); MDC.remove(facebookUserId); } } public static FacebookXmlRestClient getUserClient(HttpSession session) { return (FacebookXmlRestClient)session.getAttribute(FACEBOOK_USER_CLIENT); } } public class SetUsersStatus if (client.users_hasAppPermission(Permission.STATUS_UPDATE)) { client.users_setStatus("developing Facebook apps in Java because the new Java client kicks the PHP client's ... !", false); } } |
This post has been edited 1 times, last edit by "JasonS" (Nov 19th 2011, 12:33am)
Can't find the needed one.Quoted
Jabaco Source
1 2 import static com.emobus.stuff.LoggerConstants.facebookUserId; import static com.emobus.stuff.LoggerConstants.ipAddress;
Thats not part of the JRE or JDK. It is part of JavaEE. And with this I have no experiences. I think there existing different implementations (JBoss, Glassfish, Geronimo, ...). Don't know if I need an special JEE or can choose anyone of it.Quoted
Jabaco Source
1 2 3 4 5 6 7 8 9 import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
This can be downloaded fromQuoted
Jabaco Source
1 2 3 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC;
And this can be found atQuoted
Jabaco Source
1 2 3 4 import com.google.code.facebookapi.FacebookException; import com.google.code.facebookapi.FacebookWebappHelper; import com.google.code.facebookapi.FacebookXmlRestClient; import com.google.code.facebookapi.IFacebookRestClient;
This post has been edited 1 times, last edit by "theuserbl" (Nov 20th 2011, 5:51pm)