You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Neo Anderson <ja...@yahoo.co.uk> on 2009/06/05 22:36:39 UTC

Obtain the data inside RequestGlobals question

I following the mailing list at 
http://markmail.org/message/t5sopnse2wgqms7u#query:accessing%20requestglobals%20inside%20dispatcher%20and%20other%20data%20support%20classes+page:1+mid:ks77ayww62mra7in+state:results, trying to obtain data from RequestGlobals. 

So in my e.g. Login.java contains

	Object onSubmitFromLogin(){
		...
		requestGlobals.getRequest().setAttribute(
			"user", 
			new User(getAccount(), "", getPassword())
		);
		...
	}

and in AccessController.java it has 

	public AccessController(ApplicationStateManager asm, RequestGlobals globals){
		this.asm = asm;
		this.globals = globals;
	}

	public boolean dispatch(Request request, Response response) throws IOException{
		if(asm.exists(UserPermissions.class)){
			UserPermissions permission = asm.get(UserPermissions.class);
			User user =  (User)requestGlobals.getRequest().getAttribute(USER);
		...
		}
	}

However, it seems like the RequestGlobals inside Login.java and AccessController.java are two different instances because in Login.java I can see the User object after using getAttribute(). In contrast, I can obtain User object inside dispatch() function in AccessController.java. 

Where did I do it wrong? Or what is the right way to obtain data from Request or RequestGlobals?

Thank you very much.


      

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Mon, 08 Jun 2009 18:40:42 -0300, Neo Anderson  
<ja...@yahoo.co.uk> escreveu:

> What way is the correct method to set data in the Request object in Page  
> and retrieve it back from dispatch function?

Have you really tested the result of that code instead of just comparing  
the two Request instances?

By the way, unless your Tapestry code is inside an application that uses  
another framework (thus you need to read or write info in the session so  
the other framework car access it), use the ApplicationStateManager  
service instead.

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by Neo Anderson <ja...@yahoo.co.uk>.
I  try to use org.apache.tapestry5.services.Request.setAttribute() in
Login.java Page

    public class Login {
    	@Inject
	private Request request;
        ....
	Object onSubmitFromLogin(){
                ...
		request.setAttribute(
			org.jecommerce.services.AccessController.USER, 
			new User(...)
		);
                ...
        }
    }


 and retrieve it back in AccessController.java 

    public boolean dispatch(Request request, Response response) throws
IOException{
        ...
        User user =  (User)request.getAttribute(USER);
        ...
    }

However, it fails because it seems like the Request object in these two
classes are different. One is proxy, the other is the its impl. The output
shows

- Request in Login.java: <Proxy for
Request(org.apache.tapestry5.services.Request)>

- Request in AccessController.java:
org.apache.tapestry5.internal.services.RequestImpl@1c5cd7

What way is the correct method to set data in the Request object in Page and
retrieve it back from dispatch function? 

Thanks in advice,


Thiago H. de Paula Figueiredo wrote:
> 
> Em Fri, 05 Jun 2009 17:36:39 -0300, Neo Anderson  
> <ja...@yahoo.co.uk> escreveu:
> 
>> I following the mailing list at
>> http://markmail.org/message/t5sopnse2wgqms7u#query:accessing%20requestglobals%20inside%20dispatcher%20and%20other%20data%20support%20classes+page:1+mid:ks77ayww62mra7in+state:results,  
>> trying to obtain data from RequestGlobals.
> 
> That's an old thread . . .
> Try using just Request and its getAttribute() and setAttribute() methods,  
> not using RequestGlobals.
> 
> -- 
> Thiago H. de Paula Figueiredo
> Independent Java consultant, developer, and instructor
> http://www.arsmachina.com.br/thiago
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/Obtain-the-data-inside-RequestGlobals-question-tp3032702p3045819.html
Sent from the Tapestry Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Fri, 05 Jun 2009 17:36:39 -0300, Neo Anderson  
<ja...@yahoo.co.uk> escreveu:

> I following the mailing list at
> http://markmail.org/message/t5sopnse2wgqms7u#query:accessing%20requestglobals%20inside%20dispatcher%20and%20other%20data%20support%20classes+page:1+mid:ks77ayww62mra7in+state:results,  
> trying to obtain data from RequestGlobals.

That's an old thread . . .
Try using just Request and its getAttribute() and setAttribute() methods,  
not using RequestGlobals.

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by Otho <ta...@googlemail.com>.
I usually use Spring Security. No need to maintain a sessionstate object
myself then.:) But if Neo Anderson really insists of writing his own auth
system it could be handy indeed.

2009/6/9 Thiago H. de Paula Figueiredo <th...@gmail.com>

> On Tue, Jun 9, 2009 at 8:51 AM, Otho<ta...@googlemail.com> wrote:
> > Or did I get you totally wrong?
>
> You did totally right. Maybe you would also store user information in
> the session for later user. ;)
>
> --
> Thiago
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Obtain the data inside RequestGlobals question

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, Jun 9, 2009 at 8:51 AM, Otho<ta...@googlemail.com> wrote:
> Or did I get you totally wrong?

You did totally right. Maybe you would also store user information in
the session for later user. ;)

-- 
Thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by Otho <ta...@googlemail.com>.
That seems a very complicated way to do it.

public interface AuthService()
{
public boolean authenticate(User user);
}

publlic class AuthServiceImpl()
{
  public boolean authenticate (User user)
  {
     // authenticate and return true on success
  }
}

In your appmodule

binder.bind(AuthService.class, AuthServiceImpl.class); // at the appropriate
place


In your page

@Inject
private AuthService authService;

Object onSuccessFromLoginForm()
{
  User candidate = new User(..);
  if( ! authService .authenticate(candidate))
    return authenticationErrorPage;
  else
   return authenticationSuccessPage;
}

Or did I get you totally wrong?

2009/6/9 Neo Anderson <ja...@yahoo.co.uk>

>
> What I want to do is to perform authentication function; but would like
> auth
> logic to be separated from Page file. So the auth function can be reused
> next time if other front end client needs it.
>
> Then I searched on the tapestry website and found that seemingly it is
> achieved by using Dispatcher
> (http://wiki.apache.org/tapestry/Tapestry5HowToCreateADispatcher). That's
> why I use this mechanism trying to accomplish my task.
>
> What is the best practice if I want to achieve such goal?
>
> Or is there any resource/ guideline telling about this?
>
> Thank you very much.
>
>
> Thiago H. de Paula Figueiredo wrote:
> >
> > Em Mon, 08 Jun 2009 19:42:14 -0300, Neo Anderson
> > <ja...@yahoo.co.uk> escreveu:
> >
> >> I run the code and request.getAttribute(USER) returns null in dispatch()
> >> function. So I output the Request to see what happens; then notice that
> >> they (Request in Login.java Page and AccessController.java) are two
> >> difference objects.
> >
> > That's not what I've asked. Anyway, Tapestry uses proxies a lot, so it is
> > very likely that the two objects are functionally the same.
> >
> >> Now it looks like I can obtain the User data by marking the User object
> >> as SessionState in Login.java Page.
> >>
> >> public class Login{
> >> ...
> >>      @SessionState
> >>      private User user;
> >> ...
> >>      Object onSubmitFromLogin(){
> >>              user = new User(...);
> >>      }
> >> }
> >>
> >> Then retrieve User object from dispatch() in AccessController.java
> >>
> >>      public boolean dispatch(Request request, Response response) throws
> >> IOException{
> >>              ...
> >>              if(asm.exists(User.class)){
> >>                      User u = asm.get(User.class);
> >>                      ...
> >>              }
> >>      }
> >>
> >> However, this rise another question for me - is this a recommended way
> >> to obtain a temporary User object (because User object in Login Page is
> >> only constructed temporarily and passed to the backend side for
> >> verification)?
> >
> > If you'll need that object in a different request, yes.
> > Why are you using a dispatcher here? I can't see how a dispatcher would
> be
> > invoked in the same request after a page was rendered.
> >
> >> Also, how if I have other User object which holds different information
> >> (e.g. User object obtained from database), but needs to exist at the
> >> same time as that temporarily constructed User object? Will the later
> >> override the one that is constructed temporarily (I read Tapestry
> >> website saying if I have the same type marked with SessionState
> >> annotation, they will use the same reference regardless of their
> >> variable name)?
> >
> > Yes, it will override.
> >
> > I suggest you to tell use what you're trying to accomplish instead of
> > asking details of how to do it.
> >
> > --
> > Thiago H. de Paula Figueiredo
> > Independent Java consultant, developer, and instructor
> > http://www.arsmachina.com.br/thiago
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/Obtain-the-data-inside-RequestGlobals-question-tp3032702p3048582.html
> Sent from the Tapestry Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Obtain the data inside RequestGlobals question

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, Jun 9, 2009 at 7:11 AM, Neo
Anderson<ja...@yahoo.co.uk> wrote:

> What I want to do is to perform authentication function; but would like auth
> logic to be separated from Page file. So the auth function can be reused
> next time if other front end client needs it.
> Then I searched on the tapestry website and found that seemingly it is
> achieved by using Dispatcher
> (http://wiki.apache.org/tapestry/Tapestry5HowToCreateADispatcher). That's
> why I use this mechanism trying to accomplish my task.

Ok. You're in the right path here.

> What is the best practice if I want to achieve such goal?

You're trying to do the authentication (check the user/login pair) in
the dispatcher, but that's not the place to do that. What you should
do in the dispatcher is to check if the user is logged in and,
optionally, to check if he/she is allowed to view the requested page
(authorization). The authentication logic should be in a service and
it should be invoked in your login page, just like Otho suggested. By
the way, thanks Otho for helping. :)
>
> Or is there any resource/ guideline telling about this?
>
> Thank you very much.
>
>
> Thiago H. de Paula Figueiredo wrote:
>>
>> Em Mon, 08 Jun 2009 19:42:14 -0300, Neo Anderson
>> <ja...@yahoo.co.uk> escreveu:
>>
>>> I run the code and request.getAttribute(USER) returns null in dispatch()
>>> function. So I output the Request to see what happens; then notice that
>>> they (Request in Login.java Page and AccessController.java) are two
>>> difference objects.
>>
>> That's not what I've asked. Anyway, Tapestry uses proxies a lot, so it is
>> very likely that the two objects are functionally the same.
>>
>>> Now it looks like I can obtain the User data by marking the User object
>>> as SessionState in Login.java Page.
>>>
>>> public class Login{
>>> ...
>>>      @SessionState
>>>      private User user;
>>> ...
>>>      Object onSubmitFromLogin(){
>>>              user = new User(...);
>>>      }
>>> }
>>>
>>> Then retrieve User object from dispatch() in AccessController.java
>>>
>>>      public boolean dispatch(Request request, Response response) throws
>>> IOException{
>>>              ...
>>>              if(asm.exists(User.class)){
>>>                      User u = asm.get(User.class);
>>>                      ...
>>>              }
>>>      }
>>>
>>> However, this rise another question for me - is this a recommended way
>>> to obtain a temporary User object (because User object in Login Page is
>>> only constructed temporarily and passed to the backend side for
>>> verification)?
>>
>> If you'll need that object in a different request, yes.
>> Why are you using a dispatcher here? I can't see how a dispatcher would be
>> invoked in the same request after a page was rendered.
>>
>>> Also, how if I have other User object which holds different information
>>> (e.g. User object obtained from database), but needs to exist at the
>>> same time as that temporarily constructed User object? Will the later
>>> override the one that is constructed temporarily (I read Tapestry
>>> website saying if I have the same type marked with SessionState
>>> annotation, they will use the same reference regardless of their
>>> variable name)?
>>
>> Yes, it will override.
>>
>> I suggest you to tell use what you're trying to accomplish instead of
>> asking details of how to do it.
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java consultant, developer, and instructor
>> http://www.arsmachina.com.br/thiago
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Obtain-the-data-inside-RequestGlobals-question-tp3032702p3048582.html
> Sent from the Tapestry Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Obtain the data inside RequestGlobals question

Posted by Neo Anderson <ja...@yahoo.co.uk>.
What I want to do is to perform authentication function; but would like auth
logic to be separated from Page file. So the auth function can be reused
next time if other front end client needs it. 

Then I searched on the tapestry website and found that seemingly it is
achieved by using Dispatcher
(http://wiki.apache.org/tapestry/Tapestry5HowToCreateADispatcher). That's
why I use this mechanism trying to accomplish my task. 

What is the best practice if I want to achieve such goal?

Or is there any resource/ guideline telling about this? 

Thank you very much.


Thiago H. de Paula Figueiredo wrote:
> 
> Em Mon, 08 Jun 2009 19:42:14 -0300, Neo Anderson  
> <ja...@yahoo.co.uk> escreveu:
> 
>> I run the code and request.getAttribute(USER) returns null in dispatch()  
>> function. So I output the Request to see what happens; then notice that  
>> they (Request in Login.java Page and AccessController.java) are two  
>> difference objects.
> 
> That's not what I've asked. Anyway, Tapestry uses proxies a lot, so it is  
> very likely that the two objects are functionally the same.
> 
>> Now it looks like I can obtain the User data by marking the User object  
>> as SessionState in Login.java Page.
>>
>> public class Login{
>> ...
>> 	@SessionState
>> 	private User user;
>> ...
>> 	Object onSubmitFromLogin(){
>> 		user = new User(...);
>> 	}
>> }
>>
>> Then retrieve User object from dispatch() in AccessController.java
>>
>> 	public boolean dispatch(Request request, Response response) throws  
>> IOException{
>> 		...
>> 		if(asm.exists(User.class)){
>> 			User u = asm.get(User.class);
>> 			...
>> 		}
>> 	}
>>
>> However, this rise another question for me - is this a recommended way  
>> to obtain a temporary User object (because User object in Login Page is  
>> only constructed temporarily and passed to the backend side for  
>> verification)?
> 
> If you'll need that object in a different request, yes.
> Why are you using a dispatcher here? I can't see how a dispatcher would be  
> invoked in the same request after a page was rendered.
> 
>> Also, how if I have other User object which holds different information  
>> (e.g. User object obtained from database), but needs to exist at the  
>> same time as that temporarily constructed User object? Will the later  
>> override the one that is constructed temporarily (I read Tapestry  
>> website saying if I have the same type marked with SessionState  
>> annotation, they will use the same reference regardless of their  
>> variable name)?
> 
> Yes, it will override.
> 
> I suggest you to tell use what you're trying to accomplish instead of  
> asking details of how to do it.
> 
> -- 
> Thiago H. de Paula Figueiredo
> Independent Java consultant, developer, and instructor
> http://www.arsmachina.com.br/thiago
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/Obtain-the-data-inside-RequestGlobals-question-tp3032702p3048582.html
Sent from the Tapestry Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org