CMIS webservice client - org.xml.sax.SAXException

266 views
Skip to first unread message

Nikesh

unread,
Feb 17, 2010, 12:01:08 AM2/17/10
to CMIS Interoperability
Dear All,

I tried to execute webservice client (Apache Axis) against Alfresco
community version 3.2r2, and I am getting following exception:

10:27:33,027 ERROR [axis.client.Call] Exception:
org.xml.sax.SAXException: SimpleDeserializer encountered a child
element, which is NOT expected, in something it was trying to
deserialize.
at
org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:
145)
at
org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:
1035)
at
org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:
165)
at
org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:
1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:
236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at CMIS.CMISTest.main(CMISTest.java:120)
Remote exception : ; nested exception is:
org.xml.sax.SAXException: SimpleDeserializer encountered a child
element, which is NOT expected, in something it was trying to
deserialize.

JAVA Code:

package CMIS;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.ws.security.message.token.UsernameToken;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.util.WSSecurityUtil;
import org.w3c.dom.Document;
import org.apache.ws.security.message.token.Timestamp;

public class CMISTest {
public static void main(String [] args) {
try {

String endpoint = "http://localhost:8080/alfresco/cmis/
RepositoryService";
// String repID = "/app:company_home/cm:Shivam";
String repID ="46f8b242-9f61-423b-b824-c6118f470f01";


Service serv = new Service();
Call call = null;
try {
call = (Call) serv.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
} catch (ServiceException e) {
System.err.println("Servic call create error");
} catch (MalformedURLException e) {
System.err.println("Malformed url");
}

call.setOperationName(new QName("http://www.cmis.org/2008/05",
"getRepositories"));
call.addParameter("id", new QName("http://www.w3.org/2001/XMLSchema",
"string"), ParameterMode.IN);
call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
"string"));

SOAPEnvelope se = new SOAPEnvelope(); // dummy envelope to create
Header
Document doc = null;
try {
doc = se.getAsDocument();
} catch (Exception e) {
System.err.println("Exception caught while getting the doc");
}

WSSecHeader secHeader = new WSSecHeader();
secHeader.setMustUnderstand(true);
secHeader.insertSecurityHeader(doc);

UsernameToken username = new UsernameToken(Boolean.TRUE, doc,
WSConstants.PASSWORD_TEXT);
username.setName("admin");
username.setPassword("admin");
Timestamp ts = new Timestamp(true, doc, 300);

WSSecurityUtil.prependChildElement(doc,
secHeader.getSecurityHeader(),
username.getElement(), false);
WSSecurityUtil.prependChildElement(doc,
secHeader.getSecurityHeader(),
ts.getElement(), false);

SOAPHeaderElement she = new SOAPHeaderElement(secHeader
.getSecurityHeader());
call.addHeader(she);
String ret = null;
try {
//call.invokeOneWay(new Object[] { repID});
ret = call.invoke(new Object[] { repID }).toString();

} catch (Exception e) {
System.out.println("Inside remote");
System.err.println("Remote exception : " + e.getMessage());
}
System.out.println(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Kindly suggest how to resolve it.

Thanks,
Nikesh

David Caruana

unread,
Feb 18, 2010, 10:42:27 AM2/18/10
to cmis-i...@googlegroups.com
Nikesh,

Your usage of AXIS is incorrect. You assume the response is a string, but it contains the following structure: 
<repositories> 
  <repositoryId>0d599136-0485-4258-ad8b-0edd456d2aa3</repositoryId> 
  <repositoryName>Main Repository</repositoryName> 
</repositories> 

See fix for your code below. It contains deserialization of CmisRepositoryEntryType. 

In general it is not good practice to manually code for xml serialization/deserialization. AXIS allows to generate stubs, skeletons, and data types from WSDL using WSDL2Java tool. 

You can see Alfresco CMIS tests as example of CMIS AXIS client: 
http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/cmis-tck-ws/ 

Regards,
Dave

package CMIS;

import java.net.MalformedURLException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
import org.apache.axis.encoding.ser.ArraySerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.message.token.Timestamp;
import org.apache.ws.security.message.token.UsernameToken;
import org.apache.ws.security.util.WSSecurityUtil;
import org.w3c.dom.Document;

public class CMISTest
{
    public static void main(String[] args)
    {
        try
        {
            String endpoint = "http://localhost:8080/alfresco/cmis/RepositoryService";

            Service serv = new Service();
            Call call = null;
            try
            {
                call = (Call) serv.createCall();
                call.setTargetEndpointAddress(new java.net.URL(endpoint));
            }
            catch (ServiceException e)
            {
                System.err.println("Servic call create error");
            }
            catch (MalformedURLException e)
            {
                System.err.println("Malformed url");
            }
            
            ParameterDesc param;
            OperationDesc oper = new OperationDesc();
            oper.setName("getRepositories");
            param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "getRepositories"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", ">getRepositories"));
            oper.addParameter(param); 
            oper.setReturnType(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", ">getRepositoriesResponse"));
            oper.setReturnClass(CmisRepositoryEntryType[].class);
            oper.setReturnQName(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "getRepositoriesResponse"));
            param = oper.getReturnParamDesc();
            param.setItemQName(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "repositories"));
            oper.setStyle(org.apache.axis.constants.Style.DOCUMENT);
            oper.setUse(org.apache.axis.constants.Use.LITERAL);
            
            call.setOperationName(new javax.xml.namespace.QName("", "getRepositories"));
            call.setOperation(oper);
            
            QName qName = new QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", ">getRepositoriesResponse");
            call.registerTypeMapping(CmisRepositoryEntryType[].class, qName, ArraySerializerFactory.class, ArrayDeserializerFactory.class);

            qName = new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "cmisRepositoryEntryType");
            call.registerTypeMapping(CmisRepositoryEntryType.class, qName, BeanSerializerFactory.class, BeanDeserializerFactory.class);
            
            SOAPEnvelope se = new SOAPEnvelope(); // dummy envelope to create Header
            Document doc = null;
            try
            {
                doc = se.getAsDocument();
            }
            catch (Exception e)
            {
                System.err.println("Exception caught while getting the doc");
            }

            WSSecHeader secHeader = new WSSecHeader();
            secHeader.setMustUnderstand(true);
            secHeader.insertSecurityHeader(doc);

            UsernameToken username = new UsernameToken(Boolean.TRUE, doc, WSConstants.PASSWORD_TEXT);
            username.setName("admin");
            username.setPassword("admin");
            Timestamp ts = new Timestamp(true, doc, 300);

            WSSecurityUtil.prependChildElement(doc, secHeader.getSecurityHeader(), username.getElement(), false);
            WSSecurityUtil.prependChildElement(doc, secHeader.getSecurityHeader(), ts.getElement(), false);

            SOAPHeaderElement she = new SOAPHeaderElement(secHeader.getSecurityHeader());
            call.addHeader(she);
            CmisRepositoryEntryType[] ret = null;
            try
            {
                ret = (CmisRepositoryEntryType[]) call.invoke(new Object[] { null });

            }
            catch (Exception e)
            {
                System.out.println("Inside remote");
                System.err.println("Remote exception : " + e.getMessage());
            }
            
            for (CmisRepositoryEntryType repository : ret)
            {
                System.out.println("Repository [Name=" + repository.getRepositoryName() + ", id=" + repository.getRepositoryId() + "]");
            }
        }
        catch (Exception e)
        {

            e.printStackTrace();

        }
    }
}


/**
 * CmisRepositoryEntryType.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */

package CMIS;

public class CmisRepositoryEntryType implements java.io.Serializable, org.apache.axis.encoding.AnyContentType
{

    private static final long serialVersionUID = 9092332052641904923L;

    private java.lang.String repositoryId;

    private java.lang.String repositoryName;

    private org.apache.axis.message.MessageElement[] _any;

    public CmisRepositoryEntryType()
    {
    }

    public CmisRepositoryEntryType(java.lang.String repositoryId, java.lang.String repositoryName, org.apache.axis.message.MessageElement[] _any)
    {
        this.repositoryId = repositoryId;
        this.repositoryName = repositoryName;
        this._any = _any;
    }

    /**
     * Gets the repositoryId value for this CmisRepositoryEntryType.
     * 
     * @return repositoryId
     */
    public java.lang.String getRepositoryId()
    {
        return repositoryId;
    }

    /**
     * Sets the repositoryId value for this CmisRepositoryEntryType.
     * 
     * @param repositoryId
     */
    public void setRepositoryId(java.lang.String repositoryId)
    {
        this.repositoryId = repositoryId;
    }

    /**
     * Gets the repositoryName value for this CmisRepositoryEntryType.
     * 
     * @return repositoryName
     */
    public java.lang.String getRepositoryName()
    {
        return repositoryName;
    }

    /**
     * Sets the repositoryName value for this CmisRepositoryEntryType.
     * 
     * @param repositoryName
     */
    public void setRepositoryName(java.lang.String repositoryName)
    {
        this.repositoryName = repositoryName;
    }

    /**
     * Gets the _any value for this CmisRepositoryEntryType.
     * 
     * @return _any
     */
    public org.apache.axis.message.MessageElement[] get_any()
    {
        return _any;
    }

    /**
     * Sets the _any value for this CmisRepositoryEntryType.
     * 
     * @param _any
     */
    public void set_any(org.apache.axis.message.MessageElement[] _any)
    {
        this._any = _any;
    }

    // Type metadata
    private static org.apache.axis.description.TypeDesc typeDesc = new org.apache.axis.description.TypeDesc(CmisRepositoryEntryType.class, true);

    static
    {
        typeDesc.setXmlType(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "cmisRepositoryEntryType"));
        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("repositoryId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "repositoryId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("repositoryName");
        elemField.setXmlName(new javax.xml.namespace.QName("http://docs.oasis-open.org/ns/cmis/messaging/200908/", "repositoryName"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
    }

    /**
     * Return type metadata object
     */
    public static org.apache.axis.description.TypeDesc getTypeDesc()
    {
        return typeDesc;
    }

    /**
     * Get Custom Serializer
     */
    public static org.apache.axis.encoding.Serializer getSerializer(java.lang.String mechType, java.lang.Class _javaType, javax.xml.namespace.QName _xmlType)
    {
        return new org.apache.axis.encoding.ser.BeanSerializer(_javaType, _xmlType, typeDesc);
    }

    /**
     * Get Custom Deserializer
     */
    public static org.apache.axis.encoding.Deserializer getDeserializer(java.lang.String mechType, java.lang.Class _javaType, javax.xml.namespace.QName _xmlType)
    {
        return new org.apache.axis.encoding.ser.BeanDeserializer(_javaType, _xmlType, typeDesc);
    }

}




--
You received this message because you are subscribed to the Google Groups "CMIS Interoperability" group.
To post to this group, send email to cmis-i...@googlegroups.com.
To unsubscribe from this group, send email to cmis-interop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cmis-interop?hl=en.


Nikesh

unread,
Feb 19, 2010, 6:26:40 AM2/19/10
to CMIS Interoperability
Thanks dave

It worked....

On Feb 18, 8:42 pm, David Caruana <david.caru...@alfresco.com> wrote:
> Nikesh,
>
> Your usage of AXIS is incorrect. You assume the response is a string, but it contains the following structure:
> <repositories>
>   <repositoryId>0d599136-0485-4258-ad8b-0edd456d2aa3</repositoryId>
>   <repositoryName>Main Repository</repositoryName>
> </repositories>
>
> See fix for your code below. It contains deserialization of CmisRepositoryEntryType.
>
> In general it is not good practice to manually code for xml serialization/deserialization. AXIS allows to generate stubs, skeletons, and data types from WSDL using WSDL2Java tool.
>

> You can see Alfresco CMIS tests as example of CMIS AXIS client:http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root...

> ...
>
> read more »

Reply all
Reply to author
Forward
0 new messages