You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@ws.apache.org by Twan Kogels <tw...@twansoft.com> on 2002/11/20 17:14:39 UTC

Access Response object in Handler?

Hello,

I've got a problem and can't seem to find the answer. I've made a 
midp(j2me) client which communicates with a xml-rpc server (in java, as 
servlet). No problem here.

A clients wants to login, easy peasy, check the username en password in a 
methode "doLogin" in class "Login". Add a handler and ready. Works perfect, 
doLogin() gets executed when a xml-rpc message comes in.

But now i want to return the url of the servlet in the doLogin() methode. 
When i'm in the servlet doPost methode it's easy:
String sessionURL = response.encodeURL(request.getRequestURL().toString());
But now i'm in the handler in the "doLogin()" method. I can't see how i can 
access the Response object.

I can't pass it in the constructor when i add the handlers, cause the 
Response object is not available in the init() methode of servlet. I also 
can't seem to pass a extra parameter to the execute() methode.

I'm quite stuck. Can anybody offer me a helping hand?

Best regards,
Twan Kogels


Re: Access Response object in Handler?

Posted by Scott Duclos <sd...@fulfill-net.com>.
Hi,

This is probably not the most elegant of solutions, but it works.
The way I accomplished this was by not registering the handler in
the init() method of the servlet, but instead, the doPost method.

In the doPost method, you first register the handler (passing the
response object to the constructor).  Next execute the xmlrpc method as
you normally would.  Third remove the handler.

xmlrpc.addHandler( "whatever", new ManipulateResponse( response ) );
byte[] result = xmlrpc.execute (request.getInputStream ());
xmlrpc.removeHandler( "whatever" );

This way you have a new handler for each new request with their
appropriate response object.

And obviously you will also need to synchronize this block so your threads
don't
overlap each other.

This method works pretty well for me and has not caused any issues,
but I would test thoroughly if going into a production environment.

Hope this helps you out.
Scott Duclos



----- Original Message -----
From: "Twan Kogels" <tw...@twansoft.com>
To: <rp...@xml.apache.org>
Sent: Wednesday, November 20, 2002 11:14 AM
Subject: Access Response object in Handler?


> Hello,
>
> I've got a problem and can't seem to find the answer. I've made a
> midp(j2me) client which communicates with a xml-rpc server (in java, as
> servlet). No problem here.
>
> A clients wants to login, easy peasy, check the username en password in a
> methode "doLogin" in class "Login". Add a handler and ready. Works
perfect,
> doLogin() gets executed when a xml-rpc message comes in.
>
> But now i want to return the url of the servlet in the doLogin() methode.
> When i'm in the servlet doPost methode it's easy:
> String sessionURL =
response.encodeURL(request.getRequestURL().toString());
> But now i'm in the handler in the "doLogin()" method. I can't see how i
can
> access the Response object.
>
> I can't pass it in the constructor when i add the handlers, cause the
> Response object is not available in the init() methode of servlet. I also
> can't seem to pass a extra parameter to the execute() methode.
>
> I'm quite stuck. Can anybody offer me a helping hand?
>
> Best regards,
> Twan Kogels
>
>


Re: Access Response object in Handler?

Posted by Scott Duclos <sd...@fulfill-net.com>.
Hi,

This is probably not the most elegant of solutions, but it works.
The way I accomplished this was by not registering the handler in
the init() method of the servlet, but instead, the doPost method.

In the doPost method, you first register the handler (passing the
response object to the constructor).  Next execute the xmlrpc method as
you normally would.  Third remove the handler.

xmlrpc.addHandler( "whatever", new ManipulateResponse( response ) );
byte[] result = xmlrpc.execute (request.getInputStream ());
xmlrpc.removeHandler( "whatever" );

This way you have a new handler for each new request with their
appropriate response object.

And obviously you will also need to synchronize this block so your threads
don't
overlap each other.

This method works pretty well for me and has not caused any issues,
but I would test thoroughly if going into a production environment.

Hope this helps you out.
Scott Duclos



----- Original Message -----
From: "Twan Kogels" <tw...@twansoft.com>
To: <rp...@xml.apache.org>
Sent: Wednesday, November 20, 2002 11:14 AM
Subject: Access Response object in Handler?


> Hello,
>
> I've got a problem and can't seem to find the answer. I've made a
> midp(j2me) client which communicates with a xml-rpc server (in java, as
> servlet). No problem here.
>
> A clients wants to login, easy peasy, check the username en password in a
> methode "doLogin" in class "Login". Add a handler and ready. Works
perfect,
> doLogin() gets executed when a xml-rpc message comes in.
>
> But now i want to return the url of the servlet in the doLogin() methode.
> When i'm in the servlet doPost methode it's easy:
> String sessionURL =
response.encodeURL(request.getRequestURL().toString());
> But now i'm in the handler in the "doLogin()" method. I can't see how i
can
> access the Response object.
>
> I can't pass it in the constructor when i add the handlers, cause the
> Response object is not available in the init() methode of servlet. I also
> can't seem to pass a extra parameter to the execute() methode.
>
> I'm quite stuck. Can anybody offer me a helping hand?
>
> Best regards,
> Twan Kogels
>
>


Re: Access Response object in Handler?

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Hi Danny.  Patches should be submitted to the <rpc-dev AT
xml.apache.org> list, with Subject line prefixed with [PATCH].  See
http://jakarta.apache.org/site/source.html for details on creating
patches in unidiff format.  Please make all patches against CVS HEAD.

Know that Andrew and Ryan may have already taken care of this use
case.

Danny Yoo <dy...@acoma.Stanford.EDU> writes:

> On Wed, 20 Nov 2002, Twan Kogels wrote:
> 
> > I've got a problem and can't seem to find the answer. I've made a
> > midp(j2me) client which communicates with a xml-rpc server (in java, as
> > servlet). No problem here.
> >
> > A clients wants to login, easy peasy, check the username en password in a
> > methode "doLogin" in class "Login". Add a handler and ready. Works perfect,
> > doLogin() gets executed when a xml-rpc message comes in.
> >
> > But now i want to return the url of the servlet in the doLogin() methode.
> > When i'm in the servlet doPost methode it's easy:
> > String sessionURL = response.encodeURL(request.getRequestURL().toString());
> > But now i'm in the handler in the "doLogin()" method. I can't see how i can
> > access the Response object.
> >
> > I can't pass it in the constructor when i add the handlers, cause the
> > Response object is not available in the init() methode of servlet. I also
> > can't seem to pass a extra parameter to the execute() methode.
> >
> > I'm quite stuck. Can anybody offer me a helping hand?
> 
> I'm run into a similar problem, where the AuthenticatedXmlRpcHandler
> interface just isn't rich enough; it only allows us to pass String names
> and passwords to our handlers.  I often want to pass in a session object
> so that I can take advantage of the session state mechanisms in my
> servlets.
> 
> 
> 
> The main problem is that the AuthenticatedXmlRpcHandler has too weak of an
> interface: it takes in a String for the username and a String for the
> password.  I've refactored AuthenticatedXmlRpcHandler, and created a
> parameter object called "AuthenticationObject" to bundle the 'user' and
> 'password' strings into a single object:
> 
> 
> //////
> // AuthenticationObject.java
> package org.apache.xmlrpc;
> 
> public interface AuthenticationObject {
>     String getUser();
>     String getPassword();
> }
> //////
> 
> 
> I then munged up org.apache.xmlrpc.XmlRpcServer so that it passes
> AuthenticationObject instances rather than those plain strings:
> 
> 
> 
> // Within XmlRpcServer
>     /**
>      * Parse the request and execute the handler method, if one is
>      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
>      * use the credentials to authenticate the user.
>      */
>     public byte[] execute(InputStream is,
> 			  final String user, final String password)
>     {
> 	return execute(is, new AuthenticationObject() {
> 		public String getUser() { return user; }
> 		public String getPassword() { return password; }
> 	    });
>     }
> 
> 
>     /**
>      * Parse the request and execute the handler method, if one is
>      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
>      * use the credentials to authenticate the user.
>      */
>     public byte[] execute(InputStream is, AuthenticationObject obj) {
>         Worker worker = getWorker();
>         byte[] retval = worker.execute(is, obj);
>         pool.push(worker);
>         return retval;
>     }
> 
> 
> 
> // ... later in the code...
> 
> 
> 		if (handler instanceof AuthenticationObjectXmlRpcHandler)
> {
>                     outParam =((AuthenticationObjectXmlRpcHandler)
> handler).
> 			execute(methodName, inParams, auth);
> 		}
> 		else if (handler instanceof AuthenticatedXmlRpcHandler)
>                 {
>                     outParam =((AuthenticatedXmlRpcHandler) handler).
>                             execute(methodName, inParams,
> 				    auth.getUser(), auth.getPassword());
>                 }
>                 else
>                 {
>                     outParam =((XmlRpcHandler) handler).execute(
>                             methodName, inParams);
>                 }
> 
> 
> 
> 
> 
> There are a few more source code changes I've had to make to maintain
> compatibility with the old interface.  I'd love to send this as a diff,
> but don't know who to submit my patches to.  Does anyone have suggestions?
> 
> 
> 
> Thanks again!
> 

-- 

Daniel Rall <dl...@finemaltcoding.com>

Re: Access Response object in Handler?

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Hi Danny.  Patches should be submitted to the <rpc-dev AT
xml.apache.org> list, with Subject line prefixed with [PATCH].  See
http://jakarta.apache.org/site/source.html for details on creating
patches in unidiff format.  Please make all patches against CVS HEAD.

Know that Andrew and Ryan may have already taken care of this use
case.

Danny Yoo <dy...@acoma.Stanford.EDU> writes:

> On Wed, 20 Nov 2002, Twan Kogels wrote:
> 
> > I've got a problem and can't seem to find the answer. I've made a
> > midp(j2me) client which communicates with a xml-rpc server (in java, as
> > servlet). No problem here.
> >
> > A clients wants to login, easy peasy, check the username en password in a
> > methode "doLogin" in class "Login". Add a handler and ready. Works perfect,
> > doLogin() gets executed when a xml-rpc message comes in.
> >
> > But now i want to return the url of the servlet in the doLogin() methode.
> > When i'm in the servlet doPost methode it's easy:
> > String sessionURL = response.encodeURL(request.getRequestURL().toString());
> > But now i'm in the handler in the "doLogin()" method. I can't see how i can
> > access the Response object.
> >
> > I can't pass it in the constructor when i add the handlers, cause the
> > Response object is not available in the init() methode of servlet. I also
> > can't seem to pass a extra parameter to the execute() methode.
> >
> > I'm quite stuck. Can anybody offer me a helping hand?
> 
> I'm run into a similar problem, where the AuthenticatedXmlRpcHandler
> interface just isn't rich enough; it only allows us to pass String names
> and passwords to our handlers.  I often want to pass in a session object
> so that I can take advantage of the session state mechanisms in my
> servlets.
> 
> 
> 
> The main problem is that the AuthenticatedXmlRpcHandler has too weak of an
> interface: it takes in a String for the username and a String for the
> password.  I've refactored AuthenticatedXmlRpcHandler, and created a
> parameter object called "AuthenticationObject" to bundle the 'user' and
> 'password' strings into a single object:
> 
> 
> //////
> // AuthenticationObject.java
> package org.apache.xmlrpc;
> 
> public interface AuthenticationObject {
>     String getUser();
>     String getPassword();
> }
> //////
> 
> 
> I then munged up org.apache.xmlrpc.XmlRpcServer so that it passes
> AuthenticationObject instances rather than those plain strings:
> 
> 
> 
> // Within XmlRpcServer
>     /**
>      * Parse the request and execute the handler method, if one is
>      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
>      * use the credentials to authenticate the user.
>      */
>     public byte[] execute(InputStream is,
> 			  final String user, final String password)
>     {
> 	return execute(is, new AuthenticationObject() {
> 		public String getUser() { return user; }
> 		public String getPassword() { return password; }
> 	    });
>     }
> 
> 
>     /**
>      * Parse the request and execute the handler method, if one is
>      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
>      * use the credentials to authenticate the user.
>      */
>     public byte[] execute(InputStream is, AuthenticationObject obj) {
>         Worker worker = getWorker();
>         byte[] retval = worker.execute(is, obj);
>         pool.push(worker);
>         return retval;
>     }
> 
> 
> 
> // ... later in the code...
> 
> 
> 		if (handler instanceof AuthenticationObjectXmlRpcHandler)
> {
>                     outParam =((AuthenticationObjectXmlRpcHandler)
> handler).
> 			execute(methodName, inParams, auth);
> 		}
> 		else if (handler instanceof AuthenticatedXmlRpcHandler)
>                 {
>                     outParam =((AuthenticatedXmlRpcHandler) handler).
>                             execute(methodName, inParams,
> 				    auth.getUser(), auth.getPassword());
>                 }
>                 else
>                 {
>                     outParam =((XmlRpcHandler) handler).execute(
>                             methodName, inParams);
>                 }
> 
> 
> 
> 
> 
> There are a few more source code changes I've had to make to maintain
> compatibility with the old interface.  I'd love to send this as a diff,
> but don't know who to submit my patches to.  Does anyone have suggestions?
> 
> 
> 
> Thanks again!
> 

-- 

Daniel Rall <dl...@finemaltcoding.com>

Re: Access Response object in Handler?

Posted by Danny Yoo <dy...@acoma.Stanford.EDU>.
On Wed, 20 Nov 2002, Twan Kogels wrote:

> I've got a problem and can't seem to find the answer. I've made a
> midp(j2me) client which communicates with a xml-rpc server (in java, as
> servlet). No problem here.
>
> A clients wants to login, easy peasy, check the username en password in a
> methode "doLogin" in class "Login". Add a handler and ready. Works perfect,
> doLogin() gets executed when a xml-rpc message comes in.
>
> But now i want to return the url of the servlet in the doLogin() methode.
> When i'm in the servlet doPost methode it's easy:
> String sessionURL = response.encodeURL(request.getRequestURL().toString());
> But now i'm in the handler in the "doLogin()" method. I can't see how i can
> access the Response object.
>
> I can't pass it in the constructor when i add the handlers, cause the
> Response object is not available in the init() methode of servlet. I also
> can't seem to pass a extra parameter to the execute() methode.
>
> I'm quite stuck. Can anybody offer me a helping hand?

I'm run into a similar problem, where the AuthenticatedXmlRpcHandler
interface just isn't rich enough; it only allows us to pass String names
and passwords to our handlers.  I often want to pass in a session object
so that I can take advantage of the session state mechanisms in my
servlets.



The main problem is that the AuthenticatedXmlRpcHandler has too weak of an
interface: it takes in a String for the username and a String for the
password.  I've refactored AuthenticatedXmlRpcHandler, and created a
parameter object called "AuthenticationObject" to bundle the 'user' and
'password' strings into a single object:


//////
// AuthenticationObject.java
package org.apache.xmlrpc;

public interface AuthenticationObject {
    String getUser();
    String getPassword();
}
//////


I then munged up org.apache.xmlrpc.XmlRpcServer so that it passes
AuthenticationObject instances rather than those plain strings:



// Within XmlRpcServer
    /**
     * Parse the request and execute the handler method, if one is
     * found. If the invoked handler is AuthenticatedXmlRpcHandler,
     * use the credentials to authenticate the user.
     */
    public byte[] execute(InputStream is,
			  final String user, final String password)
    {
	return execute(is, new AuthenticationObject() {
		public String getUser() { return user; }
		public String getPassword() { return password; }
	    });
    }


    /**
     * Parse the request and execute the handler method, if one is
     * found. If the invoked handler is AuthenticatedXmlRpcHandler,
     * use the credentials to authenticate the user.
     */
    public byte[] execute(InputStream is, AuthenticationObject obj) {
        Worker worker = getWorker();
        byte[] retval = worker.execute(is, obj);
        pool.push(worker);
        return retval;
    }



// ... later in the code...


		if (handler instanceof AuthenticationObjectXmlRpcHandler)
{
                    outParam =((AuthenticationObjectXmlRpcHandler)
handler).
			execute(methodName, inParams, auth);
		}
		else if (handler instanceof AuthenticatedXmlRpcHandler)
                {
                    outParam =((AuthenticatedXmlRpcHandler) handler).
                            execute(methodName, inParams,
				    auth.getUser(), auth.getPassword());
                }
                else
                {
                    outParam =((XmlRpcHandler) handler).execute(
                            methodName, inParams);
                }





There are a few more source code changes I've had to make to maintain
compatibility with the old interface.  I'd love to send this as a diff,
but don't know who to submit my patches to.  Does anyone have suggestions?



Thanks again!


Re: Access Response object in Handler?

Posted by Danny Yoo <dy...@acoma.Stanford.EDU>.
On Wed, 20 Nov 2002, Twan Kogels wrote:

> I've got a problem and can't seem to find the answer. I've made a
> midp(j2me) client which communicates with a xml-rpc server (in java, as
> servlet). No problem here.
>
> A clients wants to login, easy peasy, check the username en password in a
> methode "doLogin" in class "Login". Add a handler and ready. Works perfect,
> doLogin() gets executed when a xml-rpc message comes in.
>
> But now i want to return the url of the servlet in the doLogin() methode.
> When i'm in the servlet doPost methode it's easy:
> String sessionURL = response.encodeURL(request.getRequestURL().toString());
> But now i'm in the handler in the "doLogin()" method. I can't see how i can
> access the Response object.
>
> I can't pass it in the constructor when i add the handlers, cause the
> Response object is not available in the init() methode of servlet. I also
> can't seem to pass a extra parameter to the execute() methode.
>
> I'm quite stuck. Can anybody offer me a helping hand?

I'm run into a similar problem, where the AuthenticatedXmlRpcHandler
interface just isn't rich enough; it only allows us to pass String names
and passwords to our handlers.  I often want to pass in a session object
so that I can take advantage of the session state mechanisms in my
servlets.



The main problem is that the AuthenticatedXmlRpcHandler has too weak of an
interface: it takes in a String for the username and a String for the
password.  I've refactored AuthenticatedXmlRpcHandler, and created a
parameter object called "AuthenticationObject" to bundle the 'user' and
'password' strings into a single object:


//////
// AuthenticationObject.java
package org.apache.xmlrpc;

public interface AuthenticationObject {
    String getUser();
    String getPassword();
}
//////


I then munged up org.apache.xmlrpc.XmlRpcServer so that it passes
AuthenticationObject instances rather than those plain strings:



// Within XmlRpcServer
    /**
     * Parse the request and execute the handler method, if one is
     * found. If the invoked handler is AuthenticatedXmlRpcHandler,
     * use the credentials to authenticate the user.
     */
    public byte[] execute(InputStream is,
			  final String user, final String password)
    {
	return execute(is, new AuthenticationObject() {
		public String getUser() { return user; }
		public String getPassword() { return password; }
	    });
    }


    /**
     * Parse the request and execute the handler method, if one is
     * found. If the invoked handler is AuthenticatedXmlRpcHandler,
     * use the credentials to authenticate the user.
     */
    public byte[] execute(InputStream is, AuthenticationObject obj) {
        Worker worker = getWorker();
        byte[] retval = worker.execute(is, obj);
        pool.push(worker);
        return retval;
    }



// ... later in the code...


		if (handler instanceof AuthenticationObjectXmlRpcHandler)
{
                    outParam =((AuthenticationObjectXmlRpcHandler)
handler).
			execute(methodName, inParams, auth);
		}
		else if (handler instanceof AuthenticatedXmlRpcHandler)
                {
                    outParam =((AuthenticatedXmlRpcHandler) handler).
                            execute(methodName, inParams,
				    auth.getUser(), auth.getPassword());
                }
                else
                {
                    outParam =((XmlRpcHandler) handler).execute(
                            methodName, inParams);
                }





There are a few more source code changes I've had to make to maintain
compatibility with the old interface.  I'd love to send this as a diff,
but don't know who to submit my patches to.  Does anyone have suggestions?



Thanks again!