cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading content to user sandbox using RestfulAPI

bigobigo
Champ in-the-making
Champ in-the-making
Hi everyone,

I'm struggling with - as it seemed to be really trivial and basic finctionality of Alfresco - sandbox content upload via RestfulAPI.

I belive my configuration is fairly standard and during long searches I've seen a few similar to mine posts, which have not been answered. That is why I've decided to create this topic.

I have created a web project called PIM, prepared simple structure for content upload and currently trying to upload image

with RestfulApi or provided uploadServlet

to particular user's sandbox -> pim–admin where pim is the name of my web project.

Using RestfulAPI approach:
http://192.168.0.141:8080/alfresco/service/api/wcm/webprojects/pim/sandboxes/pim--admin/assets/www/a... and JSON data like name, content, type
works fine only for text files and parsing binary data with JSON rather difficult that is why I have switched to uploadServlet:
http://wiki.alfresco.com/wiki/URL_Addressability and I stucked.

1. How to upload anything with this servlet to user's sandbox ?
2. How to prepare correct avm path for upload servlet ?


Uploading image without specifying nodeRef works great, but where it is actually uploading a file ?

Example URL with NodeRef not provided:
/alfresco/upload/myfile.pdf

and returns reference like:
contentUrl=store://2011/11/17/7/48/bea3f54a-5d8b-4976-a1bb-9119b4726c73.bin|mimetype=image/jpeg|size=0|encoding=UTF-8|locale=en_US_

I simply cannot find it anywhere using web client's search interface of Alfresco.

When I try to specify the right path to sandbox

http://192.168.0.141:8080/alfresco/upload/workspace/SpacesStore/pim/sandboxes/pim--admin/assets/www/...

I get Apache error: 417 Expectation Failed and adding request header like "Expect" - "100-continue" doesn't solve a problem.

I'm using JAVA PutMethod and InputStreamRequestEntity as a request body, so I think the approach is right - as I said it works fine without nodeRef.

Please provide any information for forming right AVM paths in this context.

How to upload images, pdfs into admin's sandbox ????

Luke.


Little update.

I have changed the nodeRef in actual servletUpload call to:

http://192.168.0.141:8080/alfresco/upload/avm/pim--admin/-1;www;avm_webapps;ROOT;MY_FOLDER/C21199205...

and now getting thie exception:

org.alfresco.repo.security.permissions.AccessDeniedException: 10170001 Access Denied.  The system is currently in read-only mode.
   at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:57)

Caused by: org.springframework.dao.TransientDataAccessResourceException: SqlMapClient operation; SQL [];  
— The error occurred in alfresco/ibatis/#resource.dialect#/avm-insert-SqlMap.xml. 
— The error occurred while applying a parameter map. 
— Check the alfresco.avm.insert_AVMNode-InlineParameterMap. 
— Check the statement (update failed). 
— Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  
— The error occurred in alfresco/ibatis/#resource.dialect#/avm-insert-SqlMap.xml. 
— The error occurred while applying a parameter map. 
— Check the alfresco.avm.insert_AVMNode-InlineParameterMap. 
— Check the statement (update failed). 
— Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed


Alfresco Engineers, please respond.
2 REPLIES 2

bigobigo
Champ in-the-making
Champ in-the-making
Ok, I have found the solution !!!

The key lies in the global domain transaction properties.

Go to:

WEB-INF\classes\alfresco\domain\transaction.properties ->

and remove read-only switch. I have not experienced any issues so far, but remember about backing up your work.

server.transaction.mode.readOnly=PROPAGATION_REQUIRED

After restart UploadContentServlet works perfectly for avm.

So to summarize:

1. Create node in sandbox with RestFul API with temp content:
http://192.168.0.141:8080/alfresco/service/api/wcm/webprojects/pim/sandboxes/pim--admin/assets/www/a...
and standard set of JSON properties

HttpClient httpClient = new HttpClient();
           PostMethod method = new PostMethod(base);
           String input = "{" +
                 "\"name\" : \"" + params.getParameter("name") +"\", " +
                 "\"type\" : \"" + params.getParameter("type") + "\", " +
                 "\"content\" : \"" + params.getParameter("content") //only string for JSON parser -> temp content, overwritten in step 2
                 + "\" }";

           method.setRequestEntity(new StringRequestEntity(input,"application/json", null));
           int statusCode = httpClient.executeMethod(method);

2. Go with proper content upload:


      String base = "http://192.168.0.141:8080/alfresco/upload/avm/pim--admin/-1;www;avm_webapps;ROOT;YOUR_FOLDER;your_fi...;

//get file stream
File file = new File("d:\\your_file.pdf");
      
      long length = file.length();
      if (length > Integer.MAX_VALUE) {
         System.err.println("file to large !!!");
       }

      byte[] contentBytes =  new byte[(int)length];
      FileInputStream fis = new FileInputStream(file);
HttpClient httpClient = new HttpClient();
//       Credentials cred = new UsernamePasswordCredentials("admin","tim");
//       httpClient.getState().setCredentials(AuthScope.ANY, cred);

       PutMethod method = new PutMethod(base);
           method.setRequestEntity(new InputStreamRequestEntity(fis));
//           method.addRequestHeader("Expect","100-continue");
           method.setParams(params);
          
       
          int statusCode = httpClient.executeMethod(method);
       if (statusCode != HttpStatus.SC_OK) {
          System.err.println("Method failed: " + method.getStatusLine());
       }


and that's it.

I hope you'll save some time, because I have spent quite a bit to figure that out Smiley Happy

Regards Luke.

narayana_giri
Champ in-the-making
Champ in-the-making
This is about the API - POST /alfresco/service/api/path/{store_type}/{store_id}/{id}/children
mentioned in the link http://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference#Create_folder_or_document_.28createDo...

Question - ContentStream contentStream, how do we pass ContentStream without using CMIS option?

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.