4

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.

Sabrina
  • 267
  • 5
  • 18
  • Can you add code on how the DLFileEntry is delegated to the FileEntryAdapter? – David May 30 '12 at 06:18
  • Are you trying to access this Document Library via a service? Also what version of Liferay are you using? – Jonny May 30 '12 at 07:09

3 Answers3

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

gives you the File. Did you try using this?

Ravi Kumar Gupta
  • 1,698
  • 2
  • 22
  • 38
  • 1
    This will only give you the file if your DocumentLibrary implementation supports it. I know that S3 & JCR don't support this method, and will throw "Unsupported Operation Exceptions" if you try to use it in Liferay 6.1. – Jonny May 30 '12 at 13:27
0

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?

Jonny
  • 2,663
  • 1
  • 24
  • 24
  • But how are you calling this code? In a portlet? Is the user running it logged in or a guest? Is it being accessed externally? I've never needed to use a PermissionCheck to do the above, but it does require a Service Context to get the DLFileEntry. – Jonny May 31 '12 at 15:37
  • I create a servlet to download zip file, It works but I need to set the permission of my user in the code.. I think is very strange.. – Sabrina May 31 '12 at 16:26
  • By servlet do you mean Portlet? – Jonny May 31 '12 at 18:35
0

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

Felix Christy
  • 2,179
  • 1
  • 19
  • 32
  • Hi Felix, I'm sorry but your solution doesn't work it still give the same error.. – Sabrina May 31 '12 at 13:06
  • Hi Sabrina, I would suggest you to use PortletServlet instead of HttpServlet, when you use PortletServlet, you will have a portlet context, and from there you will get portlet request, portlet session and hopefully permission checker part. – Felix Christy Jun 04 '12 at 06:52