public
Last active

Creating aspect-defined properties in CMIS with Alfresco

  • Download Gist
AspectExample.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
package com.someco.cmis.examples;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
 
/**
* Example showing how to set the description and title for a folder and document in Alfresco via CMIS.
*
* @author jpotts
*/
public class AspectExample {
private static final String CM_TITLE = "cm:title";
private static final String CM_DESCRIPTION = "cm:description";
private static final String TEST_TITLE = "this is my title";
private static final String TEST_DESCRIPTION = "this is my description";
private static final String SERVICE_URL = "http://localhost:8080/alfresco/cmisatom";
private static final String USAGE = "java AspectExample <username> <password> <non-root folder path> <content name>";
private static final String NON_ROOT = "Specify a target base folder other than root";
 
private Session session = null;
private String user;
private String password;
private String folderPath;
private String contentName;
public static void main(String[] args) throws Exception {
if (args.length != 4) doUsage(AspectExample.USAGE);
if (args[2].equals("/")) doUsage(AspectExample.NON_ROOT);
AspectExample ae = new AspectExample();
ae.setUser(args[0]);
ae.setPassword(args[1]);
ae.setFolderPath(args[2]);
ae.setContentName(args[3]);
ae.doExample();
}
public void doExample() {
createTestDoc(getFolderPath(), getContentName());
return;
}
public Session getSession() {
if (this.session == null) {
// default factory implementation
SessionFactory factory = SessionFactoryImpl.newInstance();
Map<String, String> parameter = new HashMap<String, String>();
// user credentials
parameter.put(SessionParameter.USER, getUser());
parameter.put(SessionParameter.PASSWORD, getPassword());
// connection settings
parameter.put(SessionParameter.ATOMPUB_URL, getServiceUrl());
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
// Set the alfresco object factory
// Used when using the CMIS extension for Alfresco for working with aspects
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
List<Repository> repositories = factory.getRepositories(parameter);
this.session = repositories.get(0).createSession();
}
return this.session;
}
public Folder getTestFolder(String folderPath) throws CmisObjectNotFoundException {
Session session = getSession();
 
// Grab a reference to the folder where we want to create content
Folder folder = null;
try {
folder = (Folder) session.getObjectByPath(folderPath);
System.out.println("Found folder: " + folder.getName() + "(" + folder.getId() + ")");
} catch (CmisObjectNotFoundException confe) {
Folder targetBaseFolder = null;
String baseFolderPath = folderPath.substring(0, folderPath.lastIndexOf('/') + 1);
String folderName = folderPath.substring(folderPath.lastIndexOf('/') + 1);
//if this one is not found, we'll let the exception bubble up
targetBaseFolder = (Folder) session.getObjectByPath(baseFolderPath);
 
// Create a Map of objects with the props we want to set
Map <String, Object> properties = new HashMap<String, Object>();
// Following sets the content type and adds the webable and productRelated aspects
// This works because we are using the OpenCMIS extension for Alfresco
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder, P:cm:titled");
properties.put(PropertyIds.NAME, folderName);
 
properties.put(CM_DESCRIPTION, TEST_DESCRIPTION);
properties.put(CM_TITLE, TEST_TITLE);
folder = targetBaseFolder.createFolder(properties);
System.out.println("Created folder: " + folder.getName() + " (" + folder.getId() + ")");
}
return folder;
}
public Document createTestDoc(String folderPath, String docName) {
// Grab a reference to the folder where we want to create content
Folder folder = getTestFolder(folderPath);
// Set up a name for the test document
String timeStamp = new Long(System.currentTimeMillis()).toString();
String fileName = docName + " (" + timeStamp + ")";
// Create a Map of objects with the props we want to set
Map <String, Object> properties = new HashMap<String, Object>();
 
// Following sets the content type and adds the webable and productRelated aspects
// This works because we are using the OpenCMIS extension for Alfresco
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document, P:cm:titled");
properties.put(PropertyIds.NAME, fileName);
properties.put(CM_DESCRIPTION, TEST_DESCRIPTION);
properties.put(CM_TITLE, TEST_TITLE);
// Set the content text
String docText = "This is a sample document called " + docName;
byte[] content = docText.getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = new ContentStreamImpl(fileName, BigInteger.valueOf(content.length), "text/plain", stream);
 
// Create the document
Document doc = folder.createDocument(
properties,
contentStream,
VersioningState.MAJOR);
System.out.println("Created content: " + doc.getName() + "(" + doc.getId() + ")");
System.out.println("Content Length: " + doc.getContentStreamLength());
return doc;
}
 
public String getServiceUrl() {
return SERVICE_URL;
}
 
public String getContentName() {
return this.contentName;
}
 
public void setContentName(String contentName) {
this.contentName = contentName;
}
public static void doUsage(String message) {
System.out.println(message);
System.exit(0);
}
public String getPassword() {
return password;
}
 
public void setPassword(String password) {
this.password = password;
}
 
public String getUser() {
return user;
}
 
public void setUser(String user) {
this.user = user;
}
 
public String getFolderPath() {
return folderPath;
}
 
public void setFolderPath(String folderPath) {
this.folderPath = folderPath;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.