AlfrescoArchive / alfresco-oauth-sample

Simple OAuth Sample application using html and Javascript
4 stars 8 forks source link

Create Entity or Upload file #3

Closed dima72 closed 9 years ago

dima72 commented 9 years ago

Dear All,

please show an example of creating file entity or file upload in AlfrescoСloud storage.

Thanks in advance,

dima72 commented 9 years ago

Dear All, please show example of POST request to upload file in AlfrescoСloud storage.

Thanks in advance,

dima72 commented 9 years ago

up

jottley commented 9 years ago

To be clear...you are asking about using CMIS to do a file upload? Are you wanting to use the browser bindings? Or one of the other CMIS bindings? By saying POST do you want to do a raw call against the CMIS portion of the Public API or will you be using one of the Chemistry clients to do the upload? Context please so we can try and get or point you to what you are looking for?

dima72 commented 9 years ago

Thank you for talking with me Jottley,

I think I'm trying to do CMIS bindings with raw call, I think I need Public API to upload files directly. My software uses own sockets and http request to cloud storage from desktop without any other libraries. In documentation http://ecmarchitect.com/images/articles/cmis/cmis-article.pdf they say: to create document it need to make POST request to http://localhost:8080/alfresco/s/cmis/p/Someco/Marketing/Whitepapers/children

but I'm trying to reach my.alfresco.com documentLibrary container to create file. My site is : https://my.alfresco.com/share/stumpware.com/page/site/dima/documentlibrary

what http:// path would be in my case?

thanks in advance,

jottley commented 9 years ago

All right so using the browser bindings we can work with JSON responses. Calls to create/upload content follow a HTML Form pattern.

There are two slides in this PPT (http://summit.alfresco.com/cmis/views/workspace%253A%252F%252FSpacesStore%252Fd58db4e9-7825-4818-ae51-f602d476e1d5) that give examples of using forms to create a Document. Look for the titles "Creating Content is Easy".

In this excerpt from the book "CMIS and Apache Chemistry in Action" (https://weblogs.java.net/blog/manningpubs/archive/2013/07/02/browser-binding-cmis-repository) you can see a bit of the format that you need if you are not using forms. You have to create the raw POST body to create or upload a document.

Here are some snippets of code that was used to do something similar: Creating a document. (This was written in Apex -- Salesforce's Language). The map that is passed in contains key value pairs of the CMIS Properties including an extra pair where the key name was content and the value was a json object that was written into the Alfresco repository.

public static String CONTENT_TYPE_TEXT_PLAIN = 'text/plain';

public static String BOUNDARY = '----___boundary___xxx';
public static String CONTENT_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data; boundary=' + BOUNDARY;

public static String CR = '\r';
public static String LF = '\n';
public static String DASH = '-';
public static String COLON = ':';
public static String SEMI_COLON = ';';
public static String EQUAL = '=';
public static String QUOTE = '"';
public static String SPACE = ' ';

public static String HEADER = DASH + DASH + BOUNDARY + CR + LF;
public static String CONTENT_DISPOSITION = 'Content-Disposition';
public static String FORM_DATA = 'form-data';
public static String NAME = 'name';
public static String CD_HEADER = CONTENT_DISPOSITION + COLON + SPACE + FORM_DATA + SEMI_COLON + SPACE + NAME + EQUAL;
public static String FORM_DATA_TYPE_CONTENT = 'content';
public static String FORM_DATA_FILENAME_PROPERTY = 'filename';
public static String FORM_DATA_FILNAME_DUMMY = 'dummy.txt';
public static String FOOTER = CR + LF + DASH + DASH + BOUNDARY + DASH + DASH;

global static String buildMultiPartFormData(Map<String, String> propertyMap)
{
    String body = '';

    if (!propertyMap.isEmpty())
    {            
        if (propertyMap.containsKey(FORM_DATA_TYPE_CONTENT))
        {
            body += HEADER;
            body += CONTENT_DISPOSITION + QUOTE + FORM_DATA_TYPE_CONTENT + QUOTE + SEMI_COLON + SPACE + FORM_DATA_FILNAME_DUMMY + EQUAL + QUOTE + FORM_DATA_FILNAME_DUMMY+ QUOTE + CR + LF;
            body += HEADER_CONTENT_TYPE + COLON + SPACE + CONTENT_TYPE_TEXT_PLAIN + CR + LF + CR + LF;
            body += propertyMap.remove(FORM_DATA_TYPE_CONTENT)+LF; 
        }

        if (!propertyMap.isEmpty())
        {
            for (String key : propertyMap.keySet())
            {
                body += HEADER;
                body += CONTENT_DISPOSITION + QUOTE + key + QUOTE + CR + LF +CR + LF;
                body += propertyMap.get(key) + CR + LF;
            }
        }

        return body + FOOTER;
    }

    return body;
}

The URL would be similar to https://api.alfresco.com///public/cmis/versions/1.1/browser/root/

The path may also work by passing the objectId as 'root?objectId=' but you would need to verify that...it has been a while since I've played with this code.

dima72 commented 9 years ago

Dear Jarred, thank you for responding, it is not clear how URL like https://api.alfresco.com///public/cmis/versions/1.1/browser/root/

will point to exactly my my.alfresco.com account and site?

If i understand correctly URL should contain company name and site name atleast.

I'm trying to POST to this url but getting response - 'Invalid Key!

I do provide valid token access in header, and token works for other requests where I'm getting documentLibrary listing.

any advice is welcome, thanks

jottley commented 9 years ago

You are correct, mark down strip out part of the url example

https://api.alfresco.com/ - your tenant name here - /public/cmis/versions/1.1/browser/root/ - path to the parent folder -

dima72 commented 9 years ago

I still have no success with figuring out URL entry for my.alfresco.com cloud storage. I have added my question to this post https://forums.alfresco.com/forum/developer-discussions/alfresco-api/orgapachehttpclientmethodshttppost-creating-customized-type#comment-158372

thanks in anyway

jottley commented 9 years ago

Ok..I did a quick validation using https://github.com/wiztools/rest-client.

I first generated the oauth token so that I could add it as an Authorization header to the request.

ex. Authorization Bearer 29996661050d4-fakeid-dfd96e3888888888870

Next the URL I tested with was: https://api.alfresco.com/alfresco.com/public/cmis/versions/1.1/browser/root/sites/jared-ottley-alfresco-com/documentlibrary/ The created/uploaded document will end up at the root of the document library in my site.

I performed a POST to this endpoint passing multipart form data using the following values:

cmisaction = createDocument propertyId[0] = cmis:objectTypeId propertyValue[0] = cmis:document propertyId[1] = cmis:name propertyValue[1] = text.txt succinct = true

The file was attached as part of this to the multipart form The name was the name of the document to be uploaded it then included that body of the document. If this is plain text you can just add it. If it is document it should be a base64encoded blob of the content of the document.

The REST client I used allows me to just attach a file.

When I posted the document get back a 201 Created and the response body

{ "propertiesExtension" : { "aspects" : { "properties" : { "propertyString" : [ null, null, null ] }, "appliedAspects" : [ "P:sys:localized", "P:cm:titled", "P:cm:author" ] } }, "succinctProperties" : { "cmis:isPrivateWorkingCopy" : false, "cmis:contentStreamFileName" : "test.txt", "cmis:versionSeriesCheckedOutBy" : null, "cmis:versionSeriesCheckedOutId" : null, "cmis:objectTypeId" : "cmis:document", "cmis:secondaryObjectTypeIds" : [ "P:sys:localized", "P:cm:titled", "P:cm:author" ], "cmis:isMajorVersion" : true, "cmis:contentStreamId" : "s3:\/\/value\/Id.bin", "cmis:baseTypeId" : "cmis:document", "cmis:changeToken" : null, "cmis:objectId" : "d2b68be2-a722-47ba-8d87-b556ac13b006;1.0", "cmis:isImmutable" : false, "cmis:creationDate" : 1437159612353, "cmis:lastModificationDate" : 1437159612353, "cm:title" : null, "cmis:contentStreamLength" : 841, "cmis:versionSeriesId" : "d2b68be2-a722-47ba-8d87-b556ac13b006", "cmis:versionLabel" : "1.0", "cmis:checkinComment" : "Initial Version", "cm:author" : null, "cmis:isVersionSeriesCheckedOut" : false, "cmis:lastModifiedBy" : "jared.ottley@alfresco.com", "cmis:createdBy" : "jared.ottley@alfresco.com", "cmis:description" : null, "cmis:name" : "test.txt", "cm:description" : null, "cmis:contentStreamMimeType" : "text\/plain", "cmis:isLatestMajorVersion" : true, "cmis:isLatestVersion" : true, "alfcmis:nodeRef" : "workspace:\/\/SpacesStore\/d2b68be2-a722-47ba-8d87-b556ac13b006" } }

dima72 commented 9 years ago

Thank you Maestro! Create document works perfect!

Can you please suggest how to update content of document. I'm trying to upload base64 encoded file with method proposed by you, but document on remote is always empty regardless If I sending empty file or file with data.

I tried to pure POST file content to https://api.alfresco.com/...company_name.../public/cmis/versions/1.0/atom/content?id=..object_id

but I'm getting error -Unknown operation.

thanks in advance,

dima72 commented 9 years ago

up

dima72 commented 9 years ago

up

jottley commented 9 years ago

Sorry for slow response. I’m on Holiday this week and will try to work in sometime to respond.

On Jul 22, 2015, at 4:16 AM, Dmitry Konnov notifications@github.com<mailto:notifications@github.com> wrote:

up

— Reply to this email directly or view it on GitHubhttps://github.com/Alfresco/alfresco-oauth-sample/issues/3#issuecomment-123651996.

dima72 commented 9 years ago

Thank you Sir, please take your time,

we are nearly missing this feature to update our product with Alfresco connectivity. dir listing, download is working.

dima72 commented 9 years ago

up

dima72 commented 9 years ago

Dear Jared, it is experimentally figured out.. http PUT of document content to 'https://api.alfresco.com/'+ CompanyName +'/public/cmis/versions/1.0/atom/content?id=' +GuidObject;

updates content of document. No base64 encode is need.