openrat-java-client

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/openrat-java-client.git
Log | Files | Refs

LoginAction.java (1690B)


      1 package de.openrat.client.action;
      2 
      3 import javax.security.auth.login.LoginException;
      4 
      5 import de.openrat.client.dto.User;
      6 import de.openrat.client.util.CMSConnection;
      7 import de.openrat.client.util.CMSException;
      8 import de.openrat.client.util.CMSResponse;
      9 import de.openrat.client.util.CMSServerErrorException;
     10 import de.openrat.client.util.HttpRequest.HttpMethod;
     11 
     12 /**
     13  * This class is NOT threadsafe and should be used by one thread simultaneously.
     14  * 
     15  * @author dankert
     16  */
     17 public class LoginAction extends Action
     18 {
     19 	private final static String ACTION = "login";
     20 	public LoginAction(CMSConnection connection)
     21 	{
     22 		super(connection);
     23 	}
     24 
     25 	/**
     26 	 * Login.
     27 	 * 
     28 	 * @param username
     29 	 *            username
     30 	 * @param password
     31 	 *            password
     32 	 * @param databaseId
     33 	 *            database-id
     34 	 * @return {@link User}
     35 	 * @throws LoginException
     36 	 *             if credentials are wrong
     37 	 */
     38 	public User login(String username, String password, String databaseId) throws LoginException
     39 	{
     40 
     41 		setParameter("dbid", databaseId);
     42 		executeView(ACTION, "login");
     43 
     44 		clear();
     45 		setParameter("login_name", username);
     46 		setParameter("login_password", password);
     47 		setParameter("dbid", databaseId);
     48 		try
     49 		{
     50 			CMSResponse response = executePost(ACTION, "login");
     51 
     52 			User user = new User();
     53 			return user;
     54 		}
     55 		catch (CMSException e)
     56 		{
     57 			if (e instanceof CMSServerErrorException && ((CMSServerErrorException) e).getStatus().equals("LOGIN_FAILED"))
     58 				// wrong credentials - throw checked exception
     59 				throw new LoginException(e.getLocalizedMessage());
     60 			else
     61 				// otherwise it's a technical exception
     62 				throw e;
     63 		}
     64 
     65 	}
     66 
     67 	public void logout()
     68 	{
     69 		executePost(ACTION, "logout");
     70 	}
     71 }