How to invoke Nuxeo services by local Java API?

I'm trying to invoke Nuxeo Services by local API, but I obtain NullPointerException.

I see in the documentation,

How to invoke services:

The services can be local (inside the same JVM) and most of them can be Also called remotely via RMI if your target Application Server supports EJB3 remoting.

I would like to call upon the services of Nuxeo from “local”, ie with a java client within the same JVM of Nuxeo. This need arises from having to perform operations that are not supported by remote clients documented. For example, do the tagging of a document. I tried to write the following code but I obtain a NullPointerException. Probably, I'm missing some basic concept.

I wrote java code in a jsp: **<%

    LocalSessionFactory lsf = new LocalSessionFactory();
    CoreSession session = lsf.getSession();

    TagService tsi = Framework.getService(TagService.class);
    tsi.tag(session, "2b1eefa4-81ee-4d0f-8bcb-24f5e9745f96", "trytag","Administrator");

%>**

Exception stacktrace logs:

*143: CoreSession csession = lsf.getSession(); 144: 145: TagService tsi = Framework.getService(TagService.class); 146: tsi.tag(csession, “2b1eefa4-81ee-4d0f-8bcb-24f5e9745f96”,“trytag”,“Administrator”); 147: 148: 149: Stacktrace:

    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.nuxeo.wss.servlet.BaseWSSFilter.doFilter(BaseWSSFilter.java:137)
    at org.nuxeo.wss.servlet.FailSafeWSSFilter.doFilter(FailSafeWSSFilter.java:55)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.nuxeo.ecm.platform.ui.web.rest.FancyURLFilter.doFilter(FancyURLFilter.java:129)
    ... 24 more

Caused by: java.lang.NullPointerException

    at org.nuxeo.ecm.core.api.local.LocalSession.getPrincipal(LocalSession.java:164)
    at org.nuxeo.ecm.core.api.UnrestrictedSessionRunner.isUnrestricted(UnrestrictedSessionRunner.java:103)
    at org.nuxeo.ecm.core.api.UnrestrictedSessionRunner.<init>(UnrestrictedSessionRunner.java:56)
    at org.nuxeo.ecm.platform.tag.TagServiceImpl$UnrestrictedAddTagging.<init>(TagServiceImpl.java:131)
    at org.nuxeo.ecm.platform.tag.TagServiceImpl.tag(TagServiceImpl.java:115)
    at org.apache.jsp.prova_jsp._jspService(prova_jsp.java:220)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    ... 34 more*

Thank you!

0 votes

1 answers

1011 views

ANSWER



The session you use is not associated with an authenticated login, it has no principal. You should retrieve it from the context or the request. Also give a look at CoreInstance rather than a direct call to LocalSessionFactory.

1 votes