3
votes

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
Can you add code on how the DLFileEntry is delegated to the FileEntryAdapter? - David
Are you trying to access this Document Library via a service? Also what version of Liferay are you using? - Jonny

3 Answers

3
votes
DLFileEntryLocalServiceUtil.getFile(userId, fileEntryId, version, incrementCounter) 

gives you the File. Did you try using this?

0
votes

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?

0
votes

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