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 * Commercial use is permitted to the extent that the code/component(s) 011 * do NOT become part of another Open Source or Commercially developed 012 * licensed development library or toolkit without explicit permission. 013 * 014 * DirectJNgine is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with DirectJNgine. If not, see <http://www.gnu.org/licenses/>. 021 * 022 * This software uses the ExtJs library (http://extjs.com), which is 023 * distributed under the GPL v3 license (see http://extjs.com/license). 024 */ 025 026 package com.softwarementors.extjs.djn; 027 028 import java.util.ArrayList; 029 import java.util.Enumeration; 030 import java.util.List; 031 032 import javax.servlet.ServletConfig; 033 import javax.servlet.http.HttpServletRequest; 034 035 import org.apache.log4j.Logger; 036 037 import com.softwarementors.extjs.djn.servlet.ServletConfigurationException; 038 039 public final class ServletUtils { 040 private static final Logger logger = Logger.getLogger( ServletUtils.class); 041 042 public ServletUtils() { 043 // Avoid instantiation 044 } 045 046 public static String getRequiredParameter( ServletConfig conf, String parameterName ) { 047 assert conf != null; 048 assert !StringUtils.isEmpty(parameterName); 049 050 String result = conf.getInitParameter( parameterName ); 051 assert !StringUtils.isEmpty(result); 052 return result; 053 } 054 055 public static String getParameter( ServletConfig conf, String parameterName, String valueIfNotSpecified ) { 056 assert conf != null; 057 assert !StringUtils.isEmpty(parameterName); 058 059 String result = conf.getInitParameter( parameterName ); 060 if( result != null ) { 061 result = result.trim(); 062 } 063 if( StringUtils.isEmpty(result) ) { 064 return valueIfNotSpecified; 065 } 066 return result; 067 } 068 069 public static boolean getBooleanParameter( ServletConfig conf, String parameterName, boolean valueIfNotSpecified ) { 070 assert conf != null; 071 assert !StringUtils.isEmpty(parameterName); 072 073 String valueIfNotSpecifiedString = "false"; 074 if( valueIfNotSpecified) { 075 valueIfNotSpecifiedString = "true"; 076 } 077 String valueString = getParameter( conf, parameterName, valueIfNotSpecifiedString); 078 boolean result = valueString.equalsIgnoreCase("true") || valueString.equals("1"); 079 return result; 080 } 081 082 public static int getIntParameterGreaterOrEqualToValue( ServletConfig conf, String parameterName, int minValue, int valueIfNotSpecified ) { 083 assert conf != null; 084 assert !StringUtils.isEmpty(parameterName); 085 086 assert valueIfNotSpecified >= minValue; 087 088 String resultString = getParameter( conf, parameterName, Integer.toString(valueIfNotSpecified)); 089 if( StringUtils.isEmpty(resultString) ) { 090 return valueIfNotSpecified; 091 } 092 093 try { 094 int result = Integer.parseInt(resultString); 095 if( result < minValue ) { 096 ServletConfigurationException ex = ServletConfigurationException.forParameterMustBeAnIntegerGreaterOrEqualToValue( parameterName, result, minValue ); 097 logger.fatal( ex.getMessage(), ex ); 098 throw ex; 099 } 100 return result; 101 } 102 catch( NumberFormatException e) { 103 ServletConfigurationException ex = ServletConfigurationException.forParameterMustBeAValidInteger( parameterName, resultString ); 104 logger.fatal( ex.getMessage(), ex ); 105 throw ex; 106 } 107 108 } 109 110 public static void checkRequiredParameters( ServletConfig conf, String... parameterNames ) { 111 assert conf != null; 112 assert parameterNames != null; 113 assert parameterNames.length > 0; 114 115 List<String> missingParameters = new ArrayList<String>(); 116 for( String parameterName : parameterNames ) { 117 String result = conf.getInitParameter( parameterName ); 118 if( StringUtils.isEmpty(result) ) { 119 missingParameters.add( "'" + parameterName + "'" ); 120 } 121 } 122 if( !missingParameters.isEmpty() ) { 123 ServletConfigurationException ex = ServletConfigurationException.forMissingRequiredConfigurationParameter( missingParameters ); 124 logger.fatal(ex); 125 throw ex; 126 } 127 } 128 129 public static String getDetailedRequestInformation(HttpServletRequest request) { 130 assert request != null; 131 132 String contentType = request.getContentType(); 133 if( contentType == null ) { 134 contentType = ""; 135 } 136 String method = request.getMethod(); 137 String result = "RequestType=" + contentType + ", Method=" + method + 138 ", ContextPath=" + request.getContextPath() + 139 ", ServletPath=" + request.getServletPath() + 140 ", PathInfo=" + request.getPathInfo() + 141 ", QueryString=" + request.getQueryString() + 142 ", CharacterEncoding=" + request.getCharacterEncoding() + 143 ", AuthType=" + request.getAuthType() + 144 ", ContentType=" + request.getContentType() + 145 ", Scheme=" + request.getScheme() + 146 ", Locale=" + request.getLocale() + 147 ". "; 148 149 150 result += "Headers: "; 151 Enumeration<?> headers = request.getHeaderNames(); 152 while( headers.hasMoreElements() ) { 153 String headerName = (String)headers.nextElement(); 154 result += "'" + headerName + "'="; 155 Enumeration<?> headerContent = request.getHeaders(headerName); 156 while( headerContent.hasMoreElements() ) { 157 String headerValue = (String)headerContent.nextElement(); 158 result += headerValue + ", "; 159 } 160 } 161 return result; 162 } 163 }