As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
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-747d-44ba-9d1a-6a0709be4b77. Works perfectly.
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/%7Bstore_id%7D/%7Bnode_i…
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-44ba-9d1a-6a0709be4b77?ticket=TICKET_d3e8854a4f253a1608615b294c5ed8520fd94f1f
or
http://localhost:9080/alfresco/service/api/internal/shared/share/workspace/SpacesStore/7d1cbc32-747d-44ba-9d1a-6a0709be4b77?alf_ticket=TICKET_d3e8854a4f253a1608615b294c5ed8520fd94f1f
when putting this in my browser (firefox) , I get :
statuscode 500name "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.
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
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());
Althoug, when I put the following url :
http://localhost:9080/alfresco/api/-default-/public/alfresco/versions/1/shared-links?alf_ticket=TICKET_981cf17f11ce73a45b92560e2de84bcfed719d72
in my firefox browser I get :
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.
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.0So 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:9080public 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; }
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.
how to get the username and password in above code?