Liferay:如何在servlet中获取当前登录用户的详细信息?

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

我是 Liferay 的新手。我已经使用 Proxy Portlet 在 Liferay 中配置了 Orbeon Forms,最后我创建了一个 Orbeon 表单并将表单数据发送到演示 portlet(自定义组件)。在 portlet 中,我创建了一个 servlet。如果用户保存 orbeon 表单数据然后我的 servlet 被调用并且我能够获取表单数据。现在我需要获取 servlet 中的当前用户名或用户 ID。

在表单生成器中,我已将 orbeon 表单数据发送到我的 servlet。

属性-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")'/>

如果我尝试上面的代码,我得到以下错误,

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.apply(OrbeonServletContextListener.scala:39)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextInitialized.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.apply(OrbeonServletContextListener.scala:44)
        at org.orbeon.oxf.webapp.OrbeonServletContextListener$$anonfun$contextDestroyed.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(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.apply(WebAppContext.scala:117)
        at org.orbeon.oxf.webapp.OrbeonWebApp$$anonfun.apply(WebAppContext.scala:117)
        at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:189)
        at org.orbeon.oxf.webapp.ParametersAndAttributes$$anon.getOrElseUpdate(WebAppContext.scala:93)
        at org.orbeon.oxf.webapp.OrbeonWebApp$class.$init$(WebAppContext.scala:117)

更新

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

通过上面的语句我可以得到liferay登录用户的邮件id。现在我需要将这个用户名作为参数传递给我的 portlet。您能否告诉我将 mailid 传递到我的 portlet 的过程是什么?我尝试了不同的方式,但没有发生。请给我一些建议,将 liferay 用户邮件 ID 发送到我的 portlet。

FormData Servlet 代码

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);
    }                                             
}

调用servlet时如何获取当前用户的详细信息?

一种方法是显式传递给send动作的URL,例如:

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

在 Orbeon Forms 4.9 中,有一个新的 xxf:username() 功能更直接。

在 servlet 端,您可以使用以下方法检索 URL 参数:

request.getParameter("username")