cancel
Showing results for 
Search instead for 
Did you mean: 

New to web script...please help

vjayabalan
Champ in-the-making
Champ in-the-making
Hello everyone,

I am new to web scripts. I am trying to create a folder under Company Home in my Alfresco repository. I am using the following code to execute a POST method that would create the folder:

String url = "http://localhost:11080/alfresco/service/cmis/pCompany%20Home/children"
PostMethod method = new PostMethod(url)
String filePath = "C:/testDoc.xml";
String contentType = "application/atom+xml;type=entry";
      
File upload = new File(filePath);
method.setRequestHeader("name", upload.getName());
method.setRequestHeader("Content-type", contentType);
      
method.setRequestBody(new FileInputStream(upload));
try{
      int respCode = client.executeMethod(method)
}
catch(IOException io){
      io.printStackTrace()
}
catch(HttpException hte){
      hte.printStackTrace()
}


The testDoc.xml looks like this (I found it online Smiley Happy):

<?xml version='1.0' encoding='utf-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>
<title>VJ Folder</title>
<summary>VJ's folder</summary>
<content type="text/html">${CONTENT}</content>
<cmisSmiley Surprisedbject>
<cmisSmiley Tongueroperties>
<cmisSmiley TongueropertyString cmis:name='ObjectTypeId'><cmis:value>folder</cmis:value></cmisSmiley TongueropertyString>
</cmisSmiley Tongueroperties>
</cmisSmiley Surprisedbject>
</entry>

When I run the code, I get the following exception. I don't want to bore you with the complete error message, but here is a snippet:

<table>
            <tr><td><b>404 Description:</b></td><td> Requested resource is not available.</td></tr>
            <tr><td> </td></tr>
            <tr><td><b>Message:</b></td><td>Cannot find object for ObjectPathReference[storeRef=workspace://SpacesStore,path=/app:company_homeCompany Home]</td></tr>
            <tr><td><b>Server</b>:</td><td>Community v3.4.0 (d 3370) schema 4,113</td></tr>
            <tr><td><b>Time</b>:</td><td>Aug 4, 2011 5:10:14 PM</td></tr>
            <tr><td></td><td> </td></tr>
            <tr><td><b>Diagnostics</b>:</td><td><a href="/alfresco/service/script/org/alfresco/cmis/children.post">Inspect Web Script (org/alfresco/cmis/children.post)</a></td></tr>
         </table>

I don't know what I am doing wrong here. Please help!
9 REPLIES 9

jpotts
World-Class Innovator
World-Class Innovator
Before we work on your code, can you give a brief summary of what you are trying to do? I'll go ahead and take a swing at it based on what's in your message, but a little bit of context would help…

This looks like Java code and you are using the CMIS ATOM Pub binding, so I am assuming you are in a remote Java application that needs to create a folder in the Alfresco repository. If that is the case, you might want to simplify your life by using a higher-level API. I'd strongly suggest looking at the OpenCMIS Java client API. If you use that, all you'd need is the service URL for the repository, which in your case would be http://localhost:11080/alfresco/service/api/cmis, and then you can use domain objects like "Session", "Document", and "Folder". For example, using OpenCMIS, the call to create a folder looks like this:

Folder root = session.getRootFolder();

// properties
// (minimal set: name and object type id)
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, "a new folder");

// create the folder
Folder newFolder = root.createFolder(properties);

If OpenCMIS doesn't meet your needs and you insist on moving your own XML back and forth, you should probably grab the Apache Chemistry CMIS Workbench. Then you can watch the traffic and see how exactly what the client is posting. And definitely read the spec. It's a short read and it describes exactly what the XML should be. I notice you are missing a cmisraSmiley Surprisedbject element, for example.

On the other hand, if what you are doing is writing a Web Script controller in Java, you don't need to make remote calls to create objects in the repository at all. Instead, you can use the Java Foundation API. Using the Java Foundation API you can use things like the NodeService and the FileFolderService to work with objects in the repository from within your Java-based web script controller. A wiki search should turn up some examples around that.

Jeff

vjayabalan
Champ in-the-making
Champ in-the-making
Jeff,

Thanks for the wonderful response. I just read your CMIS article and it's awesome!!!!!!!! I was a total 0 with CMIS before I read the doc. Now I understand the moving parts of the architecture. I am going to try working on the OpenCMIS java library, but I have another questions which I hope you don't mind replying.

I used the sample in your article to create a folder under Alfresco's root (Company Home). This is the xml:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-pen.org/ns/cmis/core/200908/">
<title>Someco</title>
<cmisraSmiley Surprisedbject>
<cmisSmiley Tongueroperties>
<cmisSmiley TongueropertyId propertyDefinitionId="cmisSmiley SurprisedbjectTypeId"><cmis:value>cmis:folder</cmis:value></cmisSmiley TongueropertyId>
</cmisSmiley Tongueroperties>
</cmisraSmiley Surprisedbject>
</entry>

and this is the curl:
curl -X POST -uadmin:admin "http://localhost:11080/alfresco/cmis/p/children" -H "Content-Type:application/atom+xml" -d @/cygdrive/c/atom/createSomecoFolder.xml

But when I execute this, I am getting the following error (only a few lines shown)…

Exception: org.alfresco.scripts.ScriptException - 07050011 Failed to execute script 'classpath*:alfresco/template/webscripts/org/alfresco/cmis/children.post.atom.js': Cannot read property 'nativeValue' from null.

Any idea what this means??

thanks a million!

vjayabalan
Champ in-the-making
Champ in-the-making
Jeff,

OpenCMIS is wonderful and so easy to integrate into any application. Thanks for referring me to this excellent API…makes my life so easy.

thanks,
VJ

jpotts
World-Class Innovator
World-Class Innovator
Awesome, I'm glad that worked for you. If you get a chance, please mark this topic as resolved.

Jeff

vjayabalan
Champ in-the-making
Champ in-the-making
Jeff,

I sure will mark it as resolved….just one more quick question though Smiley Happy. When creating a SessionFactory to the Alfresco Repository, I had to specify the ATOMPUB_URL parameter (parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:11080/alfresco/s/cmis")). Does this mean OpenCMIS will use the REST API behind the scenes to access the repo using web scripts?

thanks,
Vasanth

jpotts
World-Class Innovator
World-Class Innovator
Yes. If you would rather use the Web Services binding, you can. Refer to the Chemistry sample code page and JavaDoc for more info:
http://chemistry.apache.org/java/developing/client/dev-client-bindings.html

Jeff

brogits
Champ in-the-making
Champ in-the-making
hi jeff,

i have some issue on restapi, you have afull chapter 6 on your book (alfresco developer guide. im guessing you're the author) but you simply discuss how to create one but not like consuming the already provided restapi by alfresco like the blog and wiki.

i have a couple of question which up to now i can't make sure of an answer.

1. In the restAPI documentation for retrieving tags it say the uri is: /alfresco/service/api/path/{store_type}/{store_id}/{id}/tags. What are the parameters store_type, store_id and id. WHere can we see this on alfresco site. (im trying to get my tags on the share site)

2. Do you have an example on how to update a blog via RestAPI? I have post a question on this http://forums.alfresco.com/en/viewtopic.php?f=36&t=40422 but it seems this kind of stuff is foreign to the forum users….

thank you and more power

jpotts
World-Class Innovator
World-Class Innovator
Regarding your first question, in most cases…

store_type = workspace
store_id = SpacesStore (capitalization matters)
id = in this case, the path to the object, like this: /Sites/testsite1/documentLibrary/test/IMG_0003.JPG

So the full URL:
http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore//Sites/testsite1/documentLibra...

Gives me the following JSON response:
[
showcase,
lobby,
hotel
]

You can try to grok this yourself by looking at the web services index and the entry for this web script, specifically, which is:
http://localhost:8080/alfresco/service/index/uri/api/path/%7Bstore_type%7D/%7Bstore_id%7D/%7Bid%7D/t...

Note that if you wanted to look up the node by node ID instead of path, you'd change the url from "/path" to "/node" and then the ID param becomes the node ID instead of the path.

Regarding your second question, I don't have an example that shows how to update a blog via the REST API. You might want to try doing it with a CMIS client library (Java, PHP, .NET, or Python) available at the Apache Chemistry project. Or, you can use a tool like Firebug to watch the traffic that goes from Share back to the repo and then mimic that in your own app.

Jeff

brogits
Champ in-the-making
Champ in-the-making
thank you so much for the response. I'll  be trying this out today…
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.