source: ether_statistics/web/src/com/ether/ControllerEponge.java

Last change on this file was 667, checked in by vmipsl, 12 years ago

account BD

File size: 4.0 KB
Line 
1package com.ether;
2
3import com.ether.account.Account;
4import com.ether.annotation.ControllerMethod;
5import com.ether.annotation.ParamName;
6import net.sf.json.JSONObject;
7import org.apache.commons.logging.Log;
8import org.apache.commons.logging.LogFactory;
9import org.jetbrains.annotations.NotNull;
10
11import javax.servlet.http.HttpServletRequest;
12import java.security.DigestException;
13import java.security.NoSuchAlgorithmException;
14import java.util.HashMap;
15import java.util.Map;
16
17/**
18 * @author vmipsl
19 * @date 15 march 2012
20 */
21public class ControllerEponge
22        extends ControllerEther
23{
24    /** *********************************************************** **/
25    /** *********************** VIEWS ***************************** **/
26    /** *********************************************************** **/
27    /**
28     * Home method
29     *
30     * @return
31     * @throws WebException
32     */
33    @ControllerMethod(view = VIEW_INIT)
34    public Map<String, Object> home()
35            throws WebException
36    {
37        return new HashMap<String, Object>();
38    }
39
40    /** *********************************************************** **/
41    /** *********************** CALLS ***************************** **/
42    /** *********************************************************** **/
43    /**
44     * This method logs a user
45     *
46     * @param login
47     * @param password
48     * @param request
49     * @return
50     * @throws ServiceException
51     */
52    @ControllerMethod(requestMandatory = true, jsonResult = true)
53    public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login,
54                             @ParamName(ParameterConstants.PARAMETER_PWD) final String password,
55                             @NotNull final HttpServletRequest request )
56            throws WebException
57    {
58        final JSONObject jSONUser = new JSONObject();
59        final JSONObject result = new JSONObject();
60
61        if( login != null && password != null )
62        {
63            try
64            {
65                final Account account = getEtherService().getAccountByLogin( login );
66                final String encryptedPassword = EtherHelper.encryptPassword( password );
67                if( account == null )
68                    throw new WebException( WebException.WebCode.ERROR_LOGIN_NOT_FOUND, "No login found", WebException.getExceptionThrowable() );
69                else if( !encryptedPassword.equals( account.getPassword() ) )
70                    throw new WebException( WebException.WebCode.ERROR_PASSWORD, "Wrong password", WebException.getExceptionThrowable() );
71                else
72                {
73                    request.getSession().setAttribute( "SES_ACCOUNT", account );
74                    jSONUser.put( "name", account.getName() );
75                    jSONUser.put( "role", account.getRole() );
76                }
77            }
78            catch( DigestException e )
79            {
80                throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "Error to encrypt password", WebException.getExceptionThrowable() );
81            }
82            catch( NoSuchAlgorithmException e )
83            {
84                throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "Error to encrypt password", WebException.getExceptionThrowable() );
85            }
86            catch( ServiceException e )
87            {
88                throw new WebException( WebException.WebCode.ERROR_LOGIN_NOT_FOUND, "Error no login found", WebException.getExceptionThrowable() );
89            }
90        }
91
92        if( !jSONUser.isEmpty() )
93            result.put( "jSONUser", jSONUser );
94        return result;
95    }
96
97    @ControllerMethod(requestMandatory = true, jsonResult = true)
98    public JSONObject logout( @NotNull final HttpServletRequest request )
99            throws WebException
100    {
101        request.getSession().setAttribute( "SES_ACCOUNT", null );
102        request.getSession().invalidate();
103
104        return new JSONObject();
105    }
106
107    private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class );
108    private static final String VIEW_INIT = "init";
109}
Note: See TracBrowser for help on using the repository browser.