📄 utilhttp.java
字号:
/* * $Id: UtilHttp.java 7099 2006-03-28 23:02:54Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.base.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.FileNameMap;import java.net.URLConnection;import java.net.URLEncoder;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Arrays;import java.util.Calendar;import java.util.Collection;import java.util.Currency;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javolution.util.FastList;import javolution.util.FastMap;/** * HttpUtil - Misc TTP Utility Functions * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 7099 $ * @since 2.1 */public class UtilHttp { public static final String module = UtilHttp.class.getName(); public static final String MULTI_ROW_DELIMITER = "_o_"; public static final String ROW_SUBMIT_PREFIX = "_rowSubmit_o_"; public static final String COMPOSITE_DELIMITER = "_c_"; public static final int MULTI_ROW_DELIMITER_LENGTH = MULTI_ROW_DELIMITER.length(); public static final int ROW_SUBMIT_PREFIX_LENGTH = ROW_SUBMIT_PREFIX.length(); public static final int COMPOSITE_DELIMITER_LENGTH = COMPOSITE_DELIMITER.length(); /** * Create a map from an HttpServletRequest object * @return The resulting Map */ public static Map getParameterMap(HttpServletRequest request) { Map paramMap = FastMap.newInstance(); // add all the actual HTTP request parameters Enumeration e = request.getParameterNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); Object value = null; String[] paramArr = request.getParameterValues(name); if (paramArr != null) { if (paramArr.length > 1) { value = Arrays.asList(paramArr); } else { value = paramArr[0]; // does the same thing basically, nothing better about it as far as I can see: value = request.getParameter(name); } } paramMap.put(name, value); } // now add in all path info parameters /~name1=value1/~name2=value2/ // note that if a parameter with a given name already exists it will be put into a list with all values String pathInfoStr = request.getPathInfo(); if (pathInfoStr != null && pathInfoStr.length() > 0) { // make sure string ends with a trailing '/' so we get all values if (!pathInfoStr.endsWith("/")) pathInfoStr += "/"; int current = pathInfoStr.indexOf('/'); int last = current; while ((current = pathInfoStr.indexOf('/', last + 1)) != -1) { String element = pathInfoStr.substring(last + 1, current); last = current; if (element.charAt(0) == '~' && element.indexOf('=') > -1) { String name = element.substring(1, element.indexOf('=')); String value = element.substring(element.indexOf('=') + 1); Object curValue = paramMap.get(name); if (curValue != null) { List paramList = null; if (curValue instanceof List) { paramList = (List) curValue; paramList.add(value); } else { String paramString = (String) curValue; paramList = FastList.newInstance(); paramList.add(paramString); paramList.add(value); } paramMap.put(name, paramList); } else { paramMap.put(name, value); } } } } if (paramMap.size() == 0) { // nothing found in the parameters; maybe we read the stream instead Map multiPartMap = (Map) request.getAttribute("multiPartMap"); if (multiPartMap != null && multiPartMap.size() > 0) { paramMap.putAll(multiPartMap); } } //Debug.logInfo("Made parameterMap: \n" + UtilMisc.printMap(paramMap), module); if (Debug.verboseOn()) { Debug.logVerbose("Made Request Parameter Map with [" + paramMap.size() + "] Entries", module); Iterator entryIter = paramMap.entrySet().iterator(); while (entryIter.hasNext()) { Map.Entry entry = (Map.Entry) entryIter.next(); Debug.logVerbose("Request Parameter Map Entry: [" + entry.getKey() + "] --> " + entry.getValue(), module); } } return paramMap; } public static Map makeParamMapWithPrefix(HttpServletRequest request, String prefix, String suffix) { return makeParamMapWithPrefix(request, null, prefix, suffix); } public static Map makeParamMapWithPrefix(HttpServletRequest request, Map additionalFields, String prefix, String suffix) { Map paramMap = new HashMap(); Enumeration parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = (String) parameterNames.nextElement(); if (parameterName.startsWith(prefix)) { if (suffix != null && suffix.length() > 0) { if (parameterName.endsWith(suffix)) { String key = parameterName.substring(prefix.length(), parameterName.length() - (suffix.length())); String value = request.getParameter(parameterName); paramMap.put(key, value); } } else { String key = parameterName.substring(prefix.length()); String value = request.getParameter(parameterName); paramMap.put(key, value); } } } if (additionalFields != null) { Iterator i = additionalFields.keySet().iterator(); while (i.hasNext()) { String fieldName = (String) i.next(); if (fieldName.startsWith(prefix)) { if (suffix != null && suffix.length() > 0) { if (fieldName.endsWith(suffix)) { String key = fieldName.substring(prefix.length(), fieldName.length() - (suffix.length() - 1)); Object value = additionalFields.get(fieldName); paramMap.put(key, value); // check for image upload data if (!(value instanceof String)) { String nameKey = "_" + key + "_fileName"; Object nameVal = additionalFields.get("_" + fieldName + "_fileName"); if (nameVal != null) { paramMap.put(nameKey, nameVal); } String typeKey = "_" + key + "_contentType"; Object typeVal = additionalFields.get("_" + fieldName + "_contentType"); if (typeVal != null) { paramMap.put(typeKey, typeVal); } String sizeKey = "_" + key + "_size"; Object sizeVal = additionalFields.get("_" + fieldName + "_size"); if (sizeVal != null) { paramMap.put(sizeKey, sizeVal); } } } } else { String key = fieldName.substring(prefix.length()); Object value = additionalFields.get(fieldName); paramMap.put(key, value); // check for image upload data if (!(value instanceof String)) { String nameKey = "_" + key + "_fileName"; Object nameVal = additionalFields.get("_" + fieldName + "_fileName"); if (nameVal != null) { paramMap.put(nameKey, nameVal); } String typeKey = "_" + key + "_contentType"; Object typeVal = additionalFields.get("_" + fieldName + "_contentType"); if (typeVal != null) { paramMap.put(typeKey, typeVal); } String sizeKey = "_" + key + "_size"; Object sizeVal = additionalFields.get("_" + fieldName + "_size"); if (sizeVal != null) { paramMap.put(sizeKey, sizeVal); } } } } } } return paramMap; } public static List makeParamListWithSuffix(HttpServletRequest request, String suffix, String prefix) { return makeParamListWithSuffix(request, null, suffix, prefix); } public static List makeParamListWithSuffix(HttpServletRequest request, Map additionalFields, String suffix, String prefix) { List paramList = new ArrayList(); Enumeration parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = (String) parameterNames.nextElement(); if (parameterName.endsWith(suffix)) { if (prefix != null && prefix.length() > 0) { if (parameterName.startsWith(prefix)) { String value = request.getParameter(parameterName); paramList.add(value); } } else { String value = request.getParameter(parameterName); paramList.add(value); } } } if (additionalFields != null) { Iterator i = additionalFields.keySet().iterator(); while (i.hasNext()) { String fieldName = (String) i.next(); if (fieldName.endsWith(suffix)) { if (prefix != null && prefix.length() > 0) { if (fieldName.startsWith(prefix)) { Object value = additionalFields.get(fieldName); paramList.add(value); } } else { Object value = additionalFields.get(fieldName); paramList.add(value); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -