cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco Open CMIS Class Cast Exception

rutaveejshah
Champ in-the-making
Champ in-the-making
Hello!.
I am creating a new folder using Open CMIS. But i got error like class Cast Exception.
Below is my code.

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alfresco.cmis.client.AlfrescoFolder;
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.enums.BindingType;
private Session getSession(String serverUrl, String username, String password)
      {
      SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
      Map<String, String> params = new HashMap<String, String>();
      params.put(SessionParameter.USER, username);
      params.put(SessionParameter.PASSWORD, password);
      params.put(SessionParameter.ATOMPUB_URL, serverUrl);
      params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
      List<Repository> repos = sessionFactory.getRepositories(params);
      if (repos.isEmpty()) {
      throw new RuntimeException("Server has no repositories!");
      }
      return repos.get(0).createSession();
      }
public void createFolder()
      {
         String servalUrl="http://localhost:8080/alfresco/service/cmis";
         String userName="admin";
         String password="admin";
      
         Map<String, Object> properties = new HashMap<String, Object>();
         properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder,P:cm:titled");
         properties.put(PropertyIds.NAME, "TestFolder3");
         properties.put(PropertyIds.CREATED_BY, "admin");
         properties.put("cm:title", "Title");
         properties.put("cm:description", "Desc");
         
         AlfrescoFolder alfFolder=(AlfrescoFolder)getSession(servalUrl, userName,password).getObjectByPath("/");
         AlfrescoFolder newFolder=(AlfrescoFolder) alfFolder.createFolder(properties);
         System.out.println(newFolder.getName());

      }



When i run this code i got the below error.
Exception in thread "main" java.lang.ClassCastException: org.apache.chemistry.opencmis.client.runtime.FolderImpl cannot be cast to org.alfresco.cmis.client.AlfrescoFolder
   at com.tcs.alfresco.sample.Test.createFolder(Test.java:59)
   at com.tcs.alfresco.sample.SampleTest.main(SampleTest.java:8)

Please if any body have any idea please reply.
Thanks in advance.
6 REPLIES 6

kaynezhang
World-Class Innovator
World-Class Innovator

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

      params.put(SessionParameter.USER, username);

      params.put(SessionParameter.PASSWORD, password);

      params.put(SessionParameter.ATOMPUB_URL, serverUrl);

      params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
<strong>
// set the alfresco object factory
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
</strong>

      List<Repository> repos = sessionFactory.getRepositories(params);

Thanks for your reply.
I have tried your suggestion.It is work for me.But i get some other exception.I am using alfresco community edition 4.0d.
Can you just check mu code and  tell me where i made mistake?
MY code is:
<code>
private Session getSession(String serverUrl, String username, String password)
      {
      SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
      Map<String, String> params = new HashMap<String, String>();
      params.put(SessionParameter.USER, username);
      params.put(SessionParameter.PASSWORD, password);
      params.put(SessionParameter.ATOMPUB_URL, serverUrl);
      params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
      params.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");         
      List<Repository> repos = sessionFactory.getRepositories(params);
      if (repos.isEmpty()) {
      throw new RuntimeException("Server has no repositories!");
      }
      return repos.get(0).createSession();
      }
               public void  createFolder()
      {
         String servalUrl="http://localhost:8080/alfresco/service/cmis";
         String userName="admin";
         String password="admin";
      
         Map<String, Object> properties = new HashMap<String, Object>();
         properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder,P:cm:titled");   
         properties.put(PropertyIds.NAME,"TESTFOLDER2");
         properties.put(PropertyIds.CREATED_BY, "admin");
          properties.put("cm:title", "Title");
         properties.put("cm:description", "Desc");
         Session session=getSession(servalUrl, userName, password);
         AlfrescoFolder root=(AlfrescoFolder) session.getRootFolder();
         System.out.println("The Name:"+root.getId()+root.getName());
         AlfrescoFolder newFolder=(AlfrescoFolder)root.createFolder(properties);
         System.out.println(newFolder.getName());
         
      }


<code>

log4j:WARN No appenders could be found for logger (org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubSpi).
log4j:WARN Please initialize the log4j system properly.
The Name:workspace://SpacesStore/63dd2df1-3731-44be-a971-34d53bd99eceCompany Home
ERROR:  'prefix cannot be null or empty'
Exception in thread "main" org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException: Cannot access http://localhost:8080/alfresco/service/cmis/s/workspaceSmiley FrustratedpacesStore/i/63dd2df1-3731-44be-a971-34d53bd9...: null
   at org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils.invoke(HttpUtils.java:203)
   at org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils.invokePOST(HttpUtils.java:70)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.post(AbstractAtomPubService.java:566)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.ObjectServiceImpl.createFolder(ObjectServiceImpl.java:154)
   at org.apache.chemistry.opencmis.client.runtime.SessionImpl.createFolder(SessionImpl.java:769)
   at org.apache.chemistry.opencmis.client.runtime.FolderImpl.createFolder(FolderImpl.java:129)
   at org.apache.chemistry.opencmis.client.runtime.FolderImpl.createFolder(FolderImpl.java:453)
   at com.tcs.alfresco.sample.Test.createFolder(Test.java:63)
   at com.tcs.alfresco.sample.SampleTest.main(SampleTest.java:8)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.transform.TransformerException: javax.xml.stream.XMLStreamException: prefix cannot be null or empty]
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
   at org.apache.chemistry.opencmis.commons.impl.JaxBHelper.marshal(JaxBHelper.java:130)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AtomEntryWriter.write(AtomEntryWriter.java:146)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.ObjectServiceImpl$2.write(ObjectServiceImpl.java:156)
   at org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils.invoke(HttpUtils.java:180)
   … 8 more
Caused by: javax.xml.transform.TransformerException: javax.xml.stream.XMLStreamException: prefix cannot be null or empty
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.writeDom(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
   … 14 more
Caused by: javax.xml.transform.TransformerException: javax.xml.stream.XMLStreamException: prefix cannot be null or empty
   at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
   … 29 more
Caused by: javax.xml.stream.XMLStreamException: prefix cannot be null or empty
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.attribute(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.ContentHandlerAdaptor.startElement(Unknown Source)
   at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.closeStartTag(Unknown Source)
   at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.flushPending(Unknown Source)
   at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement(Unknown Source)
   at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
   at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
   … 31 more
Caused by: javax.xml.stream.XMLStreamException: prefix cannot be null or empty
   at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeAttribute(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput.attribute(Unknown Source)
   … 43 more

I got the exception Like this.

kaynezhang
World-Class Innovator
World-Class Innovator
for alfresco 4.0
<strong>
parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/alfresco/cmisatom");
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
</strong>

I have implemented your suggestion but stil it give me error.
Please if you have any solution than provide me on this.
Thanks.

kaynezhang
World-Class Innovator
World-Class Innovator
I have ran your code and didn't get any error.I use version 4.2.c ,I think maybe it's a bug of 4.0.d.

sindhu
Champ in-the-making
Champ in-the-making
I am getting forbidden exception when trying to set the object factory class. Please help me!