⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilhttp.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }        return paramList;    }    /**     * Given a request, returns the application name or "root" if deployed on root     * @param request An HttpServletRequest to get the name info from     * @return String     */    public static String getApplicationName(HttpServletRequest request) {        String appName = "root";        if (request.getContextPath().length() > 1) {            appName = request.getContextPath().substring(1);        }        return appName;    }    /**     * Put request parameters in request object as attributes.     * @param request     */    public static void parametersToAttributes(HttpServletRequest request) {        java.util.Enumeration e = request.getParameterNames();        while (e.hasMoreElements()) {            String name = (String) e.nextElement();            request.setAttribute(name, request.getParameter(name));        }    }    public static StringBuffer getServerRootUrl(HttpServletRequest request) {        StringBuffer requestUrl = new StringBuffer();        requestUrl.append(request.getScheme());        requestUrl.append("://" + request.getServerName());        if (request.getServerPort() != 80 && request.getServerPort() != 443)            requestUrl.append(":" + request.getServerPort());        return requestUrl;    }    public static StringBuffer getFullRequestUrl(HttpServletRequest request) {        StringBuffer requestUrl = UtilHttp.getServerRootUrl(request);        requestUrl.append(request.getRequestURI());        if (request.getQueryString() != null) {            requestUrl.append("?" + request.getQueryString());        }        return requestUrl;    }    private static Locale getLocale(HttpServletRequest request, HttpSession session) {        // check session first, should override all if anything set there         Object localeObject = localeObject = session != null ? session.getAttribute("locale") : null;        // next see if the userLogin has a value        if (localeObject == null) {            Map userLogin = (Map) session.getAttribute("userLogin");            if (userLogin == null) {                userLogin = (Map) session.getAttribute("autoUserLogin");            }            if (userLogin != null) {                localeObject = userLogin.get("lastLocale");            }        }        // finally request (w/ a fall back to default)        if (localeObject == null) {            localeObject = request != null ? request.getLocale() : null;        }        return UtilMisc.ensureLocale(localeObject);    }    /**     * Get the Locale object from a session variable; if not found use the browser's default     * @param request HttpServletRequest object to use for lookup     * @return Locale The current Locale to use     */    public static Locale getLocale(HttpServletRequest request) {        if (request == null) return Locale.getDefault();        return UtilHttp.getLocale(request, request.getSession());    }    /**     * Get the Locale object from a session variable; if not found use the system's default.     * NOTE: This method is not recommended because it ignores the Locale from the browser not having the request object.     * @param session HttpSession object to use for lookup     * @return Locale The current Locale to use     */    public static Locale getLocale(HttpSession session) {        if (session == null) return Locale.getDefault();        return UtilHttp.getLocale(null, session);    }    public static void setLocale(HttpServletRequest request, String localeString) {        UtilHttp.setLocale(request.getSession(), UtilMisc.parseLocale(localeString));    }    public static void setLocale(HttpSession session, Locale locale) {        session.setAttribute("locale", locale);    }    public static void setLocaleIfNone(HttpSession session, String localeString) {        if (UtilValidate.isNotEmpty(localeString) && session.getAttribute("locale") == null) {            UtilHttp.setLocale(session, UtilMisc.parseLocale(localeString));        }    }    /**     * Get the currency string from the session.     * @param session HttpSession object to use for lookup     * @return String The ISO currency code     */    public static String getCurrencyUom(HttpSession session) {        // session, should override all if set there        String iso = (String) session.getAttribute("currencyUom");        // check userLogin next, ie if nothing to override in the session        if (iso == null) {            Map userLogin = (Map) session.getAttribute("userLogin");            if (userLogin == null) {                userLogin = (Map) session.getAttribute("autoUserLogin");            }            if (userLogin != null) {                iso = (String) userLogin.get("lastCurrencyUom");            }        }        // if none is set we will use the configured default        if (iso == null) {            try {                iso = UtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD");            } catch (Exception e) {                Debug.logWarning("Error getting the general:currency.uom.id.default value: " + e.toString(), module);            }        }        // if still none we will use the default for whatever locale we can get...        if (iso == null) {            Currency cur = Currency.getInstance(getLocale(session));            iso = cur.getCurrencyCode();        }        return iso;    }    /**     * Get the currency string from the session.     * @param request HttpServletRequest object to use for lookup     * @return String The ISO currency code     */    public static String getCurrencyUom(HttpServletRequest request) {        return getCurrencyUom(request.getSession());    }    /** Simple event to set the users per-session currency uom value */    public static void setCurrencyUom(HttpSession session, String currencyUom) {        session.setAttribute("currencyUom", currencyUom);    }    public static void setCurrencyUomIfNone(HttpSession session, String currencyUom) {        if (UtilValidate.isNotEmpty(currencyUom) && session.getAttribute("currencyUom") == null) {            session.setAttribute("currencyUom", currencyUom);        }    }    /** URL Encodes a Map of arguements */    public static String urlEncodeArgs(Map args) {    	return urlEncodeArgs(args, true);    }    /** URL Encodes a Map of arguements */    public static String urlEncodeArgs(Map args, boolean useExpandedEntites) {        StringBuffer buf = new StringBuffer();        if (args != null) {            Iterator i = args.entrySet().iterator();            while (i.hasNext()) {                Map.Entry entry = (Map.Entry) i.next();                String name = (String) entry.getKey();                Object value = entry.getValue();                String valueStr = null;                if (name != null && value != null) {                    if (value instanceof String) {                        valueStr = (String) value;                    } else {                        valueStr = value.toString();                    }                    if (valueStr != null && valueStr.length() > 0) {                        if (buf.length() > 0) {                        	if (useExpandedEntites) {                            	buf.append("&");                        	} else {                            	buf.append("&");                        	}                        }                        try {                            buf.append(URLEncoder.encode(name, "UTF-8"));                        } catch (UnsupportedEncodingException e) {                            Debug.logError(e, module);                        }                        buf.append('=');                        try {                            buf.append(URLEncoder.encode(valueStr, "UTF-8"));                        } catch (UnsupportedEncodingException e) {                            Debug.logError(e, module);                        }                    }                }            }        }        return buf.toString();    }    public static String encodeAmpersands(String htmlString) {        StringBuffer htmlBuffer = new StringBuffer(htmlString);        int ampLoc = -1;        while ((ampLoc = htmlBuffer.indexOf("&", ampLoc + 1)) != -1) {            //NOTE: this should work fine, but if it doesn't could try making sure all characters between & and ; are letters, that would qualify as an entity            // found ampersand, is it already and entity? if not change it to &            int semiLoc = htmlBuffer.indexOf(";", ampLoc);            if (semiLoc != -1) {                // found a semi colon, if it has another & or an = before it, don't count it as an entity, otherwise it may be an entity, so skip it                int eqLoc = htmlBuffer.indexOf("=", ampLoc);                int amp2Loc = htmlBuffer.indexOf("&", ampLoc + 1);                if ((eqLoc == -1 || eqLoc > semiLoc) && (amp2Loc == -1 || amp2Loc > semiLoc)) {                    continue;                }            }            // at this point not an entity, no substitute with a &            htmlBuffer.insert(ampLoc + 1, "amp;");        }        return htmlBuffer.toString();    }    public static String encodeBlanks(String htmlString) {        return htmlString.replaceAll(" ", "%20");    }    public static String setResponseBrowserProxyNoCache(HttpServletRequest request, HttpServletResponse response) {        setResponseBrowserProxyNoCache(response);        return "success";    }    public static void setResponseBrowserProxyNoCache(HttpServletResponse response) {        long nowMillis = System.currentTimeMillis();        response.setDateHeader("Expires", nowMillis);        response.setDateHeader("Last-Modified", nowMillis); // always modified        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // HTTP/1.1        response.addHeader("Cache-Control", "post-check=0, pre-check=0, false");        response.setHeader("Pragma", "no-cache"); // HTTP/1.0    }    public static String getContentTypeByFileName(String fileName) {        FileNameMap mime = URLConnection.getFileNameMap();        return mime.getContentTypeFor(fileName);    }    /**     * Stream an array of bytes to the browser     * This method will close the ServletOutputStream when finished     *     * @param response HttpServletResponse object to get OutputStream from     * @param bytes Byte array of content to stream     * @param contentType The content type to pass to the browser     * @throws IOException     */    public static void streamContentToBrowser(HttpServletResponse response, byte[] bytes, String contentType) throws IOException {        // tell the browser not the cache        setResponseBrowserProxyNoCache(response);        // set the response info        response.setContentLength(bytes.length);        if (contentType != null) {            response.setContentType(contentType);        }        // create the streams        OutputStream out = response.getOutputStream();        InputStream in = new ByteArrayInputStream(bytes);

⌨️ 快捷键说明

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