]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman/src/org/splat/simer/ConnectionAction.java
Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ConnectionAction.java
index 6f7eab962e6ce2eed6499ec17fd0c02a3cbcabaf..2838c977a68e9bf9b3e289d818c7a83480836c6d 100644 (file)
@@ -1,49 +1,69 @@
 package org.splat.simer;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.Set;
 
-import javax.security.auth.login.LoginContext;
 import javax.security.auth.Subject;
-import javax.security.auth.callback.*;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.TextOutputCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.FailedLoginException;
+import javax.security.auth.login.LoginContext;
 
 import org.splat.dal.bo.kernel.User;
 import org.splat.som.ApplicationRights;
 
-import java.io.IOException;
-import javax.security.auth.login.FailedLoginException;
-
+/**
+ * User login action.
+ */
 public class ConnectionAction extends Action {
 
-       private String username = null;
-       private String password = null;
-       private String backmenu = null;
-    
-    private String _menuProperty;
+       /**
+        * User name.
+        */
+       private String _username = null;
+       /**
+        * User password.
+        */
+       private String _password = null;
+       private transient String _backmenu = null;
+
+       private String _menuProperty;
 
        /**
         * Serial version ID.
         */
        private static final long serialVersionUID = 6095471616361606231L;
 
+       /**
+        * Handler for login into SIMAN.
+        */
        private class Handler implements CallbackHandler {
-               // ------------------------------------------------
-               public void handle(Callback[] callbacks) throws IOException,
+               /**
+                * {@inheritDoc}
+                * 
+                * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
+                */
+               public void handle(final Callback[] callbacks) throws IOException,
                                UnsupportedCallbackException {
                        for (int i = 0; i < callbacks.length; i++) {
                                if (callbacks[i] instanceof TextOutputCallback) {
                                        // Display a message according to a specified type
-
+                                       LOG.info(((TextOutputCallback) callbacks[i]).getMessage());
                                } else if (callbacks[i] instanceof NameCallback) {
                                        // Get the username
                                        NameCallback call = (NameCallback) callbacks[i];
-                                       call.setName(username);
+                                       call.setName(_username);
 
                                } else if (callbacks[i] instanceof PasswordCallback) {
                                        // Get the password
-                                       if (password != null) {
+                                       if (_password != null) {
                                                PasswordCallback call = (PasswordCallback) callbacks[i];
-                                               call.setPassword(password.toCharArray());
+                                               call.setPassword(_password.toCharArray());
                                        }
                                } else {
                                        throw new UnsupportedCallbackException(callbacks[i],
@@ -57,55 +77,71 @@ public class ConnectionAction extends Action {
        // Action execution
        // ==============================================================================================================================
 
+       /**
+        * Login the user with the given name and password into SIMAN.
+        * 
+        * @return SUCCESS if succeeded, INPUT - if some mandatory parameter is absent or FailedLoginException is caught during login, ERROR -
+        *         if other exception is caught
+        * 
+        * @see org.splat.kernel.RealmLoginModule
+        */
        @SuppressWarnings("unchecked")
-       public String doLogin() throws Exception {
-               // ------------------------
-               if (username == null || username.length() == 0)
-                       return INPUT;
-               if (password != null && password.length() == 0)
-                       password = null; // User having no password
-               try {
-                       LoginContext context = new LoginContext("Simer", new Handler());
-                       context.login();
-
-                       Subject identity = context.getSubject();
-                       Set<User> table = identity.getPrincipals(User.class);
-                       if (table.isEmpty())
-                               throw new Exception();
-
-                       User user = table.iterator().next(); // The user is (apparently...) the 1st principal
-                       ApplicationRights logged = new ApplicationRights(user);
-                       if (logged.canContributeToStudy() || logged.canValidate()) {
-                               // TODO: Set the search filter according to user preferences
-                               Map<String, Object> session = getSession();
-                               // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
-                               Map<String, Object> sfilter = (Map<String, Object>) session
-                                               .get("study.filter");
+       public String doLogin() {
+               String res = INPUT;
+               if (_username != null && _username.length() > 0) {
+                       if (_password != null && _password.length() == 0) {
+                               _password = null; // User having no password
+                       }
+                       try {
+                               LoginContext context = new LoginContext("Siman", new Handler());
+                               context.login();
+
+                               Subject identity = context.getSubject();
+                               Set<User> table = identity.getPrincipals(User.class);
+                               if (table.isEmpty()) {
+                                       throw new Exception();
+                               }
 
-                               sfilter.put("state", "ANY");
-                               sfilter.put("visibility", "PRIVATE");
-                               if (logged.canCreateStudy())
-                                       sfilter.put("author", String.valueOf(user.getIndex()));
+                               User user = table.iterator().next(); // The user is (apparently...) the 1st principal
+                               ApplicationRights logged = new ApplicationRights(user);
+                               if (logged.canContributeToStudy() || logged.canValidate()) {
+                                       // TODO: Set the search filter according to user preferences
+                                       Map<String, Object> session = getSession();
+                                       // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
+                                       Map<String, Object> sfilter = (Map<String, Object>) session
+                                                       .get("study.filter");
+
+                                       sfilter.put("state", "ANY");
+                                       sfilter.put("visibility", "PRIVATE");
+                                       if (logged.canCreateStudy()) {
+                                               sfilter.put("author", String.valueOf(user.getIndex()));
+                                       }
+                               }
+                               this.connect(context, user); // Updates the session context
+
+                               setMenuProperty("none");
+                               initializationScreenContext(_menuProperty);
+
+                               res = _backmenu;
+                       } catch (FailedLoginException error) {
+                               setErrorCode("message.error.login." + error.getMessage());
+                               res = INPUT;
+                       } catch (Exception error) {
+                               LOG.error("Reason:", error);
+                               res = ERROR;
                        }
-                       this.connect(context, user); // Updates the session context
-                       
-                       setMenuProperty("none");
-                       initializationScreenContext(_menuProperty);
-                       
-                       return backmenu;
-               } catch (FailedLoginException error) {
-                       setErrorCode("message.error.login." + error.getMessage());
-                       return INPUT;
-               } catch (Exception error) {
-                       LOG.error("Reason:", error);
-                       return ERROR;
                }
+               return res;
        }
 
-
+       /**
+        * Disconnect the current user from SIMAN application.
+        * 
+        * @return SUCCESS if disconnected, ERROR - if exception is caught
+        */
        @SuppressWarnings("unchecked")
        public String doLogout() {
-               // -------------------------
+               String res;
                try {
                        Map<String, Object> session = getSession();
                        // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
@@ -117,10 +153,9 @@ public class ConnectionAction extends Action {
                        if (getConnectedUser() != null) {
                                connectedUsr = getConnectedUser().toString();
                        }
-                       
-                       LOG.info("Deconnection of " + connectedUsr
-                                       + ".");
-                       
+
+                       LOG.info("Deconnection of " + connectedUsr + ".");
+
                        if (context != null) {
                                context.logout();
                        }
@@ -132,45 +167,73 @@ public class ConnectionAction extends Action {
                        }
 
                        this.disconnect(); // Updates the session context
-                       
+
                        setMenuProperty("none");
                        initializationScreenContext(_menuProperty);
-                       
-                       return backmenu;
+
+                       res = _backmenu;
                } catch (Exception error) {
                        LOG.error("Reason:", error);
-                       return ERROR;
+                       res = ERROR;
                }
+               return res;
        }
 
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-       
-    public String getUsername () {
-//  ----------------------------       
-      return username;
-    }
-    public String getPassword () {
-//  ----------------------------
-      return password;
-    }
-
-    public void setUsername (String value) {
-//  --------------------------------------
-      this.username = value;
-    }
-    public void setPassword (String value) {
-//  --------------------------------------
-      this.password = value;
-    }
-    public void setBackMenu (String menu) {
-//  -------------------------------------
-      this.backmenu = menu;
-    }
-    
-    /**
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       /**
+        * Get user name.
+        * 
+        * @return user name
+        */
+       public String getUsername() {
+               return _username;
+       }
+
+       /**
+        * Get user password.
+        * 
+        * @return user password
+        */
+       public String getPassword() {
+               return _password;
+       }
+
+       /**
+        * Set user name.
+        * 
+        * @param value
+        *            user name
+        */
+       public void setUsername(final String value) {
+               this._username = value;
+       }
+
+       /**
+        * Set user password.
+        * 
+        * @param value
+        *            the password
+        */
+       public void setPassword(final String value) {
+               this._password = value;
+       }
+
+       /**
+        * Set menu for the user.
+        * 
+        * @param menu
+        *            menu key string
+        */
+       public void setBackMenu(final String menu) {
+               this._backmenu = menu;
+       }
+
+       /**
         * Get the menuProperty.
+        * 
         * @return the menuProperty
         */
        public String getMenuProperty() {
@@ -179,9 +242,11 @@ public class ConnectionAction extends Action {
 
        /**
         * Set the menuProperty.
-        * @param menuProperty the menuProperty to set
+        * 
+        * @param menuProperty
+        *            the menuProperty to set
         */
-       public void setMenuProperty(String menuProperty) {
+       public void setMenuProperty(final String menuProperty) {
                this._menuProperty = menuProperty;
        }
 }
\ No newline at end of file