Link to home
Start Free TrialLog in
Avatar of iAwin
iAwin

asked on

XML RPC question

Hi All,

I have been asked to complete the following :

______________________________________________________________________________

Can you write a XML-RPC Handler to handle the following request..

You can supply your own system username and system password. The response
should contain these fields.

The remote-ip should be the IP address of the machine that we will run this
test on, so you will need to read this in from the Environment.

The token can be the system time in milliseconds.


# RPC Request
<?xml version="1.0"?>
<methodCall>
    <methodName>XMLPRCServer.RetrieveUserToken</methodName>
    <params>
        <param>
            <value>
                <struct>
                    <member>
                        <name>username</name>
                        <value>
                            <string>system username </string>
                        </value>
                    </member>
                    <member>
                        <name>password</name>
                        <value>
                            <string>system-password </string>
                        </value>
                    </member>
                    <member>
                        <name>remote-ip</name>
                        <value>
                            <string>calling-system-ip-address </string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
        <param>
            <value>
                <struct>
                    <member>
                        <name>token</name>
                        <value>
                            <string>363795792015</string>
                        </value>
                    </member>
                    <member>
                        <name>username</name>
                        <value> system username </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodCall>


So your handler class should build the response to look like this. You can
supply a hardcoded msisdn in the 'DefaultMSISDN' field, or use the one
provided below.


The response should be of the following format...

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
    <params>
        <param>
            <value>
                <struct>
                    <member>
                        <name>
                            Username
                        </name>
                        <value>
                            <string>
                            Supplied username
                            </string>
                        </value>
                    </member>
                    <member>
                        <name>
                            Password
                        </name>
                        <value>
                            <string>
                                supplied
password
                            </string>
                        </value>
                    </member>
                    <member>
                        <name>
                            DefaultMSISDN
                        </name>
                        <value>
                        <string>
                            447967828974
                        </string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodResponse>


You will also need to provide a test class to build the request and send it
to the Handler. This should be in the format of a JUnit test.


So to help, the API's to use / research are:

http://ws.apache.org/xmlrpc/

All you need to provide to us is the Handler class to process this request
and a JUnit test class to send the request to the Handler.

This test class should then validate the fields in the response.

___________________________________________________________________________

This is not homework, I am in full time employment but XML RPC is relatively new to me and I need a solution quickly. I should be ok to handle the JUnit part myself but any help/code on the XML RPC part is greatly appreciated.

Thanks
Avatar of ellandrd
ellandrd
Flag of Ireland image

i have just completed a project using the XML-RPC class.

Here is a nice example that i used to get my head around it:  http://www.wordtracker.com/docs/api/ch03s02.html

If your really in trouble i will help you code something but at least give it a try yourself...
Avatar of iAwin
iAwin

ASKER


Sorry but im pretty lost, I would like to get the above working with code and then understand what the workings are. Is the above a complex program to complete ?

Thanks again
no open netbean or eclispe and create a new project - in your main class past the example code from the site and run it.  

when you run it, a XML-RPC request is made which returns a XML reponse.  

you can download the JAR file from http://ws.apache.org/xmlrpc/ that handles all this for you.  

once you have it downloaded add the JAR to your classpath and you will see the response output if you do something like:

System.out.println(response.toString());

These XML files you have pasted above - where did you get these?
Avatar of iAwin

ASKER


Manager gave them to me, just gave me the above spec and wants it done by tomorrow.

Why you ask?
>>You will also need to provide a test class to build the request and send it
to the Handler. This should be in the format of a JUnit test.

This sound so much like homework or university work....

However you will need to send the request to the server.  My project send requests to my homepage:  http://xmlrpc.seandelaney.co.uk that mapped to a PHP funciton and returned information from a database back to my Java application.

if your application has to be all Java based - e.g your server then look at these examples of how to code a XMLRPC Server:

http://ws.apache.org/xmlrpc/server.html

>>Why you ask?

As you need more information to complete the task other than 2 example XML file outputs...
Avatar of iAwin

ASKER


>>This sound so much like homework or university work....

Really this is not homework, can't see how I can convince you from sitting at my pc !

What extra information do I need ?

Cheers
you need to find out about the XMLRPC Server - what type it is?

Are you expected to create a Java based server or can you use a web based server like my XML-RPC server or the http://test.xmlrpc.wordtracker.com/ example....

if it is java based then create your XML file like you have above and implement the XMLRCP Server examples above to execute it and return the response.  im not too sure how you can going to get your response back into XML again as the XMLRPCClient handles everything for you... also see: http://ws.apache.org/xmlrpc/apidocs/org/apache/xmlrpc/server/XmlRpcServer.html

if it is web-based then just use the exmaples in http://www.wordtracker.com/docs/api/ch03s02.html.  but you will need an webserver that supports XML-RPC requests.  the server-side scripting langauge can be whatever you like (ASP or PHP) that maps to a function "XMLPRCServer.RetrieveUserToken" and returns the username, password, token and password....
Avatar of iAwin

ASKER


I can use a web based server.

Do you how I can map a function to a tomcat web server?`
what language are you using PHP?  if so then yes i do
Avatar of iAwin

ASKER


It will be jsp ??
Avatar of iAwin

ASKER

I'll be using jsp
Ouch!  I cant propram in JSP, but this might help you:

http://javaemerald.com/JavaMaster/xmlrpc.html

actually you think that example provides you will everything you need by the looks of it...
sorry getting my words mixed up ....

"actually you think that example provides you will everything you need by the looks of it..."

should read

"actually i think that example provides you with everything you need by the looks of it..."
Avatar of iAwin

ASKER


Thats seems pretty decent alright, what changes to I need to make to pass in my own xml file?

Thanks again
Avatar of iAwin

ASKER


Thats seems pretty decent alright, what changes to I need to make to pass in my own xml file?

Thanks again
Avatar of iAwin

ASKER


I think I see whats going on, the xml I posted initially posted is handled under the coper by xml-rpc. I just need to pass in the parameters according to the xml file, does this seem logical ?
Avatar of iAwin

ASKER


>>I think I see whats going on, the xml I posted initially posted is handled under the coper by xml-rpc. I just need to pass in the parameters according to the xml file, does this seem logical ?


Should read:

I think I see whats going on. The xml I posted initially is handled under the cover by xml-rpc. I just need to pass in the parameters according to the xml file, does this seem logical ?
First of all you need to know how the XMLRCP API for Java works.  You dont pass an actual XML file to the server - you pass the function name "XMLPRCServer.RetrieveUserToken" you want to use.  the XMLRCP API sends the request as XML and receives the response in XML.

look at this article to get a better understanding:  http://www.ibm.com/developerworks/xml/library/j-xmlrpc.html

what you need to do is create yor XMLRPC client.  your request should be pointing to your Tomcat server.  your JSP page should then accept this request.  older versions of the XMLRPC API accepted GET requests, but now they only accept POST requests...  have you ever used wordpress or movtable type??  or even trackbacks?  these are usign the XMLRPC protocol with POST requests... anyways your JSP page should return you the username, password and IP etc values back to your Java app.  after that do what you want with them.... display them out to the screen...

you dont see any of the XML as the XMLRPC Client classes handles all this for you.  alot of people make this mistake and think they need to send a XML file...

i think your manager either doesnt fully understand how the API works themselfs or they are looking to see if you can figure it out on your own and they have just provided you with example request & response XML outputs...

hope this helps

ellandrd
Avatar of iAwin

ASKER


It really helps. Wont get a chance to start code till this evening, ill post tomorrow though on how I get on.

Hopefully I havent wrecked your head too much.

Thanks for this
>>I think I see whats going on,

yes now you getting the hang of it and starting to understand how it works...  to be honest it is really really simple after you play about with the examples in http://www.wordtracker.com/docs/api/ch03s02.html.

when i first learnt about the XMLRPC protocol i was totally lost.  then i stripped an example of the XMLRPC for PHP apart one day and went through it line by line.... then i attacked the java version which is easier to understand than ther PHP version...
Avatar of iAwin

ASKER


It works!


I can't believe how easy it really is, thing is though I dont really understand whats going on. I realise I can get my original question working fairly easily now by just changing a a few of the method names and return variables using :  http://javaemerald.com/JavaMaster/xmlrpc.html

Would you mind giving me a quick breakdown of what happens in the jsp and class file in
 :  http://javaemerald.com/JavaMaster/xmlrpc.html

Thanks a million
>>I can't believe how easy it really is,

i told you...

>>Would you mind giving me a quick breakdown of what happens in the jsp and class file in

JSP isnt a strong area for me, but if you read the comments for each function in the class file, it tells you what each function does.  for the JSP page, the same principle is applied:

the server is listening for requests from the client.  when it accepts a request, it validates it to make sure it is a POST request.  it then parses the XML data in the request using:

byte[] reqdata = readBytes(request.getInputStream());

but before that it creates a new XMLRPC object using "setXmlRpc()" which calls the MyRPCClass() class which displays the 2 numbers you passed to in your request using "showNums()" function.

it then executes the request using "xmlrpcServ.execute()".  notice the readBytes function is doing alot of parsing and processing of the XML data from the request.   basically the execute function triggers the showNums() function.

the server then sends back the response again in XML format to the client.

i dont know if i was clear enough for you to understand - i understand what im trying to say so i hope you do to??

im glad you got it working.  
ASKER CERTIFIED SOLUTION
Avatar of ellandrd
ellandrd
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Something that doesn't seem to be answered in this thread is, when writing the server side,  how to return a Java class as teh return type of the XMLRPC call. The Apache XMLRPC library does not support the return of a user defined data structure (or class) eg
public class member{
    public String Username;
    public String Password;
    public String DefaultMSISDN;
}
How is this supposed to be accomplished?
Avatar of iAwin

ASKER


Very sorry about delay, thanks again ;-)