You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pablo Vázquez Blázquez <pv...@denodo.com> on 2007/11/28 18:24:35 UTC

Getting context properties

Hello,

How can I red my application name and the actions' extension?

For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do

I'd need a code to get "scheduler-admintool-1.0" and "do" from the context.

Thanks.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Gary Affonso <gl...@greywether.com>.
Hrmm.

I fear I have sent you on a wild goose chase.  After further 
exploration, I can't see how you can get core app-configuration 
properties through the RuntimeConfiguration.

It looks like there are really two sets of configuration data in S2, 
config data for executing the actions (actions, interceptors, results, 
etc.) and config data for internal Struts state (like the extension 
mapping suffix).

I can only hope the following serves as an acceptable apology for said 
goose chase...

What I didn't realize is that struts (xwork, actually) is using 
annotation-based IOC/DI.  As far as I can tell it has en embedded 
version of Guice.  And the IOC/DI container knows about the 
configuration data and can inject it into your objects.  I just tried 
this in an action and it worked great...

     List extensions = new ArrayList();
	
     @Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
     public void setExtensions(String extensions) {
         if (!"".equals(extensions)) {
             this.extensions = Arrays.asList(extensions.split(","));
         } else {
             this.extensions = null;
         }
     }

Presumably, you can use the @Inject annotation in other s2 object-types, 
not just actions (this should work in interceptors, custom results, etc.)

Note my previous warning, the "extensions" value can be a comma 
separated list, not just a single value, so handle that case.

Also note: I just verified that S2 does *NOT* slave or synchronize its 
action extension to the filter-mapping url defined in web.xml.

When you grab the "extensions" value using the above technique you're 
getting the value defined in struts' built-in "default.properties" or 
your own overrides in "struts.properties".  You are not getting the 
filter's url-mappings defined in web.xml.

For practical purposes it doesn't matter, it would be a configuration 
error (in most cases) if web.xml's filter mappings differed from Struts 
internal notion of the extension.  But just FYI on that.

Sorry for the goose chase.

- Gary

Gary Affonso wrote:
> I knew I had interacted with the runtime configuration before.  Just 
> found the code...
> 
>         RuntimeConfiguration runtimeConfiguration = 
> ConfigurationManager.getConfiguration().getRuntimeConfiguration();
> 
>         ActionConfig actionConfig = 
> runtimeConfiguration.getActionConfig("", 
> actionInvocation.getInvocationContext().getName());
> 
>         ResultConfig resultConfig = (ResultConfig) 
> actionConfig.getResults().get(result);
> 
> The purpose of the above code (which was run in an interceptor) was to 
> get the result configuration data for the currently executing action (in 
> our case, defined in "xwork.xml").
> 
> That's not applicable to what you want, but hopefully the above points 
> you in the right direction.
> 
> Oh, and this is code running under WebWork (2.x), might be a bit 
> different under S2.
> 
> Good Luck,
> 
> - Gary
> 
> Gary Affonso wrote:
>> Pablo Vázquez Blázquez wrote:
>>> Hello,
>>>
>>> How can I red my application name and the actions' extension?
>>>
>>> For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
>>>
>>> I'd need a code to get "scheduler-admintool-1.0" and "do" from the 
>>> context.
>>
>> The first part ("scheduler-admintool-1.0") should be your context 
>> root.  As Wes said, getContextPath() should give you that.  Or at 
>> least something you can parse for what you need.
>>
>> The second item ("do") is defined in web.xml when you specify the 
>> url-pattern of the filter mapping for the S2 filter.
>>
>> I don't know how to retrieve filter-mapping configuration data 
>> (defined in web.xml) from within a servlet (or an Action, Interceptor, 
>> etc).  As far as I know, it can't be done easily (without doing 
>> something like finding and parsing web.xml by hand).
>>
>> If you're willing to dig a bit, the S2 (and Xwork source code) imply 
>> that this information is also available in the S2 configuration data. 
>> Here's the configuration key that will lookup the "extensions" that 
>> Struts 2 knows about...
>>
>>   StrutsConstants.STRUTS_ACTION_EXTENSION
>>
>> Your next questions will be:
>>
>> "how do I get the central Struts 2 Configuration object?"
>> "how do I lookup a configuration value from this object using the key?"
>>
>> I don't have answers for those but maybe somebody else here can help 
>> with that.
>>
>> I vaguely recall that I've looked up config data on-the-fly (from 
>> within an Action) in WebWork at some distant point in the past.  I 
>> think it's possible just can't remember how it got done.
>>
>> - Gary
>>
>> P.S.  Two things to be careful of:
>>
>> 1) The "extensions" that get returned from the Configuration object 
>> are a comma separated list.  Often its just one extension but 
>> sometimes it's more than one.  So handle that case.
>>
>> 2) I have no clue how Struts 2 gather the "extensions" for its 
>> configuration.  I would think it would have to ensure they were the 
>> same as the url-pattern defined for the filter in web.xml.
>>
>> But there's some suspicious hardcoding of ".action" in the source code 
>> that makes me a little, well, suspicious.  So just be sure to 
>> double-check that the extension data you manage to get back from 
>> Configuration is properly slaved to the web.xml Struts filter's 
>> url-pattern.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Gary Affonso <gl...@greywether.com>.
I knew I had interacted with the runtime configuration before.  Just 
found the code...

         RuntimeConfiguration runtimeConfiguration = 
ConfigurationManager.getConfiguration().getRuntimeConfiguration();

         ActionConfig actionConfig = 
runtimeConfiguration.getActionConfig("", 
actionInvocation.getInvocationContext().getName());

         ResultConfig resultConfig = (ResultConfig) 
actionConfig.getResults().get(result);

The purpose of the above code (which was run in an interceptor) was to 
get the result configuration data for the currently executing action (in 
our case, defined in "xwork.xml").

That's not applicable to what you want, but hopefully the above points 
you in the right direction.

Oh, and this is code running under WebWork (2.x), might be a bit 
different under S2.

Good Luck,

- Gary

Gary Affonso wrote:
> Pablo Vázquez Blázquez wrote:
>> Hello,
>>
>> How can I red my application name and the actions' extension?
>>
>> For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
>>
>> I'd need a code to get "scheduler-admintool-1.0" and "do" from the 
>> context.
> 
> The first part ("scheduler-admintool-1.0") should be your context root. 
>  As Wes said, getContextPath() should give you that.  Or at least 
> something you can parse for what you need.
> 
> The second item ("do") is defined in web.xml when you specify the 
> url-pattern of the filter mapping for the S2 filter.
> 
> I don't know how to retrieve filter-mapping configuration data (defined 
> in web.xml) from within a servlet (or an Action, Interceptor, etc).  As 
> far as I know, it can't be done easily (without doing something like 
> finding and parsing web.xml by hand).
> 
> If you're willing to dig a bit, the S2 (and Xwork source code) imply 
> that this information is also available in the S2 configuration data. 
> Here's the configuration key that will lookup the "extensions" that 
> Struts 2 knows about...
> 
>   StrutsConstants.STRUTS_ACTION_EXTENSION
> 
> Your next questions will be:
> 
> "how do I get the central Struts 2 Configuration object?"
> "how do I lookup a configuration value from this object using the key?"
> 
> I don't have answers for those but maybe somebody else here can help 
> with that.
> 
> I vaguely recall that I've looked up config data on-the-fly (from within 
> an Action) in WebWork at some distant point in the past.  I think it's 
> possible just can't remember how it got done.
> 
> - Gary
> 
> P.S.  Two things to be careful of:
> 
> 1) The "extensions" that get returned from the Configuration object are 
> a comma separated list.  Often its just one extension but sometimes it's 
> more than one.  So handle that case.
> 
> 2) I have no clue how Struts 2 gather the "extensions" for its 
> configuration.  I would think it would have to ensure they were the same 
> as the url-pattern defined for the filter in web.xml.
> 
> But there's some suspicious hardcoding of ".action" in the source code 
> that makes me a little, well, suspicious.  So just be sure to 
> double-check that the extension data you manage to get back from 
> Configuration is properly slaved to the web.xml Struts filter's 
> url-pattern.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Gary Affonso <gl...@greywether.com>.
Pablo Vázquez Blázquez wrote:
> Hello,
> 
> How can I red my application name and the actions' extension?
> 
> For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
> 
> I'd need a code to get "scheduler-admintool-1.0" and "do" from the context.

The first part ("scheduler-admintool-1.0") should be your context root. 
  As Wes said, getContextPath() should give you that.  Or at least 
something you can parse for what you need.

The second item ("do") is defined in web.xml when you specify the 
url-pattern of the filter mapping for the S2 filter.

I don't know how to retrieve filter-mapping configuration data (defined 
in web.xml) from within a servlet (or an Action, Interceptor, etc).  As 
far as I know, it can't be done easily (without doing something like 
finding and parsing web.xml by hand).

If you're willing to dig a bit, the S2 (and Xwork source code) imply 
that this information is also available in the S2 configuration data. 
Here's the configuration key that will lookup the "extensions" that 
Struts 2 knows about...

   StrutsConstants.STRUTS_ACTION_EXTENSION

Your next questions will be:

"how do I get the central Struts 2 Configuration object?"
"how do I lookup a configuration value from this object using the key?"

I don't have answers for those but maybe somebody else here can help 
with that.

I vaguely recall that I've looked up config data on-the-fly (from within 
an Action) in WebWork at some distant point in the past.  I think it's 
possible just can't remember how it got done.

- Gary

P.S.  Two things to be careful of:

1) The "extensions" that get returned from the Configuration object are 
a comma separated list.  Often its just one extension but sometimes it's 
more than one.  So handle that case.

2) I have no clue how Struts 2 gather the "extensions" for its 
configuration.  I would think it would have to ensure they were the same 
as the url-pattern defined for the filter in web.xml.

But there's some suspicious hardcoding of ".action" in the source code 
that makes me a little, well, suspicious.  So just be sure to 
double-check that the extension data you manage to get back from 
Configuration is properly slaved to the web.xml Struts filter's url-pattern.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Martin Gainty <mg...@hotmail.com>.
Difficult to determine without looking at the code, web.xml, struts.xml and
properties

M--
----- Original Message -----
From: "Pablo Vázquez Blázquez" <pv...@denodo.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, November 29, 2007 6:05 AM
Subject: Re: Getting context properties


> Please, it must be very simple but I´m not able to find the suitable
> classes which should contain that information.
>
> Regards.
>
>
> Pablo Vázquez Blázquez escribió:
> > Hello,
> >
> > How can I red my application name and the actions' extension?
> >
> > For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
> >
> > I'd need a code to get "scheduler-admintool-1.0" and "do" from the
> > context.
> >
> > Thanks.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Wes Wannemacher <we...@wantii.com>.
I use 'request.getContextPath()' to get the app's name/path when I
need it. I've never needed the extension though, so I can't help you
there.

-W

On 11/29/07, Pablo Vázquez Blázquez <pv...@denodo.com> wrote:
> Please, it must be very simple but I´m not able to find the suitable
> classes which should contain that information.
>
> Regards.
>
>
> Pablo Vázquez Blázquez escribió:
> > Hello,
> >
> > How can I red my application name and the actions' extension?
> >
> > For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
> >
> > I'd need a code to get "scheduler-admintool-1.0" and "do" from the
> > context.
> >
> > Thanks.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Wesley Wannemacher
President, Head Engineer/Consultant
WanTii, Inc.
http://www.wantii.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Pablo Vázquez Blázquez <pv...@denodo.com>.
Thank you very much for your help!

I want to know it as the following:

If the user types in the url box: "http://localhost:8080/app/action.do" 
and is not logged-in and "action.do" requires authentication, then I 
redirect him/her to an authentication form. Well, I do this with an 
interceptor, in which I capture the URL he/she has typed. So, when 
he/she passes the authentication fase, I redirect him/her to the desired 
url (in this case, "action.do"). Maybe I don´t need to know my 
extension, but I have thought it in that way.

Regards.


Gary Affonso escribió:
> Dave Newton wrote:
>> Why do you want it?
>
> That's a good question for Dave to be asking.
>
> Even though you now know how to get it, are you really sure you want 
> it?  The framework shields you from this data for a reason. :-)
>
> - Gary
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Gary Affonso <gl...@greywether.com>.
Dave Newton wrote:
> Why do you want it?

That's a good question for Dave to be asking.

Even though you now know how to get it, are you really sure you want it? 
  The framework shields you from this data for a reason. :-)

- Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Dave Newton <ne...@yahoo.com>.
AFAIK this is not trivially available, although I
haven't dug too deeply.

Why do you want it?

d.

--- Pablo Vázquez Blázquez <pv...@denodo.com>
wrote:

> Thanks to all,
> 
> what I want to read is
> "StrutsConstants.STRUTS_ACTION_EXTENSION" property.
> 
> 
> Pablo Vázquez Blázquez escribió:
> > Please, it must be very simple but I´m not able to
> find the suitable 
> > classes which should contain that information.
> >
> > Regards.
> >
> >
> > Pablo Vázquez Blázquez escribió:
> >> Hello,
> >>
> >> How can I red my application name and the
> actions' extension?
> >>
> >> For example:
>
http://localhost:8080/scheduler-admintool-1.0/Workspace.do
> >>
> >> I'd need a code to get "scheduler-admintool-1.0"
> and "do" from the 
> >> context.
> >>
> >> Thanks.
> >>
> >>
> >>
>
---------------------------------------------------------------------
> >> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail:
> user-help@struts.apache.org
> >>
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> > For additional commands, e-mail:
> user-help@struts.apache.org
> >
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Pablo Vázquez Blázquez <pv...@denodo.com>.
Thanks to all,

what I want to read is "StrutsConstants.STRUTS_ACTION_EXTENSION" property.


Pablo Vázquez Blázquez escribió:
> Please, it must be very simple but I´m not able to find the suitable 
> classes which should contain that information.
>
> Regards.
>
>
> Pablo Vázquez Blázquez escribió:
>> Hello,
>>
>> How can I red my application name and the actions' extension?
>>
>> For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
>>
>> I'd need a code to get "scheduler-admintool-1.0" and "do" from the 
>> context.
>>
>> Thanks.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting context properties

Posted by Pablo Vázquez Blázquez <pv...@denodo.com>.
Please, it must be very simple but I´m not able to find the suitable 
classes which should contain that information.

Regards.


Pablo Vázquez Blázquez escribió:
> Hello,
>
> How can I red my application name and the actions' extension?
>
> For example: http://localhost:8080/scheduler-admintool-1.0/Workspace.do
>
> I'd need a code to get "scheduler-admintool-1.0" and "do" from the 
> context.
>
> Thanks.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org