001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.util;
020
021import org.apache.log4j.Logger;
022
023/**
024 * Misc J2EE Compatibility Utility Functions
025 */
026public class UtilJ2eeCompat
027{
028    private static Logger log = Logger.getLogger( UtilJ2eeCompat.class.getName() );
029
030    public static final String TOMCAT = "apache tomcat";
031
032    public static final String ORION = "orion";
033
034    public static final String RESIN = "resin";
035
036    public static final String REX_IP = "tradecity";
037
038    public static final String OC4J = "oracle";
039
040    public static final String JRUN = "jrun";
041
042    public static final String JETTY = "jetty";
043
044    public static final String WEBSPHERE = "websphere";
045
046    public static final String WEBSPHERE_LIBERTY = "SMF WebContainer";
047
048    public static final String WEBLOGIC = "weblogic";
049
050    public static final String GLASSFISH_1 = "sun java system application server";
051
052    public static final String GLASSFISH_2 = "glassfish server";
053
054    public static final String JBOSS = "jboss";
055    /**
056     * 
057     */
058    protected static Boolean useOutputStreamValue = null;
059
060    private static String m_serverInfo;
061
062    /**
063     * Determines the response wrapper for the servlet filters
064     * 
065     * @param serverInfo The string returned from context.getServerInfo()
066     * @return <code>true</code> if standard response wrapper does not work
067     *         properly; <code>false</code> default, otherwise
068     */
069    public static boolean useOutputStream( String serverInfo )
070    {
071        if( useOutputStreamValue == null )
072        {
073            initCompatibilityOptions( serverInfo );
074        }
075        return useOutputStreamValue.booleanValue();
076    }
077
078    /**
079     * For testing only
080     * 
081     * @param serverInfo The string returned from context.getServerInfo()
082     * @param boolInitialize True, if you want to force initialization again
083     * @return <code>true</code> if standard response wrapper does not work
084     *         properly; <code>false</code> default, otherwise
085     */
086    public static boolean useOutputStream( String serverInfo, Boolean boolInitialize )
087    {
088        if( (useOutputStreamValue == null) | (boolInitialize) )
089        {
090            initCompatibilityOptions( serverInfo );
091        }
092        return useOutputStreamValue.booleanValue();
093    }
094
095    /**
096     * Simple check of the servlet container
097     * 
098     * @param serverInfo The string returned from context.getServerInfo()
099     */
100    protected static void initCompatibilityOptions( String serverInfo )
101    {
102        log.info( "serverInfo: " + serverInfo );
103        m_serverInfo = serverInfo;
104        // response.getWriter is the default
105        boolean useStream = false;
106        if( serverInfo.toLowerCase().indexOf( RESIN ) >= 0 )
107        {
108            log.info( RESIN + " detected" );
109            // use response.getOutputStream instead of response.getWriter
110            useStream = true;
111        }
112        else if( serverInfo.toLowerCase().indexOf( REX_IP ) >= 0 )
113        {
114            log.info( REX_IP + " detected" );
115        }
116        else if( serverInfo.toLowerCase().indexOf( TOMCAT ) >= 0 )
117        {
118            log.info( TOMCAT + " detected" );
119            // use response.getOutputStream instead of response.getWriter
120            useStream = true;
121        }
122        else if( serverInfo.toLowerCase().indexOf( JRUN ) >= 0 )
123        {
124            log.info( JRUN + " detected" );
125        }
126        else if( serverInfo.toLowerCase().indexOf( JETTY ) >= 0 )
127        {
128            log.info( JETTY + " detected" );
129        }
130        else if( serverInfo.toLowerCase().indexOf( ORION ) >= 0 )
131        {
132            log.info( ORION + " detected" );
133        }
134        else if( serverInfo.toLowerCase().indexOf( WEBSPHERE ) >= 0 )
135        {
136            log.info( WEBSPHERE + " detected" );
137        }
138        else if( serverInfo.toLowerCase().indexOf( WEBSPHERE_LIBERTY ) >= 0 )
139        {
140            log.info( WEBSPHERE_LIBERTY + " detected" );
141        }
142        else if( serverInfo.toLowerCase().indexOf( WEBLOGIC ) >= 0 )
143        {
144            log.info( WEBLOGIC + " detected" );
145            // use response.getOutputStream instead of response.getWriter
146            useStream = true;
147        }
148        else if( serverInfo.toLowerCase().indexOf( GLASSFISH_1 ) >= 0 )
149        {
150            log.info( GLASSFISH_1 + " detected" );
151        }
152        else if( serverInfo.toLowerCase().indexOf( GLASSFISH_2 ) >= 0 )
153        {
154            log.info( GLASSFISH_2 + " detected" );
155        }
156        else if( serverInfo.toLowerCase().indexOf( OC4J ) >= 0 )
157        {
158            log.info( "Oracle Container for JEE detected" );
159            // use response.getOutputStream instead of response.getWriter
160            useStream = true;
161        }
162        else if( serverInfo.toLowerCase().indexOf( JBOSS ) >= 0 )
163        {
164            log.info( JBOSS + " detected" );
165            // use response.getOutputStream instead of response.getWriter
166            useStream = true;
167        }
168        useOutputStreamValue = new Boolean( useStream );
169    }
170
171    /**
172     * @return the Container type that has been detected
173     */
174    public static String getServerInfo()
175    {
176        return m_serverInfo;
177    }
178
179}