9
votes

I work with Alfresco Community 4.0.

I used cmis to update a document in Alfresco.

I have already registered a document in Alfresco and this is the document id that is retrieved after the save method: b08e8bce-1b88-489e-a357-1e6385f180a1

Now I want to change the content of this file by other content. I used this method:

   public void saveVersioning(File file, String filename, String userName, String pwd, String docId)
        throws Exception {


        SessionFactory factory = SessionFactoryImpl.newInstance();
        Map<String, String> parameters = new HashMap<String, String>();

        // User credentials.
        parameters.put(SessionParameter.USER,userName);
        parameters.put(SessionParameter.PASSWORD, pwd);

        // Connection settings.
        parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

        parameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/service/cmis"); // URL to your CMIS server.

        // Create session.
        // Alfresco only provides one repository.
        Repository repository = factory.getRepositories(parameters).get(0);

        Session session = repository.createSession();

        System.out.println("Connected to repository:" + session.getRepositoryInfo().getName());
        System.out.println("Repository id:"+session.getRepositoryInfo().getId());

         // Get the contents of the file
        Document doc = (Document) session.getObject(docId);
        ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content
        if (contentStream != null) {


                String mimetype = "text/plain; charset=UTF-8";
                String content = "";

                if (contentStream != null) {
                    filename = contentStream.getFileName();
                    mimetype = contentStream.getMimeType();
                    content = getContentAsString(contentStream);
                    System.out.println("file name "+filename);
                    System.out.println("minetype "+mimetype);
                    System.out.println("content "+content);
                }



                String updatedContents = content + "\nLine added in new version";

                byte[] buf = updatedContents.getBytes("UTF-8");
                ByteArrayInputStream input = new ByteArrayInputStream(buf);


                contentStream = session.getObjectFactory().createContentStream(
                        filename, buf.length, mimetype, input);



                System.out.println("Document version history");
                {
                    List<Document> versions = doc.getAllVersions();
                    for (Document version : versions) {
                        System.out.println("\tname: " + version.getName());
                        System.out.println("\tversion label: " + version.getVersionLabel());
                        System.out.println("\tversion series id: " + version.getVersionSeriesId());
                        System.out.println("\tchecked out by: "
                                + version.getVersionSeriesCheckedOutBy());
                        System.out.println("\tchecked out id: "
                                + version.getVersionSeriesCheckedOutId());
                        System.out.println("\tmajor version: " + version.isMajorVersion());
                        System.out.println("\tlatest version: " + version.isLatestVersion());
                        System.out.println("\tlatest major version: " + version.isLatestMajorVersion());
                        System.out.println("\tcheckin comment: " + version.getCheckinComment());
                        System.out.println("\tcontent length: " + version.getContentStreamLength()
                                + "\n");
                    }
                }
            }



    }

I call this method using this code:

  File file = new File("C:/test.pdf");
       saveVersioning(file, "test.pdf", "admin","admin","b08e8bce-1b88-489e-a357-1e6385f180a1");

I used this jar in my java project :

enter image description here

All the jars are added to the classpath, but when I test my application I get this error:

Caused by: java.lang.NoClassDefFoundError: org/apache/chemistry/opencmis/client/api/SessionFactory

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy24.sendTransfer(Unknown Source)
    at 
    ... 111 more
Caused by: java.lang.ClassNotFoundException: org.apache.chemistry.opencmis.client.api.SessionFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    ... 124 more

My Alfresco Community 4 server contains this jar:

enter image description here

UPDATED :

I found that with Alfresco Community 4.0.0
I should use chemistry-opencmis-client 0.6.0 and alfresco-opencmis-extension-0.2.jar

also I should use this URL in my code : http://localhost:9080/alfresco/cmisatom

I tried without success to get juste the session with cmis using this code :

Map<String, String> parameter = new HashMap<String, String>();

// user credentials
        parameter.put(SessionParameter.USER, "admin");
        parameter.put(SessionParameter.PASSWORD, "admin");

        // connection settings
    parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/cmisatom");

    parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

    // create session
    SessionFactory factory = SessionFactoryImpl.newInstance();
        Session session = factory.getRepositories(parameter).get(0).createSession();

as i said I used this jar :

enter image description here

I downloaded all lib from chemistry-opencmis-client-impl-0.6.0-with-dependencies.tar.gz and I also downloaded this jar alfresco-opencmis-extension-0.2.jar

when I test I have the same error .

I added this line in my code :

parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

I think that the problem is related to missing version of jar and not related to my java code but I try to change without success this line :

parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/cmisatom");

with :

parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/service/cmis");
2
Use Maven ! It Will "less-complicate" stuff :) XDTilak Maddy

2 Answers

2
votes

I'm not sure if this is the right answer, but hopefully you'll find the error and some tips.

  1. First download the latest CMIS Workbench or download the 0.6.0 version
  2. Use the workbench to connect to your Alfresco to check if everything is working, e.g. create/read/update/delete
  3. Read a lot about CMIS here and many others..
  4. By the time you've read you'd wish that you were on a higher version of Alfresco than 4.0. And if you need a stable CMIS environment then you should upgrade to 4.2.f or even higher.
  5. Ditch your current Java project and download the 0.6.0 with dependencies fresh from the download page
  6. Get to the point where you can 'actually' read & save files.
  7. In your current setup it is not possible to save files on Alfresco and not getting the NoClassDefFoundError and on update getting the error. Either you get the error always on build or you're getting another error.
  8. Move the sessionFactory and create session code to a common init() or setupConnection() method so you don't have duplicate code and thus have more chances of making mistakes
0
votes

I will assume that you run your test with another classpath then showed on the screenshots.

Caused by: java.lang.ClassNotFoundException: org.apache.chemistry.opencmis.client.api.SessionFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)

You run your test in tomcat container? Check carefull the classpath when start the test. If classpath really has cmis dependencies then check cmis client version conflict. All conflict version could be resolve by maven (as example) with ease. I recommend to create project as maven project to void this problem. Also maven will include all cmis dependencies in classpath when you run test.