cancel
Showing results for 
Search instead for 
Did you mean: 

Serving binaries (PDF/PPT/GIF...)

flefoll
Champ in-the-making
Champ in-the-making
Hi

(Im' posting on French and English forum, and I will copy/paste answers for all).

The following webscript is fully working for serving XML content, but doesn't for binary content (like pdf/ppt or else..)

view.get.desc.xml
<webscript>
        <shortname>View</shortname>
        <description>View file</description>
        <url>/view/{path}</url>
        <authentication runas="admin">none</authentication>
        <transaction>required</transaction>
</webscript>

view.get.js
// locate folder by path
// NOTE: only supports path beneath company home, not from root
logger.log("Start View script. Requested resource : " + url.extension);
var alfrescoCompanyHomeFolder = "Company Home";
logger.log("Alfresco company folder name : " + alfrescoCompanyHomeFolder);
var baseFolderPath = "My_Folder";
logger.log("Base folder path : " + baseFolderPath);
var completePath = (alfrescoCompanyHomeFolder + "/" + baseFolderPath + "/" + url.extension).replace(new RegExp("_SPACE_","g"), " ")
;
logger.log("Real requested path : " + completePath);
var requestFile = roothome.childByNamePath(completePath);
if (requestFile == undefined) {
        logger.log("Requested resource is not found");
    status.code = 404;
    status.message = "Requested resource is not found";
    status.redirect = true;
} else if (requestFile.isContainer) {
        logger.log("Browsing is not allowed");
    status.code = 403;
    status.message = "Browsing is not allowed";
    status.redirect = true;
} else {
        logger.log("Rendering selected file");
        model.requestFile = requestFile;
}

view.get.xml.ftl
${requestFile.content}

What should I put in view.get.pdf.ftl or in view.get.ppt.ftl to have the same behaviour ?

MimetTypes are already extended on webscript-framework-application-context level.

Thanks in advance,

Francois.
3 REPLIES 3

flefoll
Champ in-the-making
Champ in-the-making
I found the answer myself after two days…

It may serv others, find it here.

After having done the modification explained there : https://issues.alfresco.com/jira/browse/ETHREEOH-1630

view.get.desc.xml
<webscript kind="org.alfresco.repository.content.stream">
        <shortname>Webview rendering</shortname>
        <description>Render selected file
        </description>
        <url>/view/{path}</url>
        <format default="">argument</format>
        <authentication runas="admin">none</authentication>
        <transaction>required</transaction>
</webscript>

webview.get.js

// locate folder by path
// NOTE: only supports path beneath company home, not from root
logger.log("Start webview script. Requested resource : " + url.extension);
var alfrescoCompanyHomeFolder = "Company Home";
logger.log("Alfresco company folder name : " + alfrescoCompanyHomeFolder);
var baseFolderPath = "My_Folder";
logger.log("Base folder path : " + baseFolderPath);
var completePath = (alfrescoCompanyHomeFolder + "/" + baseFolderPath + "/" + url.extension).replace(new RegExp("_SPACE_","g")
, " ");
logger.log("Real requested path : " + completePath);
var requestFile = roothome.childByNamePath(completePath);
if (requestFile == undefined) {
        logger.log("Requested resource is not found");
    status.code = 404;
    status.message = "Requested resource is not found";
    status.redirect = true;
} else if (requestFile.isContainer) {
        logger.log("Browsing is not allowed");
    status.code = 403;
    status.message = "Browsing is not allowed";
    status.redirect = true;
} else {
        logger.log("Rendering selected file");
        model.contentNode = requestFile;
}

Bye

jhsu9
Champ in-the-making
Champ in-the-making
Hello,

Would anyone have a Java example on how to call a content streaming webscript and save the binary content being streamed back to a Java file? 

I've tried with Apache HttpComponents and have not yet figured out a way to get a response back in binary to save it to a file.  I am able to get the response body as a String, but for the webscript that I followed from the wiki example (http://wiki.alfresco.com/wiki/3.0_Web_Scripts_Kinds#Using_a_ContentStream_Web_Script), it's expecting binary data to be returned.

When I invoke the webscript from browser I get this error which I believe is fine, since it doesn’t have a response template (as directed by wiki above).
java.lang.ClassCastException - java.lang.String cannot be cast to org.alfresco.service.cmr.repository.NodeRef
   
   org.alfresco.repo.web.scripts.content.StreamContent.execute(StreamContent.java:180)
   org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:383)
   org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
   org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
   org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
   org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
   org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:351)
   org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:188)
   org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
   org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   java.lang.Thread.run(Thread.java:662)

Here is a snippet using HttpComponents:
      String url = "http://localhost:8080/alfresco/service/gov/va/vbmap/getContentStream?";
      String nodeRef = "workspace://SpacesStore/2fb74712-3eea-48c2-a0c9-32c419154701";

      String alf_ticket = "TICKET_4071d448ce4773e531c2d3b4d1878628c37b61b3";
      url += "alf_ticket=" + alf_ticket + "&nodeRef=" + nodeRef;

      HttpClient httpclient = new DefaultHttpClient();
      try {
         HttpGet httpget = new HttpGet(url);

         System.out.println("executing request " + httpget.getRequestLine());

         // Create a response handler to get the JSON response from the webscript
         ResponseHandler<String> responseHandler = new BasicResponseHandler();
         String responseBody = httpclient.execute(httpget, responseHandler);
         System.out.println("—————————————-");
         System.out.println(responseBody);
         System.out.println("—————————————-");

         // File file = new File(); // if (!file.exists()) { //
         System.out.println("File does not exist"); // return; // }

      } catch (Exception e) {
         System.out.println("error");
         e.printStackTrace();
      } finally {
         try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
      }

Thanks and appreciate the help.
Judy

jhsu9
Champ in-the-making
Champ in-the-making
Hello,

Figured it out!  Always helps to talk to someone Smiley Happy

1. I had a small bug in my webscript js controller, needed to return the node to model.contentNode.
correct snippet:
       var docNode = search.findNode(args_nodeRef);

       // if docNode not found, can't do anything!
       if (docNode == null) {
          throw "NodeReference does not exist.";
       }
       
       model.contentNode = docNode;

2.  Needed to use HttpResponse in the Java code to grab the binary data.  ClientContentStreamGet.writeFile gets the entity contents, and writes the binary to a file on disk.
correct snippet:
      String url = "http://localhost:8080/alfresco/service/gov/va/vbmap/getContentStream?";

      // txt: "workspace://SpacesStore/2fb74712-3eea-48c2-a0c9-32c419154701"
      // pdf: "workspace://SpacesStore/de70833b-73bd-49be-8ce2-b1a0111f3f70"
      String nodeRef = "workspace://SpacesStore/de70833b-73bd-49be-8ce2-b1a0111f3f70";

      String alf_ticket = "TICKET_4071d448ce4773e531c2d3b4d1878628c37b61b3";
      url += "alf_ticket=" + alf_ticket + "&nodeRef=" + nodeRef;

      HttpClient httpclient = new DefaultHttpClient();
      try {
         HttpGet httpget = new HttpGet(url);

         System.out.println("executing request " + httpget.getRequestLine());

         // execute HTTP get
         HttpResponse response = httpclient.execute(httpget);

         // Examine the response status
         System.out.println(response.getStatusLine());

         // Get hold of the response entity
         HttpEntity entity = response.getEntity();

         ClientContentStreamGet contentStreamClient = new ClientContentStreamGet();
         contentStreamClient.writeFile(httpget, entity);

      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            httpclient.getConnectionManager().shutdown();
         } catch (Exception ignore) {
         }
      }

Thanks,
Judy