/** Convert list of URL-form name/value pairs into a Map representation * TODO: backport vector values code * @param extraparams url parameters * @param target map * @return map */ public static Map paramsToMap(String extraparams, Map target) { if (Logger.log.isDebugEnabled()) { Logger.log .debug("Action link requires extra parameters from " + extraparams); } StringTokenizer st = new StringTokenizer(extraparams, "&"); while (st.hasMoreTokens()) { String token = st.nextToken(); int eqpos = token.indexOf("="); String key = decodeURL(token.substring(0, eqpos)); String value = decodeURL(token.substring(eqpos + 1)); target.put(key, value); } return target; }
/** * "Unpacks" the supplied command link "name" (as encoded using the * HTMLRenderSystem for submission controls) by treating it as a section of * URL attribute stanzas. The key/value pairs encoded in it will be added to * the supplied (modifiable) map. */ public static void unpackCommandLink(String longvalue, Map requestparams) { String[] split = longvalue.split("[&=]"); // start at 1 since string will begin with & if ((split.length % 2) == 0) { Logger.log .warn("Erroneous submission - odd number of parameters/values in " + longvalue); return; } for (int i = 1; i < split.length; i += 2) { String key = URLUtil.decodeURL(split[i]); String value = URLUtil.decodeURL(split[i + 1]); Logger.log.info("Unpacked command link key " + key + " value " + value); String[] existing = (String[]) requestparams.get(key); if (existing == null) { requestparams.put(key, new String[] { value }); } else { String[] fused = (String[]) ArrayUtil.append(existing, value); requestparams.put(key, fused); } } }
/** * Computes the "Base URL" of this servlet, defined as the complete request * path, with any trailing string agreeing with the "PathInfo" removed. With * the exception that the trailing slash IS reappended. */ public static String getBaseURL2(HttpServletRequest hsr) { String requestURL = hsr.getRequestURL().toString(); // In some totally unspecced way, javax implementation provides the URL // ENCODED, whereas the PathInfo UNENCODED. requestURL = URLUtil.decodeURL(requestURL); String extrapath = hsr.getPathInfo(); String togo; if (extrapath == null || extrapath.equals("") || extrapath.equals("/")) { togo = requestURL; } else { int embedpoint = requestURL.lastIndexOf(extrapath); if (embedpoint == -1) { throw new UniversalRuntimeException("Cannot locate path info of " + extrapath + " within request URL of " + requestURL); } togo = requestURL.substring(0, embedpoint); } // I don't trust the Servlet API further than I can throw it. if (togo.charAt(togo.length() - 1) != '/') { togo += '/'; } return togo; }