comparison flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java @ 3851:a4c9296f6efa

Use the Context PATH servlet variable when using URLs in the GGInAFilter flys-client/trunk@5591 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Mon, 24 Sep 2012 13:04:53 +0000
parents 2e12518ff5b4
children 17fe00c09b7c
comparison
equal deleted inserted replaced
3850:8d0ababa2db7 3851:a4c9296f6efa
20 20
21 public class LoginServlet extends HttpServlet { 21 public class LoginServlet extends HttpServlet {
22 22
23 private static Logger logger = Logger.getLogger(LoginServlet.class); 23 private static Logger logger = Logger.getLogger(LoginServlet.class);
24 24
25 private void redirectFailure(HttpServletResponse resp) throws IOException { 25 private void redirectFailure(HttpServletResponse resp, String path)
26 resp.sendRedirect("/login.jsp"); 26 throws IOException {
27 resp.sendRedirect(path + "/login.jsp");
27 } 28 }
28 29
29 private void redirectFailure(HttpServletResponse resp, Exception e) throws IOException { 30 private void redirectFailure(HttpServletResponse resp, String path,
30 this.redirectFailure(resp, e.getMessage()); 31 Exception e) throws IOException {
32 this.redirectFailure(resp, path, e.getMessage());
31 } 33 }
32 34
33 private void redirectFailure(HttpServletResponse resp, String message) throws IOException { 35 private void redirectFailure(HttpServletResponse resp, String path,
34 resp.sendRedirect("/login.jsp?error=" + message); 36 String message) throws IOException {
37 resp.sendRedirect(path + "/login.jsp?error=" + message);
35 } 38 }
36 private void redirectSuccess(HttpServletResponse resp, String uri) throws IOException { 39
40 private void redirectSuccess(HttpServletResponse resp, String path,
41 String uri) throws IOException {
37 if (uri == null) { 42 if (uri == null) {
38 uri = "/FLYS.html"; 43 uri = path + "/FLYS.html";
39 } 44 }
40 resp.sendRedirect(uri); 45 resp.sendRedirect(uri);
41 } 46 }
42 47
43 @Override 48 @Override
44 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 49 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
45 throws ServletException, IOException { 50 throws ServletException, IOException {
46 logger.debug("Processing get request"); 51 logger.debug("Processing get request");
47 this.redirectFailure(resp); 52 this.redirectFailure(resp, req.getContextPath());
48 } 53 }
49 54
50 @Override 55 @Override
51 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 56 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
52 throws ServletException, IOException 57 throws ServletException, IOException
57 62
58 logger.debug("Processing post request"); 63 logger.debug("Processing post request");
59 64
60 if (username == null || password == null) { 65 if (username == null || password == null) {
61 logger.debug("No username or password provided"); 66 logger.debug("No username or password provided");
62 this.redirectFailure(resp); 67 this.redirectFailure(resp, req.getContextPath());
63 return; 68 return;
64 } 69 }
65 70
66 try { 71 try {
67 Authentication aresp = this.auth(username, password, encoding); 72 Authentication aresp = this.auth(username, password, encoding);
68 if (aresp == null || !aresp.isSuccess()) { 73 if (aresp == null || !aresp.isSuccess()) {
69 logger.debug("Authentication not successful"); 74 logger.debug("Authentication not successful");
70 this.redirectFailure(resp); 75 this.redirectFailure(resp, req.getContextPath());
71 } 76 }
72 User user = aresp.getUser(); 77 User user = aresp.getUser();
73 78
74 String url = getServletContext().getInitParameter("server-url"); 79 String url = getServletContext().getInitParameter("server-url");
75 UserClient client = new UserClient(url); 80 UserClient client = new UserClient(url);
76 if (!client.userExists(user)) { 81 if (!client.userExists(user)) {
77 logger.debug("Creating db user"); 82 logger.debug("Creating db user");
78 if (!client.createUser(user)) { 83 if (!client.createUser(user)) {
79 this.redirectFailure(resp, "Could not create new user"); 84 this.redirectFailure(resp, req.getContextPath(),
85 "Could not create new user");
80 } 86 }
81 } 87 }
82 88
83 HttpSession session = req.getSession(); 89 HttpSession session = req.getSession();
84 session.setAttribute("user", user); 90 session.setAttribute("user", user);
85 91
86 String uri = (String)session.getAttribute("requesturi"); 92 String uri = (String)session.getAttribute("requesturi");
87 93
88 this.redirectSuccess(resp, uri); 94 this.redirectSuccess(resp, req.getContextPath(), uri);
89 } 95 }
90 catch(AuthenticationException e) { 96 catch(AuthenticationException e) {
91 logger.error(e); 97 logger.error(e);
92 this.redirectFailure(resp, e); 98 this.redirectFailure(resp, req.getContextPath(), e);
93 } 99 }
94 } 100 }
95 101
96 private Authentication auth(String username, String password, String encoding) 102 private Authentication auth(String username, String password, String encoding)
97 throws AuthenticationException, IOException 103 throws AuthenticationException, IOException

http://dive4elements.wald.intevation.org