View Javadoc
1   /******************************************************************************
2    * BSAccountController.java - login/logout
3    * 
4    * BSAccount - BuckoSoft Web Account Manager 
5    * Copyright(c) 2015 - Dick Balaska and BuckoSoft, Corp.
6    * 
7    */
8   package com.buckosoft.BSAccount.web;
9   
10  import java.io.IOException;
11  import java.io.PrintWriter;
12  
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.springframework.beans.factory.annotation.Autowired;
19  import org.springframework.http.MediaType;
20  import org.springframework.stereotype.Controller;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.bind.annotation.RequestMethod;
23  
24  import com.buckosoft.BSAccount.BSAccountMan;
25  import com.buckosoft.BSAccount.domain.BSAccount;
26  import com.buckosoft.BSAccount.domain.BSAccountUser;
27  import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
28  import com.buckosoft.BSAccount.domain.Credentials;
29  
30  /** Controller for login and logout.
31   * @see BSAccount
32   * @author dick
33   * @since 2015-04-22
34   *
35   */
36  @Controller
37  public class BSAccountController {
38  	protected final Log log = LogFactory.getLog(getClass());
39  
40  	@Autowired
41  	private BSAccountMan	bsAccountMan;
42  
43  	/** Login to the BuckoSoft web system 
44  	 * @param request
45  	 * @param response
46  	 * @param credentials A username/password object passed from the web
47  	 * @return either "OK" or a detailed message of the failure
48  	 */
49  	@RequestMapping(value="/login", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
50  	public void login(HttpServletRequest request, HttpServletResponse response,
51  			Credentials credentials) {
52  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
53  		if (log.isInfoEnabled()) {
54  			String s = credentials.getPassword().replaceAll(".*", "+");
55  			log.info("username=" + credentials.getUsername() + " password=" + s);
56  		}
57  		log.debug("userWebSession=" + userWebSession);
58  		BSAccount account = this.bsAccountMan.getAccount(credentials.getUsername(), credentials.getPassword());
59  		String ret = "unknown";
60  		if (account == null) {
61  			ret = "user/password not found";
62  			log.info("user " + credentials.getUsername() + " FAILED log in");
63  			try {
64  				response.sendError(401, ret);
65  				return;
66  			} catch (IOException e) {
67  				e.printStackTrace();
68  			}
69  		} else {
70  			ret = "OK";
71  			BSAccountUser user = this.bsAccountMan.getUser(account);
72  			userWebSession.setUser(user);
73  			log.info("user " + credentials.getUsername() + " logged in");
74  		}
75  		try {
76  			response.getWriter().print(ret);
77  		} catch (IOException e) {}
78  		return;
79  	}
80  	
81  	@RequestMapping(value="/logout", produces=MediaType.TEXT_PLAIN_VALUE)
82  	public void logout(HttpServletRequest request, HttpServletResponse response) {
83  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
84  		if (userWebSession.isLoggedIn()) {
85  			log.info("user " + userWebSession.getUser().getUsername() + " logged out");
86  			userWebSession.setUser(bsAccountMan.getNewUser());
87  			request.getSession().invalidate();
88  			try {
89  				response.getWriter().print("OK");
90  			} catch (IOException e) {}
91  	
92  		} else {
93  			try {
94  				response.sendError(401, "Not logged in");
95  				response.getWriter().print("Not logged in");
96  			} catch (IOException e) {}
97  		}
98  		return;
99  	}
100 
101 	@RequestMapping(value="/getJumpApp", produces=MediaType.TEXT_PLAIN_VALUE)
102 	public void getJumpApp(HttpServletRequest request, PrintWriter writer) {
103 		long ret = 0;
104 		
105 		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
106 		if (userWebSession.isLoggedIn()) {
107 			ret = this.bsAccountMan.getAppJumper(userWebSession.getUser().accessAccount());
108 		}
109 		writer.print("" + ret);
110 	}
111 }