| 1 | |
package org.restafarian.core.filters; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.net.URLEncoder; |
| 5 | |
|
| 6 | |
import javax.servlet.FilterChain; |
| 7 | |
import javax.servlet.ServletException; |
| 8 | |
import javax.servlet.ServletRequest; |
| 9 | |
import javax.servlet.ServletResponse; |
| 10 | |
import javax.servlet.http.HttpServletRequest; |
| 11 | |
import javax.servlet.http.HttpServletResponse; |
| 12 | |
|
| 13 | |
import org.apache.commons.configuration.Configuration; |
| 14 | |
import org.restafarian.core.beans.Person; |
| 15 | |
import org.restafarian.core.security.AuthenticatedUserManager; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | 0 | public class SecurityFilter extends FilterBase { |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { |
| 31 | |
|
| 32 | 0 | HttpServletRequest req = (HttpServletRequest) request; |
| 33 | 0 | HttpServletResponse res = (HttpServletResponse) response; |
| 34 | |
|
| 35 | 0 | Person authenticatedUser = AuthenticatedUserManager.getAuthenticatedUser(req); |
| 36 | |
|
| 37 | 0 | if (authenticatedUser == null) { |
| 38 | |
|
| 39 | 0 | Configuration configuration = (Configuration) context.getAttribute("configuration"); |
| 40 | 0 | if (configuration != null) { |
| 41 | |
|
| 42 | 0 | String redirectTo = configuration.getString("userLogonPage"); |
| 43 | 0 | if (redirectTo != null && redirectTo.length() > 0) { |
| 44 | |
|
| 45 | 0 | String originalURL = req.getRequestURI(); |
| 46 | 0 | String connector = "?"; |
| 47 | 0 | if (redirectTo.indexOf("?") != -1) { |
| 48 | 0 | connector = "&"; |
| 49 | |
} |
| 50 | 0 | redirectTo += connector + "returnurl=" + URLEncoder.encode(originalURL, "ISO-8859-1"); |
| 51 | 0 | res.sendRedirect(redirectTo); |
| 52 | 0 | } else { |
| 53 | 0 | throw new ServletException("SecurityFilter has not been properly initialized -- check configuration."); |
| 54 | |
} |
| 55 | 0 | } else { |
| 56 | 0 | throw new ServletException("SecurityFilter has not been properly initialized -- check configuration."); |
| 57 | |
} |
| 58 | |
} |
| 59 | |
|
| 60 | 0 | chain.doFilter(req, res); |
| 61 | 0 | } |
| 62 | |
} |