0 Replies Latest reply on Sep 25, 2007 8:20 AM by Venkata Apparao

    Getting Exception in Custom IdentityLoginModule code

    Venkata Apparao Newbie

      Hi I am new to this jboss-portal,

      Currently I am working on migrating already existing portal developed with jboss-portal2.4 to jboss-portal2.6.1.

      My portal is using a custom login module which extends IdentityLoginModule, as lot of changes happend to the Jboss portal Identity api in jboss-portal2.6.1 , I changed the existing code with the new api changes.

      This is My Code

      public class SsoLoginModule extends IdentityLoginModule

      {

      private static final Logger logger = Logger.getLogger(SsoLoginModule.class);

      private static final String DEFAULT_USER_ROLE = "User";

      private static final String JNDI_TRANS_MGR = "java:/TransactionManager";

      private static final String POLICY_CONTEXT_HTTP_SERVLET =

      "javax.servlet.http.HttpServletRequest";

      private static final String OPTION_NAME_DEFAULT_ROLE = "defaultRole";

      private static final String OPTION_NAME_ROLE_MOD_JNDI = "roleModuleJNDIName";

      // public static final short UNDEFINED_TIMEZONE = (short)0;



      protected String defaultRoleForNewUser = DEFAULT_USER_ROLE;

      protected String userModuleJNDIName;



      protected String roleModuleJNDIName;



      protected String userProfileModuleJNDIName;



      protected String membershipModuleJNDIName;



      private UserModule userModule;



      private RoleModule roleModule;



      private UserProfileModule userProfileModule;



      private MembershipModule membershipModule;



      private Transaction transaction;

      private SessionFactory identitySessionFactory;

      private boolean success = false;

      private Session session;



      /**

      * Initialize the login module.

      * @param subject Authentication subject

      * @param callbackHandler Callback handler for the login module

      * @param sharedState Shared State Map

      * @param options Configuration options of the login module

      */

      public void initialize(Subject subject, CallbackHandler callbackHandler,

      Map sharedState, Map options) {

      super.initialize(subject, callbackHandler, sharedState, options);

      logger.info("Initializing login module.");

      String specifiedDefaultRole=(String)options.get(OPTION_NAME_DEFAULT_ROLE);

      if (!isNullOrEmpty(specifiedDefaultRole)) {

      defaultRoleForNewUser = specifiedDefaultRole;

      }

      userModuleJNDIName = (String) options.get("userModuleJNDIName");

      roleModuleJNDIName = (String) options.get("roleModuleJNDIName");

      userProfileModuleJNDIName = (String) options

      .get("userProfileModuleJNDIName");

      membershipModuleJNDIName = (String) options

      .get("membershipModuleJNDIName");

      logger.info("User module JNDI = "+userModuleJNDIName);

      logger.info("Role module JNDI = "+roleModuleJNDIName);

      logger.info("UserProfile module JNDI = "+userProfileModuleJNDIName);

      logger.info("Membership module JNDI = "+membershipModuleJNDIName);

      logger.info("Exit Initialize()");

      }



      /**

      * Perform log in process.

      */

      public boolean login() throws LoginException {

      logger.info("Inside login.");

      createUserIfNew();

      logger.info("About to exit login().");

      return super.login();

      }



      /**

      * Create user if user does not exist.

      */

      protected void createUserIfNew() throws LoginException {

      try {

      logger.info("Inside createUserIfNew.");

      TransactionManager tm = (TransactionManager)

      new InitialContext().lookup(JNDI_TRANS_MGR);

      Transactions.required(tm, new Transactions.Runnable()

      {

      public Object run() throws Exception {

      String username = null;

      User user = null;

      try {

      username = (getUsernameAndPassword())[0];

      logger.info("Username = " + username);



      logger.info("Before getting user from UserModule.");

      try{

      user = getUserModule().findUserByUserName(username);

      }catch(NullPointerException ne){

      // in case module implementation doesn't throw proper

      // exception...

      if (user == null)

      {

      throw new NoSuchUserException("UserModule returned null user object");

      }

      }

      logger.info("User ID in the table jbp_users ="+user.getId().toString());

      fillContextWithUserProfile(getUserProfileModule(), user);



      return null;

      }

      catch(NoSuchUserException nsue) {

      try{

      // User not found, let's create it

      logger.info("Create new user " + username);

      // HttpServletRequest request = getHttpServletRequest();

      HttpServletRequest request = getHttpServletRequest();

      logger.info(request.getHeader(Constant.SSO_UID)+","+

      request.getHeader(Constant.SSO_GESSOUID));

      logger.info("Just about to create User");

      //identitySessionFactory = (SessionFactory)new InitialContext().lookup("java:/portal/IdentitySessionFactory");

      //session = identitySessionFactory.openSession();

      //transaction = session.beginTransaction();

      user = userModule.createUser(

      request.getHeader(Constant.SSO_UID),"Pa55word");


      // request.getHeader(Constant.SSO_EMAIL));

      logger.info("User object is ="+user);

      logger.info("User created successfully");

      if(null == userProfileModule) {

      userProfileModule = getUserProfileModule();



      }

      fillContextWithUserProfile(userProfileModule, user);



      Set roleSet = new HashSet();

      Role role = getRoleModule().findRoleByName(defaultRoleForNewUser);

      if (role != null)

      {

      roleSet.add(role);

      getMembershipModule().assignRoles(user, roleSet);

      }

      }

      catch (IllegalArgumentException e)

      {

      logger.error("Cannot create user " + username, e);

      }

      catch(IdentityException ie){

      logger.error("Cannot create user " + username, ie);

      }



      return null;

      }

      catch (Exception e) {

      e.printStackTrace();

      throw new LoginException("Error in find/create user: " +

      e.getMessage());

      }





      }

      });

      }

      catch (Exception e) {

      Throwable cause = e.getCause();

      e.printStackTrace();

      throw new LoginException(cause.toString());

      }



      logger.info("Exit CreateUserIfNew()");

      }



      protected UserModule getUserModule() throws NamingException

      {

      logger.info("Enter getUserModule()");

      if (userModule == null)

      {

      userModule = (UserModule)new InitialContext().lookup(userModuleJNDIName);

      }

      logger.info("Exit getUserModule()");

      return userModule;

      }



      protected RoleModule getRoleModule() throws NamingException

      {

      logger.info("Enter getRoleModule()");

      if (roleModule == null)

      {

      roleModule = (RoleModule)new InitialContext().lookup(roleModuleJNDIName);

      }

      logger.info("Enter getRoleModule()");

      return roleModule;

      }



      protected UserProfileModule getUserProfileModule() throws NamingException

      {

      logger.info("Enter getUserProfileModule()");

      if (userProfileModule == null) {

      userProfileModule = (UserProfileModule) new InitialContext()

      .lookup(userProfileModuleJNDIName);

      }

      logger.info("Enter getUserProfileModule()");

      return userProfileModule;

      }



      protected MembershipModule getMembershipModule() throws NamingException

      {

      logger.info("Enter getMembershipModule()");

      if (membershipModule == null)

      {

      membershipModule = (MembershipModule)new InitialContext().lookup(membershipModuleJNDIName);

      }

      logger.info("Enter getMemgershipModule()");

      return membershipModule;

      }





      /**

      * Validate if password is valid. Always return true.

      * @param inputPassword Input password

      * @param expectedPassword Expected password

      * @return true

      */

      protected boolean validatePassword(String inputPassword,

      String expectedPassword) {

      logger.info("returning from validatePassword.");

      boolean b = super.validatePassword(inputPassword, expectedPassword);

      logger.info("Login OK?="+b);

      return true;

      }



      /**

      * Get the current HTTP request object.

      * @return current HttpServletRequest object

      * @throws PolicyContextException if error in retrieving the request object

      */

      protected HttpServletRequest getHttpServletRequest()

      throws PolicyContextException {

      return (HttpServletRequest)

      PolicyContext.getContext(POLICY_CONTEXT_HTTP_SERVLET);

      }



      /**

      * Check if a String is null, or empty.

      * @param string String to be checked

      */

      protected static boolean isNullOrEmpty(String string) {

      if (string == null || "".equals(string.trim())) {

      return true;

      }

      return false;

      }



      private void fillContextWithUserProfile(UserProfileModule userProfile,

      User user) throws Exception {

      logger.info("Enter fillContextWithUserProfile()");

      HttpServletRequest request = getHttpServletRequest();

      DelegateContext ctx = new DelegateContext();

      ctx.put("userid", user.getId().toString());

      ctx.put("GIVENNAME", request.getHeader(Constant.SSO_FIRST_NAME));

      ctx.put("FAMILYNAME", request.getHeader(Constant.SSO_LAST_NAME));

      ctx.put("REALEMAIL", request.getHeader(Constant.SSO_EMAIL));

      ctx.put("LOCATION", request.getHeader(Constant.SSO_COUNTRY));

      Locale locale = request.getLocale();

      LocalizedString localizedString = new LocalizedString(locale);

      logger.info("###########The Locale Value is --------------------> " + localizedString.getString(locale,true));

      if ( null != localizedString.getString(locale,true)){

      putNonEmptyProperty(userProfile, user, User.INFO_USER_LOCALE, localizedString.getString(locale,true));

      }

      putNonEmptyProperty(userProfile, user, Constant.INFO_USER_TIME_ZONE_OFFSET,

      Constant.DEFAULT_TIME_ZONE);

      logger.info ("Exit fillContextWithUserProfile()");

      }



      /**

      * Add key value pairs properties into User object.

      * @param user User object

      * @param key Key of property

      * @param value Value of property

      */

      private void putNonEmptyProperty(UserProfileModule userProfileModule, User user, String key, String value) {

      logger.info("Enter putNonEmptyProperty()");

      if ((value != null) && (value.trim().length() != 0)) {

      try{

      userProfileModule.setProperty(user, key, value);

      }catch(IdentityException Ie){

      Ie.printStackTrace();

      }

      }

      logger.info("Exit putNonEmptyProperty()");

      }



      } // end of class







      I am getting these log messages when i tried to login to the portal



      Initializing login module.

      User module JNDI = java:/portal/UserModule

      Role module JNDI = java:/portal/RoleModule

      UserProfile module JNDI = java:/portal/UserProfileModule

      Membership module JNDI = java:/portal/MembershipModule

      Exit Initialize()

      Inside login.

      Inside createUserIfNew.

      Username = 501157612

      Before getting user from UserModule.

      Enter getUserModule()

      Exit getUserModule()

      Create new user 501157612

      501157612,80C398B9-8DE3-163B-AC22-0003BA1BAD40

      Just about to create User

      org.jboss.portal.common.transaction.NestedException: java.lang.NullPointerException

      at org.jboss.portal.common.transaction.Transactions.apply(Transactions.java:253)

      at org.jboss.portal.common.transaction.Transactions.required(Transactions.java:289)

      at com.ge.health.jboss.portal.security.jaas.SsoLoginModule.createUserIfNew(Unknown Source)


      I knew nothing about this exception, I was not able to move further, can any one help me to sortout this issue.