Quickshare by webscript

cancel
Showing results for 
Search instead for 
Did you mean: 
afasis
Active Member

Quickshare by webscript

I am using Alfresco Entreprise 5.2.3.
I created a service with org.apache.chemistry.opencmis  (client) 1.1.0 with atompub connection, which creates folders and saves documents to a repository /Sites/bdes/documentLibrary/extraitconforme/  that I can view with alfresco webpage :
http://localhost:9080/share/page/site/bdes/document-details?nodeRef=workspace://SpacesStore/7d1cbc32...

 

I also want to make a document publicly downloadable with no authentication.
Meaning after saving of the document I want to make it immediatly publicly shareble for download only.
Because there is nothing in chemistry doing this I start using Alfresco's following webscript.

/api/internal/shared/share/{store_protocol}/{store_id}/{node_id}
http://localhost:9080/alfresco/service/index/uri/api/internal/shared/share/%7Bstore_protocol%7D/%7Bs... 

 

Because this is a httppost request to the server, I cannot do this without authentication.
So I got an authtenication ticket first by the webscript : /alfresco/service/api/login

 

Than I call the "quickshare" webscript : /alfresco/service/api/internal/shared/share/workspace/SpacesStore/{node_id}.

to make the document publicly shareble and get a share_id.
Because I don't know yet how to get the nodeId (unless its the same as the docId from Document

I get de the id from sharelink in alfresco webpage in the share field and add it to the httppost

So My httppost to make the file shareable will be :

http://localhost:9080/alfresco/service/api/internal/shared/share/workspace/SpacesStore/7d1cbc32-747d...?ticket=TICKET_d3e8854a4f253a1608615b294c5ed8520fd94f1f

or

http://localhost:9080/alfresco/service/api/internal/shared/share/workspace/SpacesStore/7d1cbc32-747d...?alf_ticket=TICKET_d3e8854a4f253a1608615b294c5ed8520fd94f1f

 

when putting this in my browser (firefox) , I get :

 

status
code    500
name    "Internal Error"
description    "An error inside the HTTP server which prevented it from fulfilling the request."
message    "04290072 Wrapped Exception (with status template): 04290094 Unable to find: workspace/SpacesStore/7d1cbc32-747d-44ba-9d1a-6a0709be4b77"
exception    ""
callstack    []
server    "Enterprise v5.2.3 (r852994b1-b12) schema 10.065"
time    "29-mei-2018 18:22:04"

 

What am I doing wrong here?..
Can I even use ticket to get a document publicaly downloadable?

 

Can anyone please help me out.

I've read a lot of article about this, still I dont see whats wrong here.

10 Replies
jpotts
Professional

Re: Quickshare by webscript

Rather than using the internal end point (all internal end points should be avoided), see if the 5.2 REST API will work better for you. Specifically, take a look at Alfresco Content Services REST API Explorer 

afasis
Active Member

Re: Quickshare by webscript

Using the /shared-links with an alf_ticket in the url gives an IOException at connection.getInputStream with message
400 error (Bad Request). I also tried with

This is my code :

String quickshare = "";

        URL url;
        HttpURLConnection connection = null;
        try
        {
            String authTicket = getAlfrescoAuthTicket();
            // Create connection
            url = new URL(ged_root_url + "/alfresco/api/-default-/public/alfresco/versions/1/shared-links?alf_ticket=" + authTicket);

            //Body params
            String nodeId = "2aad8afe-4905-4009-b226-16a88c0bf4a7";
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH,1);
            String expirationDate = DateUtils.getDateString(cal.getTime(),"yyyy-MM-dd");

            String urlParameters = "{ \"nodeId\" : \"" + nodeId +"\", \"expiresAt\" : \"" + expirationDate +"\" }";
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null)
            {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            String _jsonResponse = response.toString();

            JSONObject jsonResponseObject = (JSONObject)new JSONParser().parse(_jsonResponse);
            JSONObject jsonDataObject = (JSONObject)new JSONParser().parse(jsonResponseObject.get("data").toString());

afasis
Active Member

Re: Quickshare by webscript

Althoug, when I put the following url :

http://localhost:9080/alfresco/api/-default-/public/alfresco/versions/1/shared-links?alf_ticket=TICK...

in my firefox browser I get :

list
pagination
count5
hasMoreItemsfalse
totalItems5
skipCount0
maxItems100
entries
0
entry
modifiedAt"2018-05-29T13:41:40.016+0000"
modifiedByUser
id"admin"
displayName"Administrator"
name"2018_111555_25005B0138-00G002_20180529154139"
id"5Zw2b9u6QEuKwv1cUk8E5A"
nodeId"2aad8afe-4905-4009-b226-16a88c0bf4a7"
sharedByUser
id"bdes"
displayName"bdes bdes"
content
mimeType"application/pdf"
mimeTypeName"Adobe PDF Document"
sizeInBytes93605
encoding"UTF-8"
1
entry
modifiedAt"2018-05-25T11:17:48.706+0000"
modifiedByUser
id"admin"
displayName"Administrator"
name"2018_111555_25005B0138-00G002_20180525131748"
id"sO0SOUa6RwizVuXz5YsQ4A"
nodeId"e6bafc4a-a3a7-4d82-a5e4-be9f26cdaf66"
sharedByUser
id"admin"
displayName"Administrator"
content
mimeType"application/pdf"
mimeTypeName"Adobe PDF Document"
sizeInBytes93605
encoding"UTF-8"
2
entry
modifiedAt"2018-05-25T11:16:17.135+0000"
modifiedByUser
id"admin"
displayName"Administrator"
name"2018_111555_25005B0138-00G002_20180525131617"
id"CWDRGie2Qf-Yrwiwoby7TQ"
nodeId"12c82e4e-3fa4-4815-bd70-05a690b8758f"
sharedByUser
id"admin"
displayName"Administrator"
content
mimeType"application/pdf"
mimeTypeName"Adobe PDF Document"
sizeInBytes93605
encoding"UTF-8"
3
entry
modifiedAt"2018-05-22T16:06:16.026+0000"
modifiedByUser
id"admin"
displayName"Administrator"
name"2018_111222_25005B0138-00G002_20180522180615"
id"deRXTBqFR6GwSnWrRLLvcA"
nodeId"2f2a1f7b-447b-44d2-81a2-ecf1ec7670ae"
sharedByUser
id"admin"
displayName"Administrator"
content
mimeType"application/pdf"
mimeTypeName"Adobe PDF Document"
sizeInBytes93605
encoding"UTF-8"
4
entry
modifiedAt"2018-05-18T14:55:57.579+0000"
modifiedByUser
id"admin"
displayName"Administrator"
name"2018_111222_25005B0138-00G002_20180518165547"
id"MfNEbkB_SpSbS7eLR4ZeKg"
nodeId"1acfb5fd-6100-4a54-a107-317391a83a5b"
sharedByUser
id"admin"
displayName"Administrator"
content
mimeType"application/pdf"
mimeTypeName"Adobe PDF Document"
sizeInBytes93605
encoding"UTF-8"
jpotts
Professional

Re: Quickshare by webscript

When I use curl I can create a shared link by posting to the same URL you are using. I can do it successfully without an expiration date by posting:

{ "nodeId" : "0a0e5f81-20ec-43ee-ac85-232a52abbc6b"}

And I can do it successfully with an expiration date by posting:

{ "nodeId" : "0a0e5f81-20ec-43ee-ac85-232a52abbc6b", "expiresAt": "2018-11-06T16:57:37.276+0000"}

So you are either trying to share something that is already shared, which will return an error, or you are specifying a bad/missing nodeId, or the format of the date in your expiresAt property is incorrect.

afasis
Active Member

Re: Quickshare by webscript

Hi Jeff,

Thanks that was one of the problems indeed !
In the Alfresco Content Services REST API Explorer , I found out that the expirationDate must be of the format you mentioned. So I did this simply by using SimpleDateFormat, which included the needed format :

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH,1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String expirationDate = formatter.format(cal.getTime());

I also found out that the documentId en NodeId are actualy the same
Except the docId that alfresco returns in the Document object  (org.apache.chemistry) has the version attached to it with a semicolon at the end of the string.
For example : "0a0e5f81-20ec-43ee-ac85-232a52abbc6b;1.0
So doing  : String nodeId = docId.substring(0,docId.indexOf(";"));  gets you the nodeId.

I am putting my the code in this blog for others to use :
In this code I am getting an authorisationticket first (on the bottom of this post) which I use to do the http request for the quickshareLink.

Some constants used :
ged_root_url = http://localhost:9080
public static final String ALFRESCO_REST_API_SHAREDLINKS = "/alfresco/api/-default-/public/alfresco/versions/1/shared-links";
public static final String ALFRESCO_API_LOGIN = "/alfresco/service/api/login";  // to get the authorisation ticket

public String getQuickShareLink(String docId)
{
    String quickShareLink="";

    String ticket = getAlfrescoAuthTicket();
    String nodeId = docId.substring(0,docId.indexOf(";"));
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH,1);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    String expirationDate = formatter.format(cal.getTime());
    URL url;
    HttpURLConnection connection = null;
    try
    {
        // Create connection
        url = new URL(ged_root_url + GedConstants.ALFRESCO_REST_API_SHAREDLINKS +"?alf_ticket="+ticket);
        String urlParameters = "{ \"nodeId\" : \"" + nodeId +"\", \"expiresAt\" : \"" + expirationDate +"\" }";

        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        // Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null)
        {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        String _jsonResponse = response.toString();

        JSONObject jsonResponseObject = (JSONObject)new JSONParser().parse(_jsonResponse);
        JSONObject jsonDataObject = (JSONObject)new JSONParser().parse(jsonResponseObject.get("entry").toString());
        String quickShareId = jsonDataObject.get("id").toString();
        quickShareLink = ged_root_url + GedConstants.ALFRESCO_REST_API_SHAREDLINKS + "/" + quickShareId + "/content";

    }
    catch (Exception e)
    {
        e.printStackTrace();
        logger.error("Could not create an alfresco shared link for document : " + docId);
        return null;
    }
    finally
    {
        if (connection != null)
        {
            connection.disconnect();
        }
    }
    return quickShareLink;
}
public String getAlfrescoAuthTicket()
{
    String ticket = "";

    URL url;
    HttpURLConnection connection = null;
    try
    {
        // Create connection
        url = new URL(ged_root_url + GedConstants.ALFRESCO_API_LOGIN);
        String urlParameters = "{ \"username\" : \"" + ged_username +"\", \"password\" : \"" + ged_password +"\" }";
        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        // Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null)
        {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        String _jsonResponse = response.toString();

        JSONObject jsonResponseObject = (JSONObject)new JSONParser().parse(_jsonResponse);
        JSONObject jsonDataObject = (JSONObject)new JSONParser().parse(jsonResponseObject.get("data").toString());
        ticket = jsonDataObject.get("ticket").toString();

    }
    catch (Exception e)
    {
        e.printStackTrace();
        logger.error("Could not create an Alfresco AUTHENTICATION TICKET");
        return null;
    }
    finally
    {
        if (connection != null)
        {
            connection.disconnect();
        }
    }
    return ticket;
}
jpotts
Professional

Re: Quickshare by webscript

The fact that the doc ID and the CMIS Object ID appear to be related is something you should DEFINITELY NOT depend on. The CMIS object ID is intended to be opaque. You should not try to reverse engineer it and you should not try to create it by appending the document ID to the version label. Doing so will create very brittle code that could break without notice.

Most CMIS calls in Alfresco will accept either a CMIS Object ID or the Alfresco Node Reference.

If you need to get the Alfresco Node Reference from a CMIS object, look at its properties. The NodeReference is available in the alfcmis:nodeRef property.

afasis
Active Member

Re: Quickshare by webscript

Jeff,

Thanks for the important tip.

I tested docId and nodeRef and the call does not support neither.

alfcmis:nodeRef returns value: "workspace://SpacesStore/7f5772df-a840-498b-bae1-5fcf70efb485"


In the properties there is no property nodeId ...  see link :

What I need is that part after the last "/" namingly : 7f5772df-a840-498b-bae1-5fcf70efb485

Can I substring the nodeRef to have the nodeId, or shouldn't I depend on it either?

jpotts
Professional

Re: Quickshare by webscript

Yes, you can substring the Alfresco nodeRef to remove "workspace://SpacesStore" assuming you only care about nodes in the spaces store and do not care about nodes in the archive store.

afasis
Active Member

Re: Quickshare by webscript

Great !

All works like a sharm now.

Jeff, thank you for your help and guidance.

Very much appreciated.

Samir