Browse files

Adding a set user filter

  • Loading branch information...
1 parent e942dba commit 29e0c5e304848c04a0cb5dc4f5429a5990074247 @ikai committed Jun 2, 2011
Showing with 113 additions and 42 deletions.
  1. +57 −0 src/com/ikai/protodemo/SetUserFilter.java
  2. +56 −42 war/WEB-INF/web.xml
View
57 src/com/ikai/protodemo/SetUserFilter.java
@@ -0,0 +1,57 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+
+package com.ikai.protodemo;
+
+import com.google.appengine.api.users.User;
+import com.google.appengine.api.users.UserService;
+import com.google.appengine.api.users.UserServiceFactory;
+
+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;
+
+/**
+ * @author ikai@google.com (Ikai Lan)
+ *
+ */
+public class SetUserFilter implements Filter {
+
+ @Override
+ public void doFilter(ServletRequest req, ServletResponse resp,
+ FilterChain chain) throws ServletException, IOException {
+ HttpServletRequest request = (HttpServletRequest) req;
+ UserService userService = UserServiceFactory.getUserService();
+ User user = userService.getCurrentUser();
+
+ String currentUrl = request.getRequestURI();
+ if (request.getQueryString() != null) {
+ currentUrl = currentUrl + "?" + request.getQueryString();
+ }
+
+ if (user != null) {
+ req.setAttribute("currentUser", user);
+ String logoutUrl = userService.createLogoutURL(currentUrl);
+ req.setAttribute("logoutUrl", logoutUrl);
+ } else {
+ String loginUrl = userService.createLoginURL(currentUrl);
+ req.setAttribute("loginUrl", loginUrl);
+ }
+
+ chain.doFilter(req, resp);
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) {
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+}
View
98 war/WEB-INF/web.xml
@@ -1,44 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
-<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-xmlns="http://java.sun.com/xml/ns/javaee"
-xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
-xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
- <servlet>
- <servlet-name>AddThreadServlet</servlet-name>
- <servlet-class>com.ikai.protodemo.AddThreadServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>ReplyToThreadServlet</servlet-name>
- <servlet-class>com.ikai.protodemo.ReplyToThreadServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>ViewAllThreadsServlet</servlet-name>
- <servlet-class>com.ikai.protodemo.ViewAllThreadsServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>ViewThreadServlet</servlet-name>
- <servlet-class>com.ikai.protodemo.ViewThreadServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>AddThreadServlet</servlet-name>
- <url-pattern>/add_thread</url-pattern>
- </servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>ReplyToThreadServlet</servlet-name>
- <url-pattern>/reply</url-pattern>
- </servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>ViewAllThreadsServlet</servlet-name>
- <url-pattern>/all</url-pattern>
- </servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>ViewThreadServlet</servlet-name>
- <url-pattern>/view_thread</url-pattern>
- </servlet-mapping>
-
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ version="2.5">
+
+ <filter>
+ <filter-name>SetUserFilter</filter-name>
+ <filter-class>com.ikai.protodemo.SetUserFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>SetUserFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+
+ <servlet>
+ <servlet-name>AddThreadServlet</servlet-name>
+ <servlet-class>com.ikai.protodemo.AddThreadServlet</servlet-class>
+ </servlet>
+ <servlet>
+ <servlet-name>ReplyToThreadServlet</servlet-name>
+ <servlet-class>com.ikai.protodemo.ReplyToThreadServlet</servlet-class>
+ </servlet>
+ <servlet>
+ <servlet-name>ViewAllThreadsServlet</servlet-name>
+ <servlet-class>com.ikai.protodemo.ViewAllThreadsServlet</servlet-class>
+ </servlet>
+ <servlet>
+ <servlet-name>ViewThreadServlet</servlet-name>
+ <servlet-class>com.ikai.protodemo.ViewThreadServlet</servlet-class>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>AddThreadServlet</servlet-name>
+ <url-pattern>/add_thread</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
+ <servlet-name>ReplyToThreadServlet</servlet-name>
+ <url-pattern>/reply</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
+ <servlet-name>ViewAllThreadsServlet</servlet-name>
+ <url-pattern>/all</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
+ <servlet-name>ViewThreadServlet</servlet-name>
+ <url-pattern>/view_thread</url-pattern>
+ </servlet-mapping>
+
+ <welcome-file-list>
+ <welcome-file>ViewAllThreadsServlet</welcome-file>
+ </welcome-file-list>
+
</web-app>

0 comments on commit 29e0c5e

Please sign in to comment.