Liferay from fileentry to java file object

8.2k Views Asked by At

Anyone know how to retrieve a contentStream from a DLFileEntry (http://docs.liferay.com/portal/6.0/javadocs/com/liferay/portlet/documentlibrary/model/DLFileEntry.html) using an httpservlet?

I tried using DLFileEntry.getContentStream() but it fails giving me the error

com.liferay.portal.security.auth.PrincipalException: PermissionChecker not initialized

I solved in part that problem setting hardcode my userId:

long userId=2

            PrincipalThreadLocal.setName(userId);

            User user = UserLocalServiceUtil.getUserById(userId);

            PermissionChecker permissionChecker;

                permissionChecker = PermissionCheckerFactoryUtil.create(user, false);

            PermissionThreadLocal.setPermissionChecker(permissionChecker);

The problem of this solution is how to get the real userId, and how happen if the user is a guest?

I tried Long.parseLong(req.getRemoteUser()); but fail also.

3

There are 3 best solutions below

3
Jonny On

If you're using Liferay 6.1 then the following code will get you a InputStream for the That DLFileEntry.

InputStream inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(fileEntry.getUserId(), fileEntry.getFileEntryId(), fileEntry.getVersion());

Howwever the error you're getting is seems to be due you not authenticating with Liferay before making this call to the server.

How are you calling this code?

1
Ravi Kumar Gupta On
DLFileEntryLocalServiceUtil.getFile(userId, fileEntryId, version, incrementCounter) 

gives you the File. Did you try using this?

2
Felix Christy On

Please use PermissionThreadLocal.getPermissionChecker() to get the permissionChecker object of the current logged in user with current state. If the user is guest, still you will get the permissionChecker object.

-Felix