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

📄 uiutil.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                String mn = String.valueOf(minute);                while (mn.length() < 2)                {                    mn = "0" + mn;                }                String sc = String.valueOf(second);                while (sc.length() < 2)                {                    sc = "0" + sc;                }                sb.append(hr + ":" + mn + ":" + sc + " ");            }        }        else        {            sb.append("Unset");        }        return (sb.toString());    }    /**     * Return a string for logging, containing useful information about the     * current request - the URL, the method and parameters.     *      * @param request     *            the request object.     * @return a multi-line string containing information about the request.     */    public static String getRequestLogInfo(HttpServletRequest request)    {        String report;        report = "-- URL Was: " + getOriginalURL(request) + "\n";        report = report + "-- Method: " + request.getMethod() + "\n";        // First write the parameters we had        report = report + "-- Parameters were:\n";        Enumeration e = request.getParameterNames();        while (e.hasMoreElements())        {            String name = (String) e.nextElement();            if (name.equals("login_password"))            {                // We don't want to write a clear text password                // to the log, even if it's wrong!                report = report + "-- " + name + ": *not logged*\n";            }            else            {                report = report + "-- " + name + ": \""                        + request.getParameter(name) + "\"\n";            }        }        return report;    }    /**     * Obtain a parameter from the given request as an int. <code>-1</code> is     * returned if the parameter is garbled or does not exist.     *      * @param request     *            the HTTP request     * @param param     *            the name of the parameter     *      * @return the integer value of the parameter, or -1     */    public static int getIntParameter(HttpServletRequest request, String param)    {        String val = request.getParameter(param);        try        {            return Integer.parseInt(val);        }        catch (Exception e)        {            // Problem with parameter            return -1;        }    }    /**     * Obtain an array of int parameters from the given request as an int. null     * is returned if parameter doesn't exist. <code>-1</code> is returned in     * array locations if that particular value is garbled.     *      * @param request     *            the HTTP request     * @param param     *            the name of the parameter     *      * @return array of integers or null     */    public static int[] getIntParameters(HttpServletRequest request,            String param)    {        String[] request_values = request.getParameterValues(param);        if (request_values == null)        {            return null;        }        int[] return_values = new int[request_values.length];        for (int x = 0; x < return_values.length; x++)        {            try            {                return_values[x] = Integer.parseInt(request_values[x]);            }            catch (Exception e)            {                // Problem with parameter, stuff -1 in this slot                return_values[x] = -1;            }        }        return return_values;    }    /**     * Obtain a parameter from the given request as a boolean.     * <code>false</code> is returned if the parameter is garbled or does not     * exist.     *      * @param request     *            the HTTP request     * @param param     *            the name of the parameter     *      * @return the integer value of the parameter, or -1     */    public static boolean getBoolParameter(HttpServletRequest request,            String param)    {        return ((request.getParameter(param) != null) && request.getParameter(                param).equals("true"));    }    /**     * Get the button the user pressed on a submitted form. All buttons should     * start with the text <code>submit</code> for this to work. A default     * should be supplied, since often the browser will submit a form with no     * submit button pressed if the user presses enter.     *      * @param request     *            the HTTP request     * @param def     *            the default button     *      * @return the button pressed     */    public static String getSubmitButton(HttpServletRequest request, String def)    {        Enumeration e = request.getParameterNames();        while (e.hasMoreElements())        {            String parameterName = (String) e.nextElement();            if (parameterName.startsWith("submit"))            {                return parameterName;            }        }        return def;    }    /**     * Send an alert to the designated "alert recipient" - that is, when a     * database error or internal error occurs, this person is sent an e-mail     * with details.     * <P>     * The recipient is configured via the "alert.recipient" property in     * <code>dspace.cfg</code>. If this property is omitted, no alerts are     * sent.     * <P>     * This method "swallows" any exception that might occur - it will just be     * logged. This is because this method will usually be invoked as part of an     * error handling routine anyway.     *      * @param request     *            the HTTP request leading to the error     * @param exception     *            the exception causing the error, or null     */    public static void sendAlert(HttpServletRequest request, Exception exception)    {        String logInfo = UIUtil.getRequestLogInfo(request);        try        {            String recipient = ConfigurationManager                    .getProperty("alert.recipient");            if (recipient != null)            {                Email email = ConfigurationManager.getEmail("internal_error");                email.addRecipient(recipient);                email.addArgument(ConfigurationManager                        .getProperty("dspace.url"));                email.addArgument(new Date());                email.addArgument(request.getSession().getId());                email.addArgument(logInfo);                String stackTrace;                if (exception != null)                {                    StringWriter sw = new StringWriter();                    PrintWriter pw = new PrintWriter(sw);                    exception.printStackTrace(pw);                    pw.flush();                    stackTrace = sw.toString();                }                else                {                    stackTrace = "No exception";                }                email.addArgument(stackTrace);                email.send();            }        }        catch (Exception e)        {            // Not much we can do here!            log.warn("Unable to send email alert", e);        }    }        	/**	 * Encode a bitstream name for inclusion in a URL in an HTML document.	 * This differs from the usual URL-encoding, since we want pathname	 * separators to be passed through verbatim; this is required	 * so that relative paths in bitstream names and HTML references	 * work correctly.	 * <P>	 * If the link to a bitstream is generated with the pathname separators	 * escaped (e.g. "%2F" instead of "/") then the Web user agent perceives	 * it to be one pathname element, and relative URI paths within that	 * document containing ".." elements will be handled incorrectly.	 * <P>	 * @param stringIn	 *		  input string to encode	 * @param encoding	 *		  character encoding, e.g. UTF-8	 * @return the encoded string	 */	 public static String encodeBitstreamName(String stringIn, String encoding)	 throws java.io.UnsupportedEncodingException	 {		int curStart = 0;		int nextSlash = stringIn.indexOf("/");		String out = "";			while (nextSlash != -1)		{		    out += URLEncoder.encode(stringIn.substring(curStart, nextSlash), encoding) +			   "/";		    curStart = nextSlash + 1;		    nextSlash = stringIn.indexOf("/", curStart);		}		out += URLEncoder.encode(stringIn.substring(curStart), encoding);		return out;	 }	/** Version of encodeBitstreamName with one parameter, uses default encoding	 * <P>	 * @param stringIn	 *		  input string to encode	 * @return the encoded string	 */	 public static String encodeBitstreamName(String stringIn)	 throws java.io.UnsupportedEncodingException	 {	 	return encodeBitstreamName(stringIn, Constants.DEFAULT_ENCODING);	 }}

⌨️ 快捷键说明

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