PrincipalException: Principal is null

Viewed 628

(Note: Due to lack of answers I also posted the same question to the Liferay forum)

I am trying to create a file entry in Liferay 7:

DLAppServiceUtil.addFileEntry(
    34613, // groupId
    0, // folderId
    "hello.txt",
    "text/plain",
    "title",
    "description",
    "changeLog",
    new File("hello.txt"),
    new ServiceContext()
);

Result:

com.liferay.portal.kernel.security.auth.PrincipalException: Principal is null
    at com.liferay.portal.kernel.service.BaseServiceImpl.getUserId(BaseServiceImpl.java:95)
    at com.liferay.portlet.documentlibrary.service.impl.DLAppServiceImpl.addFileEntry(DLAppServiceImpl.java:292)
    at com.liferay.portlet.documentlibrary.service.impl.DLAppServiceImpl.addFileEntry(DLAppServiceImpl.java:202)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:163)
    at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:54)
    at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:58)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
    at com.liferay.portal.service.ServiceContextAdvice.invoke(ServiceContextAdvice.java:51)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:56)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:56)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:56)
    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
    at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:169)
    at com.sun.proxy.$Proxy129.addFileEntry(Unknown Source)
    at com.liferay.document.library.kernel.service.DLAppServiceUtil.addFileEntry(DLAppServiceUtil.java:235)

The exception is thrown by this part of Liferay's source code:

public long getUserId() throws PrincipalException {
    String name = PrincipalThreadLocal.getName();
    if (Validator.isNull(name)) {
        throw new PrincipalException("Principal is null");
    }

Should I try to perform a PrincipalThreadLocal.setName before calling that method? That sounds risky, so I am sure there is a better way? (actually I tried, that leads to a further PrincipalException: PermissionChecker not initialized)

0 Answers
Related