Blob downloading problem with java automation client

I am trying to download all the files (txt, html, pdf, doc, xls, …) from nuxeo. To achieve this, I have found “Java automation client”. (Hope I have made a good choice?)

My starting point is the example from the java automation client documentation: http://doc.nuxeo.com/display/public/NXDOC/Java+Automation+Client

1) Getting all the docs:

        Documents docs = (Documents) session.newRequest("Document.Query").set(
            "query", "SELECT * FROM Document WHERE ecm:primaryType=\'File\' " +
            "OR ecm:primaryType=\'Note\'").execute();

2) With an iteration, get every “doc” from “docs” and get its blob:

        Iterator<Document> it = docs.iterator();
    while (it.hasNext()){
        Document doc = (Document)it.next();
            String path = doc.getPath();
            path = URLEncoder.encode(path, "UTF-8");
        Blob blob = (FileBlob) session.getFile(path);
        try{
            InputStream is = blob.getStream();
            // do something with "is"
        }catch(Exception e){
            e.printStackTrace();
        }
    }

But every time, I get the exception:

Exception in thread "main" java.lang.RuntimeException: Cannot execute {Authorization=Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9y}
at org.nuxeo.ecm.automation.client.jaxrs.impl.HttpConnector.execute(HttpConnector.java:122)
at org.nuxeo.ecm.automation.client.jaxrs.spi.ConnectorHandler.execute(ConnectorHandler.java:30)
at org.nuxeo.ecm.automation.client.jaxrs.spi.DefaultSession.getFile(DefaultSession.java:127)
at Extract.main(Extract.java:79)
Caused by: java.lang.NullPointerException
at org.nuxeo.ecm.automation.client.RemoteException.extractInfo(RemoteException.java:61)
at org.nuxeo.ecm.automation.client.RemoteException.<init>(RemoteException.java:38)
at org.nuxeo.ecm.automation.client.jaxrs.impl.HttpConnector.execute(HttpConnector.java:145)
at org.nuxeo.ecm.automation.client.jaxrs.impl.HttpConnector.execute(HttpConnector.java:118)
... 3 more

I have spent already 2 days on this, and I am making little progress. Please help!!! thanks in advance!!!

0 votes

1 answers

3018 views

ANSWER



Hi,

There are ways to download blobs with Nuxeo automation client:

// Retrieve document note with full schemas - or using Query operation with doc listing
    note = (Document) session.newRequest(DocumentService.FetchDocument).setHeader(
            Constants.HEADER_NX_SCHEMAS, "*").set("value",
            "/path/path").execute();

    PropertyList list = note.getProperties().getList("files:files");

    PropertyMap map = list.getMap(0).getMap("file");
    assertEquals(filename1, map.getString("name"));
    assertEquals("text/xml", map.getString("mime-type"));

    // get the data URL
    String path = map.getString("data");
    blob = (FileBlob) session.getFile(path);

    // the same for the second file
    map = list.getMap(1).getMap("file");
    assertEquals(filename2, map.getString("name"));
    assertEquals("text/xml", map.getString("mime-type"));

    // get the data URL
    path = map.getString("data");
    blob = (FileBlob) session.getFile(path);

You can have several informations on download blobs url pattern here: http://doc.nuxeo.com/display/NXDOC/Downloading+Files

Another way is to use “Blob.Get”, “Blob.GetList”, “Blob.GetAll” operations. Here an example:

// now test the GetDocumentBlobs operation on the note document
    blobs = (Blobs) session.newRequest(GetDocumentBlobs.ID).setInput(note).set(
            "xpath", "files:files").execute();

We're going to improve the way we can download blobs in general.

Thanks for your feedback.

0 votes



thanks for your answer!!! i will let you about the results.
10/28/2013

I tried the two methods. Bun in both methods, the "PropertyMap" I got was always null. So all I got was an exception, at the end.

There were another example in java remote automation page, which uploads a file "/myfile" to the root directory and download it after. Strangely, this example worked fine, which means "PropertyMap" was not a null value.

I got a file in nuxeo like this: "/Text Document/workspaces/myworkspace/PourIvan.txt", which I had uploaded using nuxeo interface. I replaced "/myfile" with the path of this file, and still the "PropertyMap" was a null value…

So Those experiments led me to followoing questions:

1) Is my query is valid to get the files and their "PropertyMap"?

&quot;SELECT * FROM Document WHERE ecm:primaryType=\&apos;File\&apos; &quot; +
        &quot;OR ecm:primaryType=\&apos;Note\&apos;&quot;

2) If we upload a file using nuxeo interface, will it have a "PropertyMap"?

3) I replaced the "/myfile" in the example with "/Text Document/workspaces/myworkspace/myfile", and it gave me an error which says "failed Blob.Attach action"… So is it possible to attach a file to other directories other than root using the same code in the example?

4) I printed the "/myfile" path in the example, and it gave me something like this: files/a8b3556c-2b99-47c3-876d-2e0b9720276d?path=%2Fcontent it seems like "files" + id + ??? … So, how can we construct a file path if we have the file id?

I am restraining myself to those four questions. :) thanks in advance !!!

10/29/2013

@murataht this is not a forum, please don't post questions in the Answers section. Ask another question or update your original question or add a comment to it.
10/29/2013