You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Michael Bernagou <bl...@gmail.com> on 2007/11/20 11:45:34 UTC
[Axiom]Send receive an object with an ArrayList
Hi,
I'm using Axiom method to sendReceive a list.
In fact, at the client level I call the server via a web service to get a
list of user :
Client :
public static List<UserLite> getUserList(String login, char[] password)
throws PapoException {
EndpointReference targetEPR =3D new EndpointReference(baseUrl +
CoreVariables.APPLICATION_WEBSERVICE);
OMFactory fac =3D OMAbstractFactory.getOMFactory();
OMNamespace omNs =3D fac.createOMNamespace("
http://webservices.papo/xsd", "tns");
OMElement method =3D fac.createOMElement("getUserList", omNs);
OMElement loginElement =3D fac.createOMElement("login", omNs);
loginElement.addChild(fac.createOMText(loginElement, login));
method.addChild(loginElement);
OMElement passwordElement =3D fac.createOMElement("password", omNs)=
;
passwordElement.addChild(fac.createOMText(passwordElement, new
String(password)));
method.addChild(passwordElement);
Options options =3D new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
try {
ServiceClient sender =3D new ServiceClient();
sender.setOptions(options);
OMElement result =3D sender.sendReceive(method);
OMElement firstElement =3D result.getFirstElement();
if(firstElement.getLocalName().equals("message")) {
throw new PapoException(firstElement.getText());
}
else {
OMElement userList =3D result.getFirstElement();
if (!userList.getText().equals("UserList")) {
throw new PapoException("The xml receive is different
from what expected. Unknown server response!!");
}
userList.getChildElements();
OMElement firstNameElement =3D
(OMElement)loginElement.getNextOMSibling();
String firstName =3D firstNameElement.getText();
OMElement lastNameElement =3D
(OMElement)firstElement.getNextOMSibling();
String lastName =3D lastNameElement.getText();
OMElement emailElement =3D
(OMElement)lastNameElement.getNextOMSibling();
String email =3D emailElement.getText();
MainUser user =3D new MainUser(login);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
return new ArrayList();
}
}
catch (AxisFault af) {
af.printStackTrace();
throw new PapoException(af.getMessage(), af);
}
}
I have a ClassCastException for this line : OMElement result =3D
sender.sendReceive(method);
The web service is :
public OMElement getUserList(OMElement element) throws XMLStreamException {
// Something that get a user object
if (user !=3D null) {
ApplicationServiceImpl asi =3D new ApplicationServiceImpl();
try {
// <UserList>
OMElement userListElement =3D fac.createOMElement("UserList", omNs)=
;
UserList userList =3D asi.getUserList();
for (int i =3D 0; i < userList.getUserList().size(); i++) {
UserLite userLite =3D userList.getUserList().get(i);
// <User>
OMElement userElement =3D fac.createOMElement("User", omNs);
// <login>String</login>
loginElement =3D fac.createOMElement("login", omNs);
loginElement.addChild(fac.createOMText(loginElement,
userLite.getLogin()));
userElement.addChild(loginElement);
// <status>boolean</status>
OMElement statusElement =3D fac.createOMElement("status", omNs);
statusElement.addChild(fac.createOMText(statusElement,
userLite.isAlive()));
userElement.addChild(statusElement);
// <ip>String</ip>
OMElement ipElement =3D fac.createOMElement("ip", omNs);
ipElement.addChild(fac.createOMText(ipElement, userLite.getLastIP
()));
userElement.addChild(ipElement);
// <lastUpdate>Date.toString</lastUpdate>
OMElement lastUpdateElement =3D fac.createOMElement("lastUpdate",
omNs);
lastUpdateElement.addChild(fac.createOMText(lastUpdateElement,
userLite.getLastUpdate()));
userElement.addChild(lastUpdateElement);
// </User>
userListElement.addChild(userElement);
}
logger.debug("\tListe des utilisateurs cr=E9=E9 sous forme de messa=
ge.
");
// </UserList>
method.addChild(userListElement);
}
catch (PapoException pe) {
OMElement message =3D fac.createOMElement("errorMessage", omNs);
message.addChild(fac.createOMText(message, "Unable to get the
refreshed user list! Try again later or check the server status"));
method.addChild(message);
}
}
else {
OMElement message =3D fac.createOMElement("errorMessage", omNs);
message.addChild(fac.createOMText(message, "Unable to get the
refreshed user list! Try again later or check the server status"));
method.addChild(message);
}
return method;
}
In debugging mode, this last code is executed normally and completely
without any error (when the client call it). I logged the list and it is ok=
,
then it is send to the client as expected...
What is strange is I used exactly the same king of code to authenticate a
user and receive a complete user object. The soap message should have this
kind of structure :
<UserList>
<User>
<login>test1</login>
<status>ONLINE</status>
<ip>127.0.0.1</ip>
<lastUpdate></lastUpdate>
</User>
<User>
<login>test2</login>
<status>ONLINE</status>
<ip>127.0.0.1</ip>
<lastUpdate></lastUpdate>
</User>
...
</UserList>
So, why this ClassCastException ?
Thanks!
--
Michael Bernagou
Java Developper
Re: [Axiom]Send receive an object with an ArrayList
Posted by Michael Bernagou <bl...@gmail.com>.
Hum, just a word to say I know my code is not finished to create the list
received. Anyway the error comes immediately after de sendReceive(method)
call.
I tried to explicitely force having ArrayList instead of List, without
success...
2007/11/20, Michael Bernagou <bl...@gmail.com>:
>
> Hi,
>
> I'm using Axiom method to sendReceive a list.
> In fact, at the client level I call the server via a web service to get a
> list of user :
>
> Client :
>
> public static List<UserLite> getUserList(String login, char[] password)
> throws PapoException {
> EndpointReference targetEPR =3D new EndpointReference(baseUrl +
> CoreVariables.APPLICATION_WEBSERVICE);
> OMFactory fac =3D OMAbstractFactory.getOMFactory ();
> OMNamespace omNs =3D fac.createOMNamespace("
> http://webservices.papo/xsd", "tns");
>
> OMElement method =3D fac.createOMElement("getUserList", omNs);
>
> OMElement loginElement =3D fac.createOMElement("login", omNs);
> loginElement.addChild(fac.createOMText(loginElement, login));
> method.addChild(loginElement);
>
> OMElement passwordElement =3D fac.createOMElement("password",
> omNs)=
> ;
> passwordElement.addChild(fac.createOMText (passwordElement, new
> String(password)));
> method.addChild(passwordElement);
> Options options =3D new Options();
> options.setTo(targetEPR);
> options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
>
> try {
> ServiceClient sender =3D new ServiceClient();
> sender.setOptions(options);
> OMElement result =3D sender.sendReceive(method);
> OMElement firstElement =3D result.getFirstElement();
> if(firstElement.getLocalName().equals("message")) {
> throw new PapoException(firstElement.getText());
> }
> else {
> OMElement userList =3D result.getFirstElement();
> if (!userList.getText().equals("UserList")) {
> throw new PapoException("The xml receive is different
> from what expected. Unknown server response!!");
> }
>
> userList.getChildElements();
> OMElement firstNameElement =3D
> (OMElement)loginElement.getNextOMSibling();
> String firstName =3D firstNameElement.getText();
>
> OMElement lastNameElement =3D
> (OMElement)firstElement.getNextOMSibling();
> String lastName =3D lastNameElement.getText ();
>
> OMElement emailElement =3D
> (OMElement)lastNameElement.getNextOMSibling();
> String email =3D emailElement.getText();
>
> MainUser user =3D new MainUser(login);
> user.setFirstName(firstName);
> user.setLastName(lastName);
> user.setEmail(email);
>
> return new ArrayList();
>
> }
> }
> catch (AxisFault af) {
> af.printStackTrace();
> throw new PapoException(af.getMessage(), af);
> }
>
> }
>
> I have a ClassCastException for this line : OMElement result =3D
> sender.sendReceive(method);
>
> The web service is :
>
> public OMElement getUserList(OMElement element) throws XMLStreamException
> {
> // Something that get a user object
> if (user !=3D null) {
> ApplicationServiceImpl asi =3D new ApplicationServiceImpl();
>
> try {
> // <UserList>
> OMElement userListElement =3D fac.createOMElement("UserList",
> omNs)=
> ;
> UserList userList =3D asi.getUserList();
> for (int i =3D 0; i < userList.getUserList().size(); i++) {
> UserLite userLite =3D userList.getUserList().get(i);
> // <User>
> OMElement userElement =3D fac.createOMElement("User", omNs);
>
>
> // <login>String</login>
> loginElement =3D fac.createOMElement("login", omNs);
> loginElement.addChild(fac.createOMText (loginElement,
> userLite.getLogin()));
> userElement.addChild(loginElement);
>
> // <status>boolean</status>
> OMElement statusElement =3D fac.createOMElement("status", omNs);
> statusElement.addChild(fac.createOMText(statusElement,
> userLite.isAlive()));
> userElement.addChild(statusElement);
>
> // <ip>String</ip>
> OMElement ipElement =3D fac.createOMElement("ip", omNs);
> ipElement.addChild(fac.createOMText(ipElement, userLite.getLastIP
> ()));
> userElement.addChild(ipElement);
>
> // <lastUpdate>Date.toString </lastUpdate>
> OMElement lastUpdateElement =3D fac.createOMElement("lastUpdate",
> omNs);
> lastUpdateElement.addChild(fac.createOMText(lastUpdateElement,
> userLite.getLastUpdate()));
> userElement.addChild(lastUpdateElement);
>
> // </User>
> userListElement.addChild(userElement);
> }
> logger.debug("\tListe des utilisateurs cr=E9=E9 sous forme de
> messa=
> ge.
> ");
> // </UserList>
> method.addChild(userListElement);
> }
> catch (PapoException pe) {
> OMElement message =3D fac.createOMElement("errorMessage", omNs);
> message.addChild(fac.createOMText(message, "Unable to get the
> refreshed user list! Try again later or check the server status"));
> method.addChild(message);
> }
> }
> else {
> OMElement message =3D fac.createOMElement("errorMessage", omNs);
> message.addChild(fac.createOMText(message, "Unable to get the
> refreshed user list! Try again later or check the server status"));
> method.addChild(message);
> }
> return method;
> }
>
> In debugging mode, this last code is executed normally and completely
> without any error (when the client call it). I logged the list and it is
> ok=
> ,
> then it is send to the client as expected...
>
> What is strange is I used exactly the same king of code to authenticate a
> user and receive a complete user object. The soap message should have this
> kind of structure :
>
> <UserList>
> <User>
> <login>test1</login>
> <status>ONLINE</status>
> <ip> 127.0.0.1</ip>
> <lastUpdate></lastUpdate>
> </User>
> <User>
> <login>test2</login>
> <status>ONLINE</status>
> <ip> 127.0.0.1</ip>
> <lastUpdate></lastUpdate>
> </User>
> ...
> </UserList>
>
> So, why this ClassCastException ?
>
> Thanks!
>
> --
> Michael Bernagou
> Java Developper
--
Michael Bernagou
Java Developper