📄 requestutils.java
字号:
} catch (ClassNotFoundException cnfe) {
throw new ServletException(
"Cannot find multipart class \""
+ multipartClass
+ "\""
+ ", exception: "
+ cnfe.getMessage());
} catch (InstantiationException ie) {
throw new ServletException(
"InstantiaionException when instantiating "
+ "multipart class \""
+ multipartClass
+ "\", exception: "
+ ie.getMessage());
} catch (IllegalAccessException iae) {
throw new ServletException(
"IllegalAccessException when instantiating "
+ "multipart class \""
+ multipartClass
+ "\", exception: "
+ iae.getMessage());
}
if (multipartHandler != null) {
return multipartHandler;
}
}
return multipartHandler;
}
/**
* Create a map containing all of the parameters supplied for a multipart
* request, keyed by parameter name. In addition to text and file elements
* from the multipart body, query string parameters are included as well.
*
* @param request The (wrapped) HTTP request whose parameters are to be
* added to the map.
* @param multipartHandler The multipart handler used to parse the request.
*
* @return the map containing all parameters for this multipart request.
*/
private static Map getAllParametersForMultipartRequest(
HttpServletRequest request,
MultipartRequestHandler multipartHandler) {
Map parameters = new HashMap();
Enumeration enum;
Hashtable elements = multipartHandler.getAllElements();
enum = elements.keys();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
parameters.put(key, elements.get(key));
}
if (request instanceof MultipartRequestWrapper) {
request = ((MultipartRequestWrapper)request).getRequest();
enum = request.getParameterNames();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
parameters.put(key, request.getParameterValues(key));
}
} else {
log.debug("Gathering multipart parameters for unwrapped request");
}
return parameters;
}
/**
* Return true if a message string for the specified message key
* is present for the specified Locale.
*
* @param pageContext The PageContext associated with this request
* @param bundle Name of the servlet context attribute for our
* message resources bundle
* @param locale Name of the session attribute for our user's Locale
* @param key Message key to be looked up and returned
* @return true if a message string for message key exists
* @exception JspException if a lookup error occurs (will have been
* saved in the request already)
*/
public static boolean present(
PageContext pageContext,
String bundle,
String locale,
String key)
throws JspException {
MessageResources resources =
retrieveMessageResources(pageContext, bundle, true);
Locale userLocale = retrieveUserLocale(pageContext, locale);
return (resources.isPresent(userLocale, key));
}
/**
* Compute the printable representation of a URL, leaving off the
* scheme/host/port part if no host is specified. This will typically
* be the case for URLs that were originally created from relative
* or context-relative URIs.
*
* @param url URL to render in a printable representation
* @return printable representation of a URL
*/
public static String printableURL(URL url) {
if (url.getHost() != null) {
return (url.toString());
}
String file = url.getFile();
String ref = url.getRef();
if (ref == null) {
return (file);
} else {
StringBuffer sb = new StringBuffer(file);
sb.append('#');
sb.append(ref);
return (sb.toString());
}
}
/**
* Return the context-relative URL that corresponds to the specified
* {@link ActionConfig}, relative to the module associated
* with the current modules's {@link ModuleConfig}.
*
* @param request The servlet request we are processing
* @param action ActionConfig to be evaluated
* @param pattern URL pattern used to map the controller servlet
* @return context-relative URL relative to the module
*
* @since Struts 1.1
*/
public static String actionURL(
HttpServletRequest request,
ActionConfig action,
String pattern) {
StringBuffer sb = new StringBuffer();
if (pattern.endsWith("/*")) {
sb.append(pattern.substring(0, pattern.length() - 2));
sb.append(action.getPath());
} else if (pattern.startsWith("*.")) {
ModuleConfig appConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
sb.append(appConfig.getPrefix());
sb.append(action.getPath());
sb.append(pattern.substring(1));
} else {
throw new IllegalArgumentException(pattern);
}
return (sb.toString());
}
/**
* Return the context-relative URL that corresponds to the specified
* ForwardConfig. The URL is calculated based on the properties of the
* {@link ForwardConfig} instance as follows:
* <ul>
* <li>If the <code>contextRelative</code> property is set, it is
* assumed that the <code>path</code> property contains a path
* that is already context-relative:
* <ul>
* <li>If the <code>path</code> property value starts with a slash,
* it is returned unmodified.</li>
* <li>If the <code>path</code> property value does not start
* with a slash, a slash is prepended.</li>
* </ul></li>
* <li>Acquire the <code>forwardPattern</code> property from the
* <code>ControllerConfig</code> for the application module used
* to process this request. If no pattern was configured, default
* to a pattern of <code>$M$P</code>, which is compatible with the
* hard-coded mapping behavior in Struts 1.0.</li>
* <li>Process the acquired <code>forwardPattern</code>, performing the
* following substitutions:
* <ul>
* <li><strong>$M</strong> - Replaced by the module prefix for the
* application module processing this request.</li>
* <li><strong>$P</strong> - Replaced by the <code>path</code>
* property of the specified {@link ForwardConfig}, prepended
* with a slash if it does not start with one.</li>
* <li><strong>$$</strong> - Replaced by a single dollar sign
* character.</li>
* <li><strong>$x</strong> (where "x" is any charater not listed
* above) - Silently omit these two characters from the result
* value. (This has the side effect of causing all other
* $+letter combinations to be reserved.)</li>
* </ul></li>
* </ul>
*
* @param request The servlet request we are processing
* @param forward ForwardConfig to be evaluated
* @return context-relative URL
* @since Struts 1.1
*/
public static String forwardURL(HttpServletRequest request, ForwardConfig forward) {
String path = forward.getPath();
// Handle a ForwardConfig marked as context relative
StringBuffer sb = new StringBuffer();
if (forward.getContextRelative()) {
if (!path.startsWith("/")) {
sb.append("/");
}
sb.append(path);
return (sb.toString());
}
// Calculate a context relative path for this ForwardConfig
ModuleConfig moduleConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
String forwardPattern = moduleConfig.getControllerConfig().getForwardPattern();
if (forwardPattern == null) {
// Performance optimization for previous default behavior
sb.append(moduleConfig.getPrefix());
// smoothly insert a '/' if needed
if (!path.startsWith("/")) {
sb.append("/");
}
sb.append(path);
} else {
boolean dollar = false;
for (int i = 0; i < forwardPattern.length(); i++) {
char ch = forwardPattern.charAt(i);
if (dollar) {
switch (ch) {
case 'M' :
sb.append(moduleConfig.getPrefix());
break;
case 'P' :
// add '/' if needed
if (!path.startsWith("/")) {
sb.append("/");
}
sb.append(path);
break;
case '$' :
sb.append('$');
break;
default :
; // Silently swallow
}
dollar = false;
continue;
} else if (ch == '$') {
dollar = true;
} else {
sb.append(ch);
}
}
}
return (sb.toString());
}
/**
* <p>Return the context-relative URL that corresponds to the specified
* <code>page</code> attribute value, calculated based on the
* <code>pagePattern</code> property of the current module's
* {@link ModuleConfig}.</p>
*
* @param request The servlet request we are processing
* @param page The module-relative URL to be substituted in
* to the <code>pagePattern</code> pattern for the current module
* (<strong>MUST</strong> start with a slash)
* @return context-relative URL
* @since Struts 1.1
*/
public static String pageURL(HttpServletRequest request, String page) {
StringBuffer sb = new StringBuffer();
ModuleConfig moduleConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
String pagePattern = moduleConfig.getControllerConfig().getPagePattern();
if (pagePattern == null) {
sb.append(moduleConfig.getPrefix());
sb.append(page);
} else {
boolean dollar = false;
for (int i = 0; i < pagePattern.length(); i++) {
char ch = pagePattern.charAt(i);
if (dollar) {
switch (ch) {
case 'M' :
sb.append(moduleConfig.getPrefix());
break;
case 'P' :
sb.append(page);
break;
case '$' :
sb.append('$');
break;
default :
; // Silently swallow
}
dollar = false;
continue;
} else if (ch == '$') {
dollar = true;
} else {
sb.append(ch);
}
}
}
return (sb.toString());
}
/**
* Return the URL representing the current request. This is equivalent
* to <code>HttpServletRequest.getRequestURL()</code> in Servlet 2.3.
*
* @param request The servlet request we are processing
* @return URL representing the current request
* @exception MalformedURLException if a URL cannot be created
*/
public static URL requestURL(HttpServletRequest request) throws MalformedURLException {
StringBuffer url = new StringBuffer();
String scheme = request.getScheme();
int port = request.getServerPort();
if (port < 0) {
port = 80; // Work around java.net.URL bug
}
url.append(scheme);
url.append("://");
url.append(request.getServerName());
if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 44
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -