cancel
Showing results for 
Search instead for 
Did you mean: 

org.apache.http.client.methods.HttpPost - creating a customized type

dmralfing
Champ in-the-making
Champ in-the-making
I´m trying to create a customized document instance of my model type "prs:mydocument"
using the Alfresco Java API. I´m trying it with the library:
     org.apache.http.client.methods.HttpPost
Somebody knows what´s exactly the script I need?. I was trying with the script
"/share/proxy/alfresco/slingshot/doclib/activity", but it cannot offers you any way to
indicate in a parameter your content type "prs:mydocument", so you can only indicate in
a parameter "type" the value "file-added", that I think it means the type is "cm:content".
  Somebody know anyway to create a customized document using the Java API with POST?
Thanks
8 REPLIES 8

kaynezhang
World-Class Innovator
World-Class Innovator
I'm not very sure what you mean.
Do you mean to create a document node with type prs:mydocument" using webscript api ?
Then you can use

POST  /api/node/{store_type}/{store_id}/{id}/children
POST  /api/path/{store_type}/{store_id}/{nodepath}/children

you can't use activity webscript api ,It will only create an activity with specified type.

dmralfing
Champ in-the-making
Champ in-the-making
Ok, but can I find somewhere in the web any example with real values - not descriptions - or at least the range of values that can have any parameter?. I´m trying, but I don´t get it!

kaynezhang
World-Class Innovator
World-Class Innovator
Following is sample code I used to test custom type.

package com.kayne.cmis.webscript;

import java.io.File;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.FileUtils;
public class CMISTest {
   
   private void testAddNode(){
      HttpClient client = new HttpClient();
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));

      String apiurl = "http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/TESTFOLDER1/chi...";
                                                         
      PostMethod post = new PostMethod(apiurl);
      try {
         String test = FileUtils.readFileToString(new File("E:\\KayneZhang\\test2.xml"));
         System.out.println(test);
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/atom+xml");
         post.setRequestEntity(new StringRequestEntity(test,
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String resultString = post.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }
   }
   public static void main(String[] args) {
      CMISTest test = new CMISTest();
      test.testAddNode();
   }
}



Following is content of E:\KayneZhang\test2.xml

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/" xmlns:alf="http://www.alfresco.org">
   <author>
      <name>admin</name>
   </author>
   <summary>Presentation.</summary>
   <title>test_add_custom_type</title>
   <cmisra:object>
      <cmis:properties>
         <cmis:propertyId propertyDefinitionId="cmis:objectTypeId">
            <cmis:value>D:ca:customContent</cmis:value>
         </cmis:propertyId>
         <cmis:propertyInteger propertyDefinitionId="cmis:contentStreamLength" displayName="Content Stream Length" queryName="cmis:contentStreamLength">
            <cmis:value>98495335</cmis:value>
         </cmis:propertyInteger>
         <cmis:propertyString propertyDefinitionId="cmis:description" displayName="Titre" queryName="cmis:description">
            <cmis:value>cmis:description</cmis:value>
         </cmis:propertyString>
         <cmis:propertyString propertyDefinitionId="ca:anotherText2" displayName="ca:anotherText2" queryName="ca:anotherText2">
            <cmis:value>another text</cmis:value>
         </cmis:propertyString>
      </cmis:properties>
   </cmisra:object>
<cmisra:content>
<cmisra:mediatype>text/plain</cmisra:mediatype>
<cmisra:base64>VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=</cmisra:base64>
</cmisra:content>
</entry>

dmralfing
Champ in-the-making
Champ in-the-making
Ok, but where do you indicate that the content that you want to create is "prs:mydocument" and not "cm:content" neither "cm:folder"?. I´m not sure if is the right script, all that I want is so easy: to create a customized content type with a web script!. I have tried executing this webscript also
      /share/proxy/alfresco/api/type/prs%3amydocument/formprocessor
…but is doesn´t work..
It must exist any webscript, I don´t know if it is the one you tell me..

kaynezhang
World-Class Innovator
World-Class Innovator
The xml file you sent to webscript url "http://localhost:8080/alfresco/service/cmis/p/TESTFOLDER1/children?format=atomentry" contains all node information ,include node(document) type ,properties and so on.

In my example code  test2.xml following section contains node type (my custom type is ca:customContent,for yours you should use prs:mydocument). 😧 means ca:customContent is subtype of cm:cotent.

         <cmis:propertyId propertyDefinitionId="cmis:objectTypeId">

            <cmis:value>D:ca:customContent</cmis:value>

         </cmis:propertyId>

Following section contains sample code of a custom property named ca:anotherText2 that belong to custom type ca:customContent,in your case you should use your custom property instead.

         <cmis:propertyString propertyDefinitionId="ca:anotherText2" displayName="ca:anotherText2" queryName="ca:anotherText2">
            <cmis:value>another text</cmis:value>
         </cmis:propertyString>

dmralfing
Champ in-the-making
Champ in-the-making
Thank you Kaynezhand, you are right, but it doesn´t include any file in the code you have put. I could
create a customized type with a function like this:

public static synchronized void testPost (String strName,String strPath) throws Exception
{
    String url="http://localhost:8080/share/proxy/alfresco/api/type/prs%3amydocument/formprocessor";
    String param="{";
    param+="\"alf_destination\":\"workspace://SpacesStore/2198ff48-de64-4800-98c9-389136439153\",";
    param+=",\"prop_cm_name\":\"HELLO MY NEW DOCUMENT\"";
    param+=",\"prop_prs_department\":\"CORPORATIVE FINANCE\"";
    param+="}";
    org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
    client.setCookieStore(cookieStore);
    org.apache.http.client.methods.HttpPost post = new org.apache.http.client.methods.HttpPost(url);
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(param,"UTF-8"));
   org.apache.http.client.ResponseHandler<String> responseHandler = new org.apache.http.impl.client.BasicResponseHandler();
    client.execute(post, responseHandler);
}

I prefer it because it´s easier, but I don´t mind to do it with CMIS as you say if it exists any way
to upload also the file content. It should me something like this, but it doesn´t work..


   File f=new File ("D:/temp/mydocument.pdf");
   post.setEntity (new FileEntity (f, "application/pdf"));


How I create my customized content including also the file?

kaynezhang
World-Class Innovator
World-Class Innovator
NO,I already include a txt file in my code .
Please check this section

<cmisra:content>
<cmisra:mediatype>text/plain</cmisra:mediatype>
<cmisra:base64>VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=</cmisra:base64>
</cmisra:content>

It is used to upload a file(content),in my case I upload  a file whose format is a text/plain(indicated by <strong> cmisra:mediatype</strong> element) and whose content is The quick brown fox jumps over the lazy dog.(indicated by <strong> cmisra:base64</strong> element,base64 encoded of course).

In cmis atom binding if you want to upload a file ,it must be encoded using base64 encoder.

syncovery
Champ in-the-making
Champ in-the-making
Dear Kaynezhang, can you please suggest a URL entry for upload to my.alfresco.com cloud site/documentLibrary

for on-premises storage as you told entry is:
http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/TESTFOLDER1/chi...


but for cloud this does not work. There should be company name, account name, and site name I'm guessing.

thanks,
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.