4 Replies Latest reply on May 7, 2008 1:32 AM by YOGESH SRINIVASAN

    How can I get current portal user from javax.portlet.Portlet

    Charles Woo Newbie

      hi ,
      i want to obtain the current user object from my prtlet based on struts2 portlet,but I can only get javax.portlet.PortletContext use

      PortletActionContext.getPortletContext()

      in referenceGuide of Jboss Portal, it says
      The org.jboss.portal.api.PortalRuntimeContext gives access to state or operations associated at runtime with the current user of the portal. The String getUserId() retrieve the user id and can return null if no user is associated with the context. It also gives access to the PortalSession instance associated with the current user. Finally it gives access to the NavigationalStateContext associated with the current user.


      but how can i associate javax.portlet.PortletContext with org.jboss.portal.api.PortalRuntimeContext??

      thank you!!

        • 1. Re: How can I get current portal user from javax.portlet.Por
          Charles Woo Newbie

          Yesterday I can't understand jboss portlet container very well, so I didn'i konw the relationship between javax.portlet.ActionRequest and org.jboss.portlet.JBossActionRequest, but now I get it.

          public class org.jboss.portlet.JBossActionRequest extends org.jboss.portal.portlet.impl.jsr168.api.ActionRequestImpl {
          }


          and implements javax.portlet.ActionRequest interface.

          So we can get current user in struts2 like that:
          ((org.jboss.portlet.JBossActionRequest) PortletActionContext
           .getActionRequest()).getUser();


          • 2. Re: How can I get current portal user from javax.portlet.Por
            sagar c Newbie

            Hi freesoul.
            i am stuck with the same problem of retriving the current logge in user of the portal.I have this code with me,

            --------------------------------------------------------------------------------------
            /*****************************************
            * *
            * JBoss Portal: The OpenSource Portal *
            * *
            * Distributable under LGPL license. *
            * See terms of license at gnu.org. *
            * *
            *****************************************/

            package org.jboss.portlet1.hello;

            import java.io.IOException;

            import javax.portlet.ActionRequest;
            import javax.portlet.ActionResponse;
            import javax.portlet.GenericPortlet;
            import javax.portlet.PortletConfig;
            import javax.portlet.PortletException;

            import javax.portlet.PortletRequestDispatcher;
            import javax.portlet.RenderRequest;
            import javax.portlet.RenderResponse;
            import javax.portlet.UnavailableException;

            import org.jboss.portal.api.PortalRuntimeContext;
            import org.jboss.portal.api.navstate.NavigationalStateContext;
            import org.jboss.portal.api.session.PortalSession;
            import org.jboss.portal.core.controller.ControllerContext;




            public class HelloWorldJSPPortlet extends GenericPortlet implements PortalRuntimeContext
            {
            private ControllerContext ctx;
            private PortalSession psession;
            private String userId;
            /*private UserProfileModule userProfileModule;*/

            /*@Override
            public void init(PortletConfig config) throws PortletException {
            // TODO Auto-generated method stub
            super.init(config);
            userProfileModule = (UserProfileModule)getPortletContext().getAttribute("UserProfileModule");
            }*/

            protected void doView(RenderRequest rRequest, RenderResponse rResponse) throws PortletException, IOException, UnavailableException
            {
            rResponse.setContentType("text/html");

            //PortalRuntimeContext context=(PortalRuntimeContext)this.getPortletContext();
            //userId=context.getUserId();
            //ControllerContext ctx;

            //userId=getUserId();

            //System.out.println("user:"+userId);

            String sYourName = (String) rRequest.getParameter("yourname");

            if(sYourName != null)
            {
            rRequest.setAttribute("yourname", sYourName);
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/view2.jsp");
            prd.include(rRequest, rResponse);
            }
            else
            {
            //rRequest.setAttribute("yourname", userId);
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/view.jsp");
            prd.include(rRequest, rResponse);
            }
            }



            public void processAction(ActionRequest aRequest, ActionResponse aResponse) throws PortletException, IOException, UnavailableException
            {
            String sYourname = (String) aRequest.getParameter("yourname");

            // do something

            aResponse.setRenderParameter("yourname", sYourname);
            }

            protected void doHelp(RenderRequest rRequest, RenderResponse rResponse) throws PortletException, IOException, UnavailableException
            {
            rResponse.setContentType("text/html");
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/help.jsp");
            prd.include(rRequest, rResponse);
            }

            protected void doEdit(RenderRequest rRequest, RenderResponse rResponse) throws PortletException, IOException, UnavailableException
            {
            rResponse.setContentType("text/html");
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/edit.jsp");
            prd.include(rRequest, rResponse);
            }

            public NavigationalStateContext getNavigationalStateContext() {
            // TODO Auto-generated method stub
            return null;
            }

            public PortalSession getSession() {
            // TODO Auto-generated method stub
            return null;
            }

            public String getUserId() {
            // TODO Auto-generated method stub
            //((org.jboss.portlet.JBossActionRequest) PortletContext
            // .getActionRequest()).getUser();
            return null;


            }


            }
            -----------------------------------------------------------------------------
            This is the sample code provided in helloworldjspportlet zip here---

            I need to get the current user of the system to display a jsp page in the portlet. According to docs ,i tried to implement the PortalRuntimeContext interface , but no success.....

            You seem to be successful in it.however i am not using the struts framework. is it possible to retrieve the current user ?

            waiting for reply.

            thanks
            csagar

            • 3. Re: How can I get current portal user from javax.portlet.Por
              Alaaeldin El-Nattar Newbie

              Hi,

              request.getRemoteUser() should give you the currently logged in user or null if not logged in. You could use the UserModule or UserProfileModule JNDI entries to get the necessary user information.

              Regards,
              Shagul

              • 4. Re: How can I get current portal user from javax.portlet.Por
                YOGESH SRINIVASAN Newbie

                Hi,

                You can use the below mentioned API to get the currently logged in user

                renderRequest.getUserPrincipal().getName();

                or

                actionRequest.getUserPrincipal().getName();