Param1414086
Param1414086

Reputation: 183

Programmatically downloading image from CQ5

I am trying to programmatically download a image from CQ5.I have made a link which when clicked should download a image.To do this I have made a ajax call to a servlet ,for whenever user clicks on the image,we should get a pop of open and save dialog. This is the code I have written in servlet to download the image.

**response.setContentType("image/png");
response.setHeader("Content-Disposition", "attachment; filename=icon" + ".png");
URL url = new URL("http://somehost:portnmuber/content/dam/image.jpg");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();**

I am continuously getting 401 error. Is there some another way that I can do this.

Thanks,in advance.

Upvotes: 3

Views: 1821

Answers (4)

Matt Bearson
Matt Bearson

Reputation: 1033

I had a similar issue trying to download images from AEM to iOS devices. It turns out AEM (at least the instance I'm hitting) requires a referer header; if absent the request is rejected. The problem did not surface with the simulator, just with real devices. This did the trick:

[request setValue: @"https://www.example.com/" forHTTPHeaderField: @"Referer"];

Upvotes: 0

Alexander Drobyshevsky
Alexander Drobyshevsky

Reputation: 4247

You are gettin 401 error because anonymous user permissions for the DAM are not set. At first, you should grant access permissions for 'anonymous' users for node '/content/dam'. You can do it from the page: "http://somehost:portnuber/useradmin" . Doble click on user 'anonymous' and go to the tab 'permissions' (on the window right). Check read permissions for 'dam' node.

Secondly, go to the system console, components tab (http://somehost:portnmuber/system/console/components) and find org.apache.sling.engine.impl.auth.SlingAuthenticator component. Click to 'configure' button. In the component configuration check 'Allow Anonymous Access' and press 'Save'.

Should work.

Upvotes: 1

Michael Shopsin
Michael Shopsin

Reputation: 2138

CQ5 uses sessions so downloading from a path requires a username/password on the author instance, for example curl -u username:password http://someserver.com:4502/etc/packages/my_packages/package.zip -o localpath/package.zip would download a package based on someone who had permissions to read it. For images you need the image path something like http://someserver.com:4502/content/dam/640x960.jpg to get the image, but you would need to add /jcr:content/renditions/original to get the original e.g. http://someserver.com:4502/content/dam/640x960.jpg/jcr:content/renditions/original. You can also change the image path to get a specific rendition of the image, for example http://someserver.com:4502/content/dam/640x960.jpg/jcr:content/renditions/cq5dam.thumbnail.140.100.png gets a 140x100 version of the image.

Upvotes: 0

Bayani Portier
Bayani Portier

Reputation: 660

Firstly, have a look at the download component in the foundation libs: /libs/foundation/components/download/download.jsp

Secondly, if you don't have a session open with CQ5, it will by default give you a 401 error. I am assuming you are in a generated container for CQ5, or are you just randomly accessing from another domain?

Note that even for anonymous access, CQ will still establish an anonymous session, and give you a login token. Anonymous is effectively still authenticated, just without the requirement of a username and password.

Upvotes: 5

Related Questions