Liferay: how to get the current login user details in servlet?

拈花ヽ惹草 提交于 2019-12-25 08:51:03

问题


I'm completely new to Liferay. I have configured Orbeon Forms in Liferay by using the Proxy Portlet, finally I created an Orbeon form and sent the form data to a demo portlet (custom portlet). In the portlet I have created a servlet. If user save the orbeon form data then my servlet getting called and I'm able to get the form data. Now I need to get the current user name or userid in the servlet.

In form builder I have send the orbeon form data to my servlet.

properties-local.xml

    <property
  as="xs:string"
  name="oxf.fr.detail.process.send.*.*"
  value='require-valid
         then send(uri = "http://localhost:9090/FRunner-portlet/html/jsp/formData.jsp?username={xxf:get-request-header('Orbeon-Username')}", method="POST", content="metadata")
                 then success-message("save-success")
                 recover error-message("database-error")'/>

If I tried the above code I got the following errors,

SEVERE: Exception sending context initialized event to listener instance of class org.orbeon.oxf.webapp.OrbeonServletContextListener
javax.servlet.ServletException: org.orbeon.oxf.common.ValidationException: line 80, column 122 of oxf:/config/properties-local.xml: Fatal error: Element type "property" must be followed by either attribute specifications, ">" or "/>".
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextInitialized$2.apply(OrbeonServletContextListener.scala:39)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextInitialized$2.apply(OrbeonServletContextListener.scala:39)
        at org.orbeon.oxf.util.ScalaUtils$.withRootException(ScalaUtils.scala:87)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener.contextInitialized(OrbeonServletContextListener.scala:39)


Caused by: org.orbeon.oxf.common.ValidationException: line 80, column 122 of oxf:/config/properties-local.xml: Fatal error: Element type "property" must be followed by either attribute specifications, ">" or "/>".
        at org.orbeon.oxf.xml.XMLParsing$ErrorHandler.fatalError(XMLParsing.java:215)
        at orbeon.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at orbeon.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at orbeon.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at orbeon.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at orbeon.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)


SEVERE: Exception sending context destroyed event to listener instance of class org.orbeon.oxf.webapp.OrbeonServletContextListener
javax.servlet.ServletException: java.lang.NullPointerException
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextDestroyed$2.apply(OrbeonServletContextListener.scala:44)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextDestroyed$2.apply(OrbeonServletContextListener.scala:44)
        at org.orbeon.oxf.util.ScalaUtils$.withRootException(ScalaUtils.scala:87)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener.contextDestroyed(OrbeonServletContextListener.scala:44)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4819)
        at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5466)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)


Caused by: java.lang.NullPointerException
        at org.orbeon.oxf.pipeline.InitUtils$.org$orbeon$oxf$pipeline$InitUtils$$fromProperty$1(InitUtils.scala:195)
        at org.orbeon.oxf.pipeline.InitUtils$.processorDefinitions$lzycompute(InitUtils.scala:196)
        at org.orbeon.oxf.pipeline.InitUtils$.processorDefinitions(InitUtils.scala:179)
        at org.orbeon.oxf.webapp.Orbeon$.initialize(Orbeon.scala:84)
        at org.orbeon.oxf.webapp.OrbeonWebApp$$anonfun$1.apply(WebAppContext.scala:117)
        at org.orbeon.oxf.webapp.OrbeonWebApp$$anonfun$1.apply(WebAppContext.scala:117)
        at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:189)
        at org.orbeon.oxf.webapp.ParametersAndAttributes$$anon$1.getOrElseUpdate(WebAppContext.scala:93)
        at org.orbeon.oxf.webapp.OrbeonWebApp$class.$init$(WebAppContext.scala:117)

Update

xxf:get-request-header('orbeon-liferay-user-email')

With the above statement I am able to get the liferay login user mail id. Now I need to pass this username to my portlet as a parameter. Can you please let me know what is the procedure to pass the mailid to my portlet. I tried in different ways but it is not happen. Please suggest me something to send the liferay user mail id to my portlet.

FormData Servlet code

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();                        
    try {                       
        DataInputStream in = new DataInputStream (request.getInputStream());
        StringBuffer buffer = new StringBuffer();
           int value;
           while ((value=in.read()) != -1) {
               buffer.append((char)value);
            }
           String formData =  buffer.toString();
           System.out.println("Form Data==========>"+ formData);
    } catch (Exception e) {                   
      System.out.println("ERROR2=====>"+e);
    }                                             
}

How to get the current user details when servlet getting called?


回答1:


One way is to explicitly pass it to the URL of the send action, for example:

uri = ".../FormData?username={xxf:get-request-header('Orbeon-Username')}"

In Orbeon Forms 4.9, there is a new xxf:username() function which is more direct.

On the servlet side, you can retrieve a URL parameter using:

request.getParameter("username")


来源:https://stackoverflow.com/questions/29746692/liferay-how-to-get-the-current-login-user-details-in-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!