001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * DirectJNgine is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
017     * 
018     * This software uses the ExtJs library (http://extjs.com), which is 
019     * distributed under the GPL v3 license (see http://extjs.com/license).
020     */
021    
022    package com.softwarementors.extjs.djn;
023    
024    import java.util.ArrayList;
025    import java.util.Enumeration;
026    import java.util.List;
027    
028    import javax.servlet.ServletConfig;
029    import javax.servlet.http.HttpServletRequest;
030    
031    import org.apache.log4j.Logger;
032    
033    import com.softwarementors.extjs.djn.servlet.ServletConfigurationException;
034    
035    public final class ServletUtils {
036      private static final Logger logger = Logger.getLogger( ServletUtils.class);
037      
038      public ServletUtils() {
039        // Avoid instantiation
040      }
041    
042      public static String getRequiredParameter( ServletConfig conf, String parameterName ) {
043        assert conf != null;
044        assert !StringUtils.isEmpty(parameterName);
045        
046        String result = conf.getInitParameter( parameterName );
047        assert !StringUtils.isEmpty(result);
048        return result;
049      }
050      
051      public static String getParameter( ServletConfig conf, String parameterName, String valueIfNotSpecified ) {
052        assert conf != null;
053        assert !StringUtils.isEmpty(parameterName);
054        
055        String result = conf.getInitParameter( parameterName );
056        if( result != null ) {
057          result = result.trim();
058        }
059        if( StringUtils.isEmpty(result) ) {
060          return valueIfNotSpecified;
061        }
062        return result;
063      }
064    
065      public static int getIntParameterGreaterOrEqualToValue( ServletConfig conf, String parameterName, int minValue, int valueIfNotSpecified ) {
066        assert conf != null;
067        assert !StringUtils.isEmpty(parameterName);
068        
069        assert valueIfNotSpecified >= minValue;
070        
071        String resultString = getParameter( conf, parameterName, Integer.toString(valueIfNotSpecified));
072        if( StringUtils.isEmpty(resultString) ) {
073          return valueIfNotSpecified;
074        }
075        
076        try {
077          int result = Integer.parseInt(resultString);
078          if( result < minValue ) {
079            ServletConfigurationException ex = ServletConfigurationException.forParameterMustBeAnIntegerGreaterOrEqualToValue( parameterName, result, minValue );
080            logger.fatal( ex.getMessage(), ex );
081            throw ex;
082          }
083          return result;
084        }
085        catch( NumberFormatException e) {
086          ServletConfigurationException ex = ServletConfigurationException.forParameterMustBeAValidInteger( parameterName, resultString );
087          logger.fatal( ex.getMessage(), ex );
088          throw ex;
089        }
090        
091      }
092      
093      public static void checkRequiredParameters( ServletConfig conf, String... parameterNames ) {
094        assert conf != null;
095        assert parameterNames != null;
096        assert parameterNames.length > 0;
097        
098        List<String> missingParameters = new ArrayList<String>();
099        for( String parameterName : parameterNames ) {
100          String result = conf.getInitParameter( parameterName );
101          if( StringUtils.isEmpty(result) ) {
102            missingParameters.add( "'" + parameterName + "'" );
103          }
104        }
105        if( !missingParameters.isEmpty() ) {
106          ServletConfigurationException ex = ServletConfigurationException.forMissingRequiredConfigurationParameter( missingParameters );
107          logger.fatal(ex);
108          throw ex;
109        }
110      }
111      
112      public static String getDetailedRequestInformation(HttpServletRequest request) {
113        assert request != null;
114        
115        String contentType = request.getContentType();
116        if( contentType == null ) {
117          contentType = "";
118        }
119        String method = request.getMethod();    
120        String result = "RequestType=" + contentType + ", Method=" + method +
121            ", ContextPath=" + request.getContextPath() +
122            ", ServletPath=" + request.getServletPath() + 
123            ", PathInfo=" + request.getPathInfo() +
124            ", QueryString=" + request.getQueryString(); 
125            
126        result += "Headers: "; 
127        Enumeration<?> headers = request.getHeaderNames();
128        while( headers.hasMoreElements() ) {
129          String headerName = (String)headers.nextElement();
130          result += "'" + headerName + "'=";
131          Enumeration<?> headerContent = request.getHeaders(headerName);
132          while( headerContent.hasMoreElements() ) {
133            String headerValue = (String)headerContent.nextElement();
134            result += headerValue + ", ";
135          }
136        }
137        return result;
138      }
139    }