Skip to content

Instantly share code, notes, and snippets.

@pmonks
Last active December 19, 2023 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pmonks/9674126 to your computer and use it in GitHub Desktop.
Save pmonks/9674126 to your computer and use it in GitHub Desktop.
/*
*
* Copyright © 2011-2016 Peter Monks (pmonks@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of an unsupported extension to Alfresco.
*
*/
//Note: with logback, OpenCMIS will log at debug level to stdout, which is a bit noisy
//@Grab(group="ch.qos.logback", module="logback-classic", version="1.1.3")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-commons-api", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-commons-impl", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-api", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-impl", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-bindings", version="0.13.0")
import org.apache.chemistry.opencmis.commons.*;
import org.apache.chemistry.opencmis.commons.enums.*;
import org.apache.chemistry.opencmis.client.*;
import org.apache.chemistry.opencmis.client.api.*;
import org.apache.chemistry.opencmis.client.runtime.*;
import org.apache.chemistry.opencmis.commons.data.*;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.*;
import org.apache.chemistry.opencmis.commons.exceptions.*;
final String CMIS_SERVER_HOSTNAME = "localhost"
final int CMIS_SERVER_PORT = 8080
final String CMIS_USER = "admin"
final String CMIS_PASSWORD = "admin"
final String ALFRESCO33_CMIS10_ATOMPUB_URL = "http://${CMIS_SERVER_HOSTNAME}:${CMIS_SERVER_PORT}/alfresco/service/cmis";
final String ALFRESCO40_CMIS10_ATOMPUB_URL = "http://${CMIS_SERVER_HOSTNAME}:${CMIS_SERVER_PORT}/alfresco/cmisatom";
final String ALFRESCO42_CMIS10_ATOMPUB_URL = "http://${CMIS_SERVER_HOSTNAME}:${CMIS_SERVER_PORT}/alfresco/api/-default-/public/cmis/versions/1.0/atom";
final String ALFRESCO42_CMIS11_ATOMPUB_URL = "http://${CMIS_SERVER_HOSTNAME}:${CMIS_SERVER_PORT}/alfresco/api/-default-/public/cmis/versions/1.1/atom";
final String ALFRESCO42_CMIS11_BROWSER_URL = "http://${CMIS_SERVER_HOSTNAME}:${CMIS_SERVER_PORT}/alfresco/api/-default-/public/cmis/versions/1.1/browser";
final String ALFRESCO50_CMIS10_ATOMPUB_URL = ALFRESCO42_CMIS10_ATOMPUB_URL;
final String ALFRESCO50_CMIS11_ATOMPUB_URL = ALFRESCO42_CMIS11_ATOMPUB_URL;
final String ALFRESCO50_CMIS11_BROWSER_URL = ALFRESCO42_CMIS11_BROWSER_URL;
final String ALFRESCOCLOUD_CMIS10_ATOMPUB_URL = "https://api.alfresco.com/cmis/versions/1.0/atom/"; // Note: OAuth authentication, which isn't supported by this script yet
final String ALFRESCOCLOUD_CMIS11_ATOMPUB_URL = "https://api.alfresco.com/cmis/versions/1.1/atom/"; // Note: OAuth authentication, which isn't supported by this script yet
final String CMIS_URL = ALFRESCO50_CMIS11_BROWSER_URL; // Switch this as you wish
final BindingType CMIS_BINDING = BindingType.BROWSER; // Make sure this is set correctly for the URL
// ---------- Script starts here ----------
println "\n CMIS Test Client"
println " ----------------\n"
println "Connecting to CMIS ${CMIS_BINDING.value()} endpoint at ${CMIS_URL}..."
Session session = createCMISSession(CMIS_URL, CMIS_BINDING, CMIS_USER, CMIS_PASSWORD);
println "Finding or creating test folder..."
Folder testFolder = findOrCreateTestFolder(session);
println "Creating test file..."
Document testFile = createTestFile(session, testFolder);
println "Done!"
// ---------- Script ends here ----------
// ---------- From here down are stateless functions ----------
Session createCMISSession(final String cmisEndpointUrl,
final BindingType cmisBinding,
final String cmisUser,
final String cmisPassword)
{
SessionFactory sf = SessionFactoryImpl.newInstance();
Session result = null;
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(SessionParameter.BINDING_TYPE, cmisBinding.value());
if (cmisBinding == BindingType.ATOMPUB)
{
parameters.put(SessionParameter.ATOMPUB_URL, cmisEndpointUrl);
}
else if (cmisBinding == BindingType.BROWSER)
{
parameters.put(SessionParameter.BROWSER_URL, cmisEndpointUrl);
}
parameters.put(SessionParameter.USER, cmisUser);
parameters.put(SessionParameter.PASSWORD, cmisPassword);
// Note: grabbing the first repository may not work as expected on multi-tenant Alfresco (most notably Cloud)
result = sf.getRepositories(parameters).get(0).createSession();
return(result);
}
/**
* Find or create a test folder.
* Note: NOT RACE CONDITION TOLERANT!!
*/
Folder findOrCreateTestFolder(Session session)
{
Folder result = null;
// Note: this code is NOT safe from race conditions (i.e. two or more threads or processes independently execute this logic at the same time). That logic needs to be added (i.e. check for a duplicate error and retry the find loop).
result = findTestFolder(session);
// We didn't find the test folder, so create it.
if (result == null)
{
result = createTestFolder(session);
}
return(result);
}
Folder findTestFolder(Session session)
{
Folder result = null;
try
{
CmisObject testFolder = session.getObjectByPath("/cmis_test");
if (testFolder != null &&
BaseTypeId.CMIS_FOLDER.equals(testFolder.getBaseTypeId()))
{
result = (Folder)testFolder;
}
}
catch (CmisObjectNotFoundException confe)
{
// Swallow and move on - we return null in this case
}
return(result);
}
Folder createTestFolder(Session session)
{
Folder result = null;
Map<String, Object> properties = new HashMap<String, Object>();
// properties.put(PropertyIds.OBJECT_TYPE_ID, ObjectType.FOLDER_BASETYPE_ID); // As of OpenCMIS v0.11.0 ObjectType no longer has useful constants like this in it.
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, "cmis_test");
result = session.getRootFolder().createFolder(properties);
return(result);
}
Document createTestFile(Session session, Folder folder)
{
String fileName = "cmis_test_${System.currentTimeMillis()}.txt";
String content = "This is ${fileName}, created ${new Date()}.";
Document result = null;
ContentStream stream = null;
Map<String, Object> properties = new HashMap<String, Object>();
stream = new ContentStreamImpl(fileName, "text/plain", content);
// properties.put(PropertyIds.OBJECT_TYPE_ID, ObjectType.DOCUMENT_BASETYPE_ID); // As of OpenCMIS v0.11.0 ObjectType no longer has useful constants like this in it.
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, fileName);
result = folder.createDocument(properties, stream, VersioningState.MAJOR);
return(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment