requesthelper.java

来自「一个实用工具类」· Java 代码 · 共 149 行

JAVA
149
字号
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file.  */package org.butor.web.helper;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import org.butor.helper.URLEncoder;import org.butor.log.Log;/** * Various helper methods to use on an ButorServletRequest. *  * @author 	mateusfi */public abstract class RequestHelper {	/**	 * Gets a cookie by its name from the request.	 * 	 * @param request 	 * @param name Name of the cookie to get.	 * @return The found cookie or null if not found.	 */	public static Cookie getCookie(HttpServletRequest request, String name) {		if (request == null) {			throw new NullPointerException("request can't be null");		}		if (name == null) {			throw new NullPointerException("name can't be null");		}				Cookie[] cookies = request.getCookies();		if (cookies == null) {			return null;		}				for (int i = 0; i < cookies.length; i++) {			Cookie cookie = cookies[i];			if (cookie != null) {				if (cookie.getName().equals(name)) {					return cookie;				}			}		}				return null;	}	/**	 * Builds an absolute URL using the information of the request and	 * an context-relative path.	 * 	 * @param request 	 * @param path path to include in the URL, can be null	 * @param contextRelative if true, the URL will contain the current contextPath	 */	public static String buildUrl(HttpServletRequest request, String path, boolean contextRelative) {		return buildUrl(request, path, contextRelative, null);	}	/**	 * Builds an absolute URL using the information of the request and	 * an context-relative path.	 * 	 * @param request 	 * @param path path to include in the URL, can be null 	 * @param contextRelative if true, the URL will contain the current contextPath	 * @param params map of parameters to append to URL.  Map key is a String and map value could be either	 * 	a String of a String[].	 */		public static String buildUrl(HttpServletRequest request, String path, boolean contextRelative, Map params) {		if (request == null) {			Log.logStr(RequestHelper.class, Log.LOG_TYPE_WARN, "buildUrl", "request is null, can't build URL");			return null;		}		// If the server port correspond to the default according to scheme, do not 		// include it in the URL otherwise this causes some trouble for the login process.		String port = "";		if (("http".equals(request.getScheme()) && request.getServerPort() != 80) 			|| ("https".equals(request.getScheme()) && request.getServerPort() != 443)) {			port = ":" + request.getServerPort();		}				StringBuffer url = new StringBuffer();		url.append(request.getScheme());		url.append("://");		url.append(request.getServerName());		url.append(port);		if (contextRelative) {			url.append(request.getContextPath());		}				if (path != null) {			url.append(path);		}				if (params != null) {			if (params.size() > 0) {				url.append("?");								StringBuffer queryString = new StringBuffer();								Set keys = params.keySet();				for (Iterator it = keys.iterator(); it.hasNext(); ) {					String key = (String)it.next();					Object o = params.get(key);										String value = "";					// If map value is of type String[], we must iterate					// into it and create multiple keys having the same name.					if (o instanceof String[]) {						String[] valueArray = (String[])o;						for (int i = 0; i < valueArray.length; i++) {							appendParameter(queryString, key, valueArray[i]);						}					} else {						appendParameter(queryString, key, params.get(key).toString());					}				}								url.append(queryString.toString());			}		}				return url.toString();	}		private static void appendParameter(StringBuffer queryString, String name, String value) {		if (queryString.length() > 0) {			queryString.append("&");		}		queryString.append(name + "=" + URLEncoder.encode(value));	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?