📄 servlethelper.java
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.utils.www;import com.queplix.core.error.IncorrectParameterException;import com.queplix.core.modules.config.ejb.CaptionManagerLocal;import com.queplix.core.modules.config.ejb.CaptionManagerLocalHome;import com.queplix.core.modules.eql.CompoundKey;import com.queplix.core.utils.JNDINames;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.cache.CacheObjectManager;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import java.util.HashSet;import java.util.Properties;import java.util.Set;/** * Helper class for servlets. * * @author [DAG] Dmitry Gaponenko * @author [ALB] Baranov Andrey * @author [ONZ] Oleg N. Zhovtanyuk */public final class ServletHelper { // =============================================================== Constants /** Charset encoding. */ public static final String CHARSET_ENCODING = "UTF-8"; /** XML content-type. */ public static final String CONTENT_TYPE_XML = "text/xml; charset=UTF-8"; /** HTML content-type. */ public static final String CONTENT_TYPE_HTML = "text/html; charset=UTF-8"; /** Plaint text content-type. */ public static final String CONTENT_TYPE_TEXT = "text/plain; charset=UTF-8"; /** Javascript text content-type. */ public static final String CONTENT_TYPE_JAVASCRIPT = "text/javascript; charset=UTF-8"; /** Gzip content-type. */ public static final String CONTENT_TYPE_GZIP = "gzip"; /** Octet-stream (binary) content-type. */ public static final String CONTENT_TYPE_BINARY = "application/octet-stream"; /** "Cache-contol" http header. */ public static final String HEADER_CACHE_CONTROL = "Cache-Control"; /** "Pragma" http header. */ public static final String HEADER_PRAGMA = "Pragma"; /** "no-cache" http header value. */ public static final String HEADER_VALUE_NO_CACHE = "no-cache"; /** Cache object attribute in servlet context */ private static final String ATTR_CACHE_OBJECT = "_cache_object_"; private static Properties popupMenuCaptions = new Properties(); private static Properties serverMessages = new Properties(); private static Properties focusCaptions = new Properties(); // ================================================================= Methods /** * Retrives request URL from HTTP request. * * @param request HTTP request * @return request URL */ public static String getRequestURL( HttpServletRequest request ) { StringBuffer sb = request.getRequestURL(); String qs = request.getQueryString(); if( qs != null ) { sb.append( "?" ); sb.append( qs ); } return sb.toString(); } /** * Gets the parameter value from HTTP request as string. * Parameter is checked for empty value. * * @param request HTTP request * @param paramName parameter name * @return parameter value * @throws IncorrectParameterException */ public static String getParamAsString( HttpServletRequest request, String paramName ) throws IncorrectParameterException { return getParamAsString( request, paramName, true ); } /** * Gets the parameter value from HTTP request as string. * * @param request HTTP request * @param paramName parameter name * @param noEmpty whether to check for empty string * @return parameter value * @throws IncorrectParameterException */ public static String getParamAsString( HttpServletRequest request, String paramName, boolean noEmpty ) throws IncorrectParameterException { String value = request.getParameter( paramName ); if( noEmpty ) { if( StringHelper.isEmpty( value ) ) throw new IncorrectParameterException( paramName ); } return value; } /** * Gets the parameter value from HTTP request as string. * Parameter is checked for empty value. * * @param request HTTP request * @param paramName parameter name * @param defaultValue default value for parameter * @return parameter value * @throws IncorrectParameterException */ public static String getParamAsString( HttpServletRequest request, String paramName, String defaultValue ) throws IncorrectParameterException { String value = request.getParameter( paramName ); return StringHelper.isEmpty( value ) ? defaultValue : value; } /** * Gets all values of the given parameter from HTTP request as string array. * Defaults are: * <ul> * <li>to check values for emptiness;</li> * <li>to exclude equals values.</li> * </ul> * * @param request HTTP request * @param paramName parameter name * @return parameter values * @throws IncorrectParameterException */ public static String[] getAllParamValuesAsString( HttpServletRequest request, String paramName ) throws IncorrectParameterException { return getAllParamValuesAsString( request, paramName, true, true ); } /** * Gets all values of the given parameter from HTTP request as string array. * * @param request HTTP request * @param paramName parameter name * @param noEmpty whether to check for empty string * @param noEquals whether to exclude equal strings * * @return parameter values * @throws IncorrectParameterException */ public static String[] getAllParamValuesAsString( HttpServletRequest request, String paramName, boolean noEmpty, boolean noEquals ) throws IncorrectParameterException { String[] values = request.getParameterValues( paramName ); // Null (and maybe, emptiness) checks. if( values == null ) { throw new IncorrectParameterException( paramName ); } if( noEmpty ) { for( int i = 0; i < values.length; i++ ) { if( StringHelper.isEmpty( values[i] ) ) { throw new IncorrectParameterException( paramName ); } } } else { for( int i = 0; i < values.length; i++ ) { if( values[i] == null ) { throw new IncorrectParameterException( paramName ); } } } // Get values. if( noEquals ) { Set uniqueValues = new HashSet(); for( int i = 0; i < values.length; i++ ) { uniqueValues.add( values[i] ); } return( String[] ) uniqueValues.toArray( new String[0] ); } else { return values; } } /** * Gets the parameter value from HTTP request as long. * * @param request HTTP request * @param paramName parameter name * @return parameter value * @throws IncorrectParameterException */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -