cancel
Showing results for 
Search instead for 
Did you mean: 

How to get a download URL using webscripts

ltardioli
Champ in-the-making
Champ in-the-making
Hello people!

I want to write a webscript that generates a PDF file and return its URL so the user could download it;

The problem is that when I get the URL using: url.getServer()+url.context+node.url, Alfresco asks the user to log in again.

I'm using user authentication in description file, so the user have to authenticate before run the webscript.

Someone knows how to do that without two authentications.
3 REPLIES 3

steven_okennedy
Star Contributor
Star Contributor
Hi Itardioli,

It sounds like you're accidentally using 2 authentication mechanisms. How are you accessing the the webscript, is it through /alfresco/service/… (or /alfresco/s/… which is equivalent)  or /alfresco/wcservice/… (/alfresco/wcs/…)

The first bypasses the authentication chain and uses basic authentication as far as I know. The second one uses the mechanisms defined in the chain, including SSO if you have it configured.

The node.url function will produce a link that looks like /d/a/<node ref>/… which uses the download servlet rather and also pays attention to the auth chain. 

I think if you use the /alfresco/wcs/ it should avoid the double authentication problem

Regards

Steven

rjohnson
Star Contributor
Star Contributor
I would suggest that you modify the webscript that generates the PDF to be a webscript that returns content to a browser window.

You need to define your webscript as a special kind as shown below

<webscript kind="org.alfresco.repository.content.stream">


and then you return your PDF content as follows:-


function main(){
    var nodeRef = args.noderef;
    // get the node from the reference
    if(nodeRef) {
       var doc = search.findNode(nodeRef);
       // Get the kids
       for each (node in doc.getChildren()) {
          if(node.name == "pdfExport") {
             var pdfNode = node;
             break;
          }
       }
       // One of them must have a name cm:pdfExport or you lash it
       if (pdfNode)
       {
          model.contentNode = pdfNode;
       }
       else
       {
          status.code = 400;
          status.message = "Node " + nodeRef + " does not have a PDF rendition";
          status.redirect = true;
       }
    } else {
      status.code = 404;
      status.message = "Node " + nodeRef + " does not exist";
      status.redirect = true;       
    }
}

main();


Bob

ltardioli
Champ in-the-making
Champ in-the-making
Thanks a lot Steven and Bob!
Both solution worked here and it will help me a lot on another problems!!

I really didnt know about those kind of authentication and about to change the kind of the webscript!