Java tutorial
/* * Copyright 2007-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 * * 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 net.ymate.framework.commons; import net.ymate.platform.core.lang.BlurObject; import net.ymate.platform.core.util.RuntimeUtils; import net.ymate.platform.core.util.UUIDUtils; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * @author (suninformation@163.com) on 15-1-12 ?4:50 * @version 1.0 */ public class ParamUtils { private static final Log _LOG = LogFactory.getLog(ParamUtils.class); /** * @param params ? * @param encode ??? * @param charset Encode?UTF-8 * @return ?ASCII??? */ public static String buildQueryParamStr(Map<String, ?> params, boolean encode, String charset) { String[] _keys = params.keySet().toArray(new String[params.size()]); Arrays.sort(_keys); StringBuilder _paramSB = new StringBuilder(); for (String _key : _keys) { Object _value = params.get(_key); if (_value != null) { if (_value.getClass().isArray()) { for (Object _v : (Object[]) _value) { __doAppendParamValue(_key, _v, _paramSB, encode, charset); } } else { __doAppendParamValue(_key, _value, _paramSB, encode, charset); } } } return _paramSB.toString(); } private static void __doAppendParamValue(String _key, Object _v, StringBuilder _paramSB, boolean encode, String charset) { if (_paramSB.length() > 0) { _paramSB.append("&"); } String _valueStr = _v.toString(); if (encode) { try { _paramSB.append(_key).append("=") .append(URLEncoder.encode(_valueStr, StringUtils.defaultIfBlank(charset, "UTF-8"))); } catch (UnsupportedEncodingException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } else { _paramSB.append(_key).append("=").append(_valueStr); } } public static String appendQueryParamValue(String url, Map<String, String> params, boolean encode, String charset) { if (params != null && !params.isEmpty()) { StringBuilder _paramSB = new StringBuilder(url); if (!url.contains("?")) { _paramSB.append("?"); } else { _paramSB.append("&"); } for (Map.Entry<String, String> _param : params.entrySet()) { if (encode) { try { _paramSB.append(_param.getKey()).append("=").append( URLEncoder.encode(_param.getValue(), StringUtils.defaultIfBlank(charset, "UTF-8"))) .append("&"); } catch (UnsupportedEncodingException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } else { _paramSB.append(_param.getKey()).append("=").append(_param.getValue()).append("&"); } } if (_paramSB.length() > 0 && _paramSB.charAt(_paramSB.length() - 1) == '&') { _paramSB.setLength(_paramSB.length() - 1); } return _paramSB.toString(); } return url; } public static Map<String, String> convertParamMap(Map<String, Object> sourceMap) { Map<String, String> _returnValue = new HashMap<String, String>(sourceMap.size()); for (Map.Entry<String, Object> _ent : sourceMap.entrySet()) { if (_ent.getValue() != null) { _returnValue.put(_ent.getKey(), BlurObject.bind(_ent.getValue()).toStringValue()); } } return _returnValue; } /** * ?????, ???Map * * @param paramStr ?? * @return ? */ public static Map<String, String> parseQueryParamStr(String paramStr) { return parseQueryParamStr(paramStr, false, null); } public static Map<String, String> parseQueryParamStr(String paramStr, boolean decode, String charset) { // &? String[] _paramArr = StringUtils.split(paramStr, '&'); // ?????? Map<String, String> _returnValue = new HashMap<String, String>(_paramArr.length); for (String _param : _paramArr) { //=? int nPos = _param.indexOf('='); // int nLen = _param.length(); //???? String strKey = _param.substring(0, nPos); // String strValue = _param.substring(nPos + 1, nLen); if (decode) { try { strValue = URLDecoder.decode(strValue, StringUtils.defaultIfBlank(charset, "UTF-8")); } catch (UnsupportedEncodingException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } //MAP _returnValue.put(strKey, strValue); } return _returnValue; } public static String buildActionForm(String actionUrl, boolean usePost, Map<String, String> params) { return buildActionForm(actionUrl, usePost, false, false, null, params); } public static String buildActionForm(String actionUrl, boolean usePost, boolean encode, boolean enctype, String charset, Map<String, String> params) { String _charset = StringUtils.defaultIfBlank(charset, "UTF-8"); StringBuilder _payHtml = new StringBuilder(); _payHtml.append("<form id=\"_payment_submit\" name=\"_payment_submit\" action=\"").append(actionUrl) .append("\" method=\"").append(usePost ? "POST" : "GET").append("\""); if (enctype) { _payHtml.append("\" enctype=\"application/x-www-form-urlencoded;charset=").append(_charset) .append("\""); } _payHtml.append(">"); // for (Map.Entry<String, String> _entry : params.entrySet()) { __doAppendHiddenElement(_payHtml, _entry.getKey(), _entry.getValue(), encode, _charset); } // submit???name _payHtml.append("<input type=\"submit\" value=\"doSubmit\" style=\"display:none;\"></form>"); _payHtml.append("<script>document.forms['_payment_submit'].submit();</script>"); return _payHtml.toString(); } private static void __doAppendHiddenElement(StringBuilder stringBuilder, String key, String value, boolean encode, String charset) { if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) { String _splitStr = StringUtils.contains(value, '\"') ? "\'" : "\""; // stringBuilder.append("<input type=").append(_splitStr).append("hidden").append(_splitStr) .append(" name=").append(_splitStr).append(key).append(_splitStr); // String _valueStr = value; if (encode) { try { _valueStr = URLEncoder.encode(_valueStr, charset); } catch (UnsupportedEncodingException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } stringBuilder.append(" value=").append(_splitStr).append(_valueStr).append(_splitStr).append(">"); } } /** * @return ?632?? */ public static String createNonceStr() { return UUIDUtils.randomStr(UUIDUtils.randomInt(6, 32), false).toLowerCase(); } /** * @param queryParamMap ??? * @param encode ?? * @param extraParams ? * @return ??? */ public static String createSignature(Map<String, ?> queryParamMap, boolean encode, String... extraParams) { return createSignature(queryParamMap, encode, true, extraParams); } public static String createSignature(Map<String, ?> queryParamMap, boolean encode, boolean upperCase, String... extraParams) { StringBuilder _queryParamStr = new StringBuilder(buildQueryParamStr(queryParamMap, encode, null)); if (extraParams != null && extraParams.length > 0) { for (String _extraParam : extraParams) { _queryParamStr.append("&").append(_extraParam); } } String _signStr = DigestUtils.md5Hex(_queryParamStr.toString()); if (upperCase) { _signStr = _signStr.toUpperCase(); } return _signStr; } }