cancel
Showing results for 
Search instead for 
Did you mean: 

integrity check when adding a document

yosrioh
Confirmed Champ
Confirmed Champ
hello,

i am trying to add a document to alfresco with a java code :

<code>
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 {      
      
   
       AspectExample ae = new AspectExample();
       ae.setUser("admin");
       ae.setPassword("admin");
       ae.setFolderPath("/");
       ae.setContentName("/home/yosri/gg.jpg");
       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;
  }
<code>



but when i run i got an integrity check exception and i dont know where is the problem


<code>
nvalid property value:
   Node: workspace://SpacesStore/f1455b0d-efdb-40aa-b727-7817e0d6d5a7
   Name: /home/yosri/gg.jpg (1460478193873)
   Type: {http://www.alfresco.org/model/content/1.0}content
   Property: {http://www.alfresco.org/model/content/1.0}name
   Constraint: 03120135 Value '/home/yosri/gg.jpg (1460478193873)' is not valid as a file name. This property must be a valid file name.
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.convertStatusCode(AbstractAtomPubService.java:445)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.post(AbstractAtomPubService.java:568)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.ObjectServiceImpl.createDocument(ObjectServiceImpl.java:109)
   at org.apache.chemistry.opencmis.client.runtime.SessionImpl.createDocument(SessionImpl.java:650)
   at org.apache.chemistry.opencmis.client.runtime.FolderImpl.createDocument(FolderImpl.java:88)
   at org.apache.chemistry.opencmis.client.runtime.FolderImpl.createDocument(FolderImpl.java:443)
   at com.someco.cmis.examples.AspectExample.createTestDoc(AspectExample.java:145)
   at com.someco.cmis.examples.AspectExample.doExample(AspectExample.java:56)
   at com.someco.cmis.examples.AspectExample.main(AspectExample.java:52)
<code>
2 REPLIES 2

niketapatel
Star Contributor
Star Contributor
As you are setting your filename - "/home/yosri/gg.jpg (1460478193873)" and Slashes are not allowed in Alfresco fileName.

Please check fileName constraint in contentModel.xml

FileName regEx value is -


<value>
<![CDATA[
(.*[\"\*\\\>\<\?\/\:\|]+.*)|(.*[\.]?.*[\.]+$)|(.*[ ]+$)
]]>
</value>

thx for the answer but i have another issue is that when i try to add document to alfresco with a python code using cmis i have mimetype set to unknown.
do you you know sth to solve this issue ?