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        assert !StringUtils.isEmpty(valueIfNotSpecified);
055        
056        String result = conf.getInitParameter( parameterName );
057        if( result == null ) {
058          return valueIfNotSpecified;
059        }
060        return result;
061      }
062    
063      public static void checkRequiredParameters( ServletConfig conf, String... parameterNames ) {
064        assert conf != null;
065        assert parameterNames != null;
066        assert parameterNames.length > 0;
067        
068        List<String> missingParameters = new ArrayList<String>();
069        for( String parameterName : parameterNames ) {
070          String result = conf.getInitParameter( parameterName );
071          if( StringUtils.isEmpty(result) ) {
072            missingParameters.add( "'" + parameterName + "'" );
073          }
074        }
075        if( !missingParameters.isEmpty() ) {
076          ServletConfigurationException ex = ServletConfigurationException.forMissingRequiredConfigurationParameter( missingParameters );
077          logger.fatal(ex);
078          throw ex;
079        }
080      }
081      
082      public static void logTraceDetailedRequestInformation(HttpServletRequest request) {
083        assert request != null;
084        
085        if( !logger.isTraceEnabled() )
086          return;
087        
088        String contentType = request.getContentType();
089        if( contentType == null ) {
090          contentType = "";
091        }
092        String method = request.getMethod();    
093        String result = "RequestType=" + contentType + ", Method=" + method +
094            ", ContextPath=" + request.getContextPath() +
095            ", ServletPath=" + request.getServletPath() + 
096            ", PathInfo=" + request.getPathInfo() +
097            ", QueryString=" + request.getQueryString(); 
098            
099        result += "Headers: "; 
100        Enumeration<?> headers = request.getHeaderNames();
101        while( headers.hasMoreElements() ) {
102          String headerName = (String)headers.nextElement();
103          result += "'" + headerName + "'=";
104          Enumeration<?> headerContent = request.getHeaders(headerName);
105          while( headerContent.hasMoreElements() ) {
106            String headerValue = (String)headerContent.nextElement();
107            result += headerValue + ", ";
108          }
109        }
110        logger.trace( result );
111      }
112    }