Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/LoginServlet.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/server/LoginServlet.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.server; | |
2 | |
3 import java.io.IOException; | |
4 | |
5 import javax.servlet.ServletException; | |
6 import javax.servlet.ServletContext; | |
7 import javax.servlet.http.HttpServlet; | |
8 import javax.servlet.http.HttpServletRequest; | |
9 import javax.servlet.http.HttpServletResponse; | |
10 import javax.servlet.http.HttpSession; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import org.dive4elements.river.client.server.auth.Authentication; | |
15 import org.dive4elements.river.client.server.auth.AuthenticationException; | |
16 import org.dive4elements.river.client.server.auth.AuthenticationFactory; | |
17 import org.dive4elements.river.client.server.auth.User; | |
18 import org.dive4elements.river.client.server.auth.UserClient; | |
19 import org.dive4elements.river.client.server.features.Features; | |
20 | |
21 public class LoginServlet extends HttpServlet { | |
22 | |
23 private static Logger logger = Logger.getLogger(LoginServlet.class); | |
24 | |
25 private static final String FLYS_PAGE = "FLYS.html"; | |
26 private static final String LOGIN_PAGE = "login.jsp"; | |
27 | |
28 private void redirectFailure(HttpServletResponse resp, String path) | |
29 throws IOException { | |
30 resp.sendRedirect(path + "/" + LOGIN_PAGE); | |
31 } | |
32 | |
33 private void redirectFailure(HttpServletResponse resp, String path, | |
34 Exception e) throws IOException { | |
35 this.redirectFailure(resp, path, e.getMessage()); | |
36 } | |
37 | |
38 private void redirectFailure(HttpServletResponse resp, String path, | |
39 String message) throws IOException { | |
40 resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message); | |
41 } | |
42 | |
43 private void redirectSuccess(HttpServletResponse resp, String path, | |
44 String uri) throws IOException { | |
45 if (uri == null) { | |
46 String redirecturl = getServletContext().getInitParameter("redirect-url"); | |
47 if (redirecturl == null) { | |
48 redirecturl = FLYS_PAGE; | |
49 } | |
50 uri = "/" + redirecturl; | |
51 } | |
52 resp.sendRedirect(uri); | |
53 } | |
54 | |
55 @Override | |
56 protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
57 throws ServletException, IOException { | |
58 logger.debug("Processing get request"); | |
59 this.redirectFailure(resp, req.getContextPath()); | |
60 } | |
61 | |
62 @Override | |
63 protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
64 throws ServletException, IOException | |
65 { | |
66 String encoding = req.getCharacterEncoding(); | |
67 String username = req.getParameter("username"); | |
68 String password = req.getParameter("password"); | |
69 | |
70 logger.debug("Processing post request"); | |
71 | |
72 if (username == null || password == null) { | |
73 logger.debug("No username or password provided"); | |
74 this.redirectFailure(resp, req.getContextPath()); | |
75 return; | |
76 } | |
77 | |
78 try { | |
79 Authentication aresp = this.auth(username, password, encoding); | |
80 if (aresp == null || !aresp.isSuccess()) { | |
81 logger.debug("Authentication not successful"); | |
82 this.redirectFailure(resp, req.getContextPath()); | |
83 return; | |
84 } | |
85 User user = aresp.getUser(); | |
86 | |
87 String url = getServletContext().getInitParameter("server-url"); | |
88 UserClient client = new UserClient(url); | |
89 if (!client.userExists(user)) { | |
90 logger.debug("Creating db user"); | |
91 if (!client.createUser(user)) { | |
92 this.redirectFailure(resp, req.getContextPath(), | |
93 "Could not create new user"); | |
94 return; | |
95 } | |
96 } | |
97 | |
98 HttpSession session = req.getSession(); | |
99 session.setAttribute("user", user); | |
100 | |
101 String uri = (String)session.getAttribute("requesturi"); | |
102 | |
103 this.redirectSuccess(resp, req.getContextPath(), uri); | |
104 } | |
105 catch(AuthenticationException e) { | |
106 logger.error(e, e); | |
107 this.redirectFailure(resp, req.getContextPath(), e); | |
108 } | |
109 } | |
110 | |
111 private Authentication auth(String username, String password, String encoding) | |
112 throws AuthenticationException, IOException | |
113 { | |
114 ServletContext sc = this.getServletContext(); | |
115 Features features = (Features)sc.getAttribute(Features.CONTEXT_ATTRIBUTE); | |
116 String auth = sc.getInitParameter("authentication"); | |
117 return AuthenticationFactory.getInstance(auth).auth(username, password, | |
118 encoding, features); | |
119 } | |
120 } |