You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Miguel Alcalde <ma...@dome-consulting.com> on 2006/08/18 10:29:25 UTC

How to get tomcat configuration inside a servlet

Hi All.

My problem is that
I need to get "ssl port" inside a servlet in order to do a transparent
redirection to "secure zone".

In our architecture we're running multiple tomcat instances with
different configurations. Not in all cases ports are set  80/443.
We're thinking to get tomcat configuration for connectors and obtain
which port we should redirect.

How can I resolve it? How can I get information about the connector
inside my servlet?

Thank in advanced


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get tomcat configuration inside a servlet

Posted by Miguel Alcalde <ma...@dome-consulting.com>.
> I'd try to use MBeans and JMX infrastructure to find out tomcat 
> configuration. http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html
> The admin webapp depends on MBeans to 'talk' to tomcat. So try this way.
You're GREAT ;-).

Very thank you.

------------------------------------------------------------------------------------------------------------------------------------------
Added t our code
------------------------------------------------------------------------------------------------------------------------------------------

import javax.management.*;
import org.apache.commons.modeler.Registry;

private static void initialize() {
        try {
            MBeanServer mBeanServer = Registry.getRegistry(null, 
null).getMBeanServer();
            extractParametersTomcat(mBeanServer,"*:*");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement 
use File | Settings | File Templates.
        }
    }

    private static void extractParametersTomcat(MBeanServer mBeanServer, 
String qry )
    {

        Set names = null;
        try {
            names=mBeanServer.queryNames(new ObjectName(qry), null);
        } catch (Exception e) {
            return;
        }

        Iterator it=names.iterator();
        while( it.hasNext()) {
            ObjectName oname=(ObjectName)it.next();

            try {
                MBeanInfo minfo=mBeanServer.getMBeanInfo(oname);
               
                String code=minfo.getClassName();
                if 
("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    code=(String)mBeanServer.getAttribute(oname, 
"modelerType");
                }

               
                boolean protocolIsHTTP = false;
                boolean isSecure = false;
                boolean isSchemeHTTPS = false;

                String _protocol;
                Object _port;

                if( 
minfo.getClassName().equals("org.apache.catalina.mbeans.ConnectorMBean") 
) {//if#1
                   _protocol = (String)mBeanServer.getAttribute(oname, 
"protocol");
                   _port = mBeanServer.getAttribute(oname, "port");

                   if( _protocol.toLowerCase().startsWith("http") ) {
                      
                       isSecure = ( ( mBeanServer.getAttribute(oname, 
"secure") != null ) &&
                                    ( mBeanServer.getAttribute(oname, 
"secure").toString().equalsIgnoreCase("yes") ) );
                       isSchemeHTTPS = ( ( 
mBeanServer.getAttribute(oname, "scheme") != null ) &&
                                         ( 
mBeanServer.getAttribute(oname, 
"scheme").toString().equalsIgnoreCase("https") ));

                       if( isSecure || isSchemeHTTPS )
                           portSSL = _port.toString();
                       else
                           portNoSSL = _port.toString();
                   }
                } //final if#1
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get tomcat configuration inside a servlet

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
Mikolaj Rydzewski wrote:
>> But we still need to get tomcat ssl configuration inside a servlet.  
>> The reason: In our strange architecture (struts-flavour) a servlet is 
>> a wrapper for actions with states. Only some of these states have to 
>> be secured.  Have anybody an idea how to get that information?
> You can run an apache server in the front. And then use mod_rewrite 
> (for specific URLs) to redirect users to secure pages. The application 
> (and Tomcat) doesn't need to know anything about security in this 
> scenario.
Oh, I read your original posting again and it now the solution with 
apache in the front is not the best one.

I'd try to use MBeans and JMX infrastructure to find out tomcat 
configuration. http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html
The admin webapp depends on MBeans to 'talk' to tomcat. So try this way.

-- 
Mikolaj Rydzewski <mi...@ceti.pl>


Re: How to get tomcat configuration inside a servlet

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
Miguel Alcalde wrote:
> Mikolaj Rydzewski escribió:
>> You don't have to worry about it. Read about redirectPort attribute 
>> at http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
> Thanks, mate.
> It solves 90% of our problems.
>
> But we still need to get tomcat ssl configuration inside a servlet.  
> The reason: In our strange architecture (struts-flavour) a servlet is 
> a wrapper for actions with states. Only some of these states have to 
> be secured.  
> Have anybody an idea how to get that information?
You can run an apache server in the front. And then use mod_rewrite (for 
specific URLs) to redirect users to secure pages. The application (and 
Tomcat) doesn't need to know anything about security in this scenario.

-- 
Mikolaj Rydzewski <mi...@ceti.pl>


Re: How to get tomcat configuration inside a servlet

Posted by Miguel Alcalde <ma...@dome-consulting.com>.
Mikolaj Rydzewski escribió:
> You don't have to worry about it. Read about redirectPort attribute at 
> http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
Thanks, mate.
It solves 90% of our problems.

But we still need to get tomcat ssl configuration inside a servlet.  
 The reason: In our strange architecture (struts-flavour) a servlet is a 
wrapper for actions with states. Only some of these states have to be 
secured.   

Have anybody an idea how to get that information?
Thank in advanced


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to get tomcat configuration inside a servlet

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
Miguel Alcalde wrote:
> My problem is that
> I need to get "ssl port" inside a servlet in order to do a transparent
> redirection to "secure zone".
>
> In our architecture we're running multiple tomcat instances with
> different configurations. Not in all cases ports are set  80/443.
> We're thinking to get tomcat configuration for connectors and obtain
> which port we should redirect.
>
> How can I resolve it? How can I get information about the connector
> inside my servlet?
You don't have to worry about it. Read about redirectPort attribute at 
http://tomcat.apache.org/tomcat-5.5-doc/config/http.html


-- 
Mikolaj Rydzewski <mi...@ceti.pl>