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