cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble creating a folder via web script

mikew
Champ in-the-making
Champ in-the-making
I’m attempting to write a very simple proof of concept stand-alone java program that communicates directly with a remote Alfresco server (3.0b) via web scripts. The program will, hopefully, recreate a local directory structure and upload local files into it. I am running into problems, specifically with http POST requests.

I am having no problems getting Alfresco tickets using login web script and no problem performing GET requests to:
GET /alfresco/service/api/path/{store_type}/{store_id}/{id}/descendants
to list the directory contents.

However, I am receiving a 400 response when I change the method to POST (either in code or via a form in an html page.)… with the message "Posting of media resource not supported"
POST /alfresco/service/api/path/{store_type}/{store_id}/{id}/descendants

I have been looking for guidance and inspiration here:
/alfresco/service/script/org/alfresco/repository/store/descendants.post

There are two js files listed:
File: org/alfresco/repository/store/descendants.post.atom.js
File: org/alfresco/repository/store/descendants.post.js

I'm guessing I'm hitting the latter (It issues a 400 response with the previously mentioned message) rather than the previous, which actually does the directory creation.

I'm also guessing I'm either taking the wrong approach entirely, or not constructing the POST request correctly (GET to the same URL returns a list of documents in the specified location).

My first question is: is this possible? If so, how would a POST request be constructed? I figure there must be some content in the POST (parameters: title or name, alf_ticket, typeId???)

Sorry if I'm missing something obvious, but any help or guidance would be appreciated.
17 REPLIES 17

groberts
Champ on-the-rise
Champ on-the-rise
Thanks for this discussion, its helped me.   

Can anyone say why I get a document instead of a folder when I try to create a folder with Firefox REST client as follows;

I POST to http://admin:admin@xxx.mydomain.org/alfresco/service/api/path/workspace/SpacesStore/Clinical%20Studi...

with just one Header

Content-type    application/atom+xml;type=entry

and a body of

<?xml version='1.0' encoding='utf-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>
<title>ThisShouldBeAFolder</title>
<summary>ThisShouldBeAFolder</summary>
<cmisSmiley Surprisedbject>
<cmisSmiley Tongueroperties>
<cmisSmiley TongueropertyString cmis:name='ObjectTypeId'>
<cmis:value>folder</cmis:value>
</cmisSmiley TongueropertyString>
</cmisSmiley Tongueroperties>
</cmisSmiley Surprisedbject>
</entry>

I see an ordinary bit of content created !

groberts
Champ on-the-rise
Champ on-the-rise
Just checked in the book, "Alfresco 3 Web Services" and the correct Atom XML to create a folder (for me in Alferesco Community 3.3g) is as follows.

(Just a bit confused as to why the recent XML posted above created a document instead of a folder)
(While I am at it,  non of this is RESTful, its just like a web service !)

<entry xmlns="http://www.w3.org/2005/Atom"
     xmlns:app="http://www.w3.org/2007/app"
     xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">
<author>
    <name>admin</name>
</author>
<id>ignored</id>
<summary>Created via CMIS AtomPub</summary>
<title>CMIS Demo</title>
<updated>2010-02-27T15:30:39.767+01:00</updated>
<cmisraSmiley Surprisedbject>
  <cmisSmiley Tongueroperties>
     <cmisSmiley TongueropertyId  propertyDefinitionId="cmisSmiley SurprisedbjectTypeId">
         <cmis:value>cmis:folder</cmis:value>
     </cmisSmiley TongueropertyId>
     <cmisSmiley TongueropertyString propertyDefinitionId="cmis:name">
           <cmis:value>CMIS Demo</cmis:value>
     </cmisSmiley TongueropertyString>
</cmisSmiley Tongueroperties>
</cmisraSmiley Surprisedbject>
</entry>

zuzoovn
Champ in-the-making
Champ in-the-making
Here is a working source for reference.  Special thanks for all related articles in this forum.  Smiley Very Happy


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class CreateFolder {

   public static void main(String[] args) throws Exception {
      long start = System.currentTimeMillis();
      
      String host = "192.168.1.1";
      int port = 8080;
      String username = "admin";
      String password = "admin";
      String parentFolder = "Company Home";
      String folderName = "sales3";
      String description = "sales space3";
      String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
      String contentType = "application/atom+xml;type=entry";
      
      String xml =
         "<?xml version='1.0' encoding='utf-8'?>\n" +
         "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
         "<title>" + folderName + "</title>\n" +
         "<summary>" + description + "</summary>\n" +
         "<cmis:object>\n"+
         "<cmis:properties>\n" +
         "<cmis:propertyString cmis:name='ObjectTypeId'>\n" +
         "<cmis:value>folder</cmis:value>\n" +
         "</cmis:propertyString>\n" +
         "</cmis:properties>\n" +
         "</cmis:object>\n" +
         "</entry>\n";
      
      DefaultHttpClient httpclient = new DefaultHttpClient();

      httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));
      
      HttpPost httppost = new HttpPost(url);
      httppost.setHeader("Content-type", contentType);
      
      StringEntity requestEntity = new StringEntity(xml, "UTF-8");
      httppost.setEntity(requestEntity);

      
      System.out.println("executing request" + httppost.getRequestLine());
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();

      System.out.println("—————————————-");
      System.out.println(response.getStatusLine());
      if (entity != null) {
         System.out.println("Response content type: " + entity.getContentType());
         long contentLength = entity.getContentLength();
         System.out.println("Response content length: "
               + entity.getContentLength());
         
         if (contentLength > 0) {
            byte [] b = new byte[(int) contentLength];
            entity.getContent().read(b);
            System.out.println("Response content: " + new String(b));
         }
         
         entity.writeTo(System.out);
      }

      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
      long end = System.currentTimeMillis();
      System.out.println("Time spend: " + (end-start) + "ms");
   }
}

I have error 404 : Not found

Help me please
============
UPDATE: i change link to: /alfresco/service/cmis/i/{id}/children

So i can create, but file, not folder. I am using: "<cmis:value>folder</cmis:value>\n"

zuzoovn
Champ in-the-making
Champ in-the-making
Here is another working sample to upload files.  Tested for pfd and word.  Again, special thanks for all related articles in this forum.  Smiley Very Happy


import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Collection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import sun.misc.BASE64Encoder;
import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.MimeUtil2;

/**
* A simple example that uses HttpClient to execute an HTTP request against a
* target site that requires user authentication.
*/
public class UploadDocument {

   public static void main(String[] args) throws Exception {
      long start = System.currentTimeMillis();
      
      String host = "192.168.21.111";
      int port = 8080;
      String username = "admin";
      String password = "admin";
      String parentFolder = "Company Home";
      String filePath = "test.pdf";
      String description = "PDF upload test";
      String author = "admin";
      String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children";
      String contentType = "application/atom+xml;type=entry";

      BASE64Encoder encoder = new BASE64Encoder();
      
      File file = new File(filePath);
       FileInputStream fis = new FileInputStream(file);
       DataInputStream dis = new DataInputStream(fis);
       // Create the byte array to hold the data
       byte[] bytes = new byte[(int) file.length()];
       dis.readFully(bytes);
      
       Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
       MimeType mimeType = MimeUtil.getMostSpecificMimeType(mimeTypes);
       if(mimeType == null) mimeType = MimeUtil2.UNKNOWN_MIME_TYPE;      
      
      String xml =
         "<?xml version='1.0' encoding='utf-8'?>\n" +
         "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" +
         "<title>" + file.getName() + "</title>\n" +
         "<summary>" + description + "</summary>\n" +
         "<author>" + author + "</author>\n" +
         "<content type='" + mimeType.toString() + "'>" + encoder.encode(bytes) + "</content>\n" +
         "<cmis:object>\n"+
         "<cmis:properties>\n" +
         "<cmis:propertyString cmis:name='ObjectTypeId'>\n" +
         "<cmis:value>document</cmis:value>\n" +
         "</cmis:propertyString>\n" +
         "</cmis:properties>\n" +
         "</cmis:object>\n" +
         "</entry>\n";
            
      DefaultHttpClient httpclient = new DefaultHttpClient();

      httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));
      
      HttpPost httppost = new HttpPost(url);
      httppost.setHeader("Content-type", contentType);
      
      StringEntity requestEntity = new StringEntity(xml, "UTF-8");
      httppost.setEntity(requestEntity);

      System.out.println("executing request" + httppost.getRequestLine());
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();

      System.out.println("—————————————-");
      System.out.println(response.getStatusLine());
      if (entity != null) {
         System.out.println("Response content type: " + entity.getContentType());
         long contentLength = entity.getContentLength();
         System.out.println("Response content length: "
               + entity.getContentLength());
         
         if (contentLength > 0) {
            byte [] b = new byte[(int) contentLength];
            entity.getContent().read(b);
            System.out.println("Response content: " + new String(b));
         }
         
         entity.writeTo(System.out);
      }

      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
      long end = System.currentTimeMillis();
      System.out.println("Time spend: " + (end-start) + "ms");
   }
}


Hi, when i uses your code, i have error 404.

Do you know any code to upload file which work in Android?

alabbas
Champ in-the-making
Champ in-the-making
I have the same problem with this code , do you fegar out the solution

kaynezhang
World-Class Innovator
World-Class Innovator
Why not use opencmis client

fahad
Champ in-the-making
Champ in-the-making
I am executing the above mentioned code but unable to resolve error 404 just appear because of parent folder not found. I aleady created a site "ABC" and a folder in that "folder123". Now I want to create a new folder inside "folder123". Can you guide me what will be the parentFolder in the code provided above?? How can I create a folder inside "forlder123". Your reply is encouraged

shibu
Champ in-the-making
Champ in-the-making
First of all, Thanx for this great post.
Code for create folder is working properly. But instead of creating folder it created a Binary File(Octet Stream). 

Please help. How to create folder instead of Binary File(Octet Stream)?
And can anybody tell , how can I create a JSON here instead of xml?