📄 requestutils.java
字号:
///////////////////////////////////////////////////////////
// DeJaved by mDeJava v1.0. Copyright 1999 MoleSoftware. //
// To download last version of this software: //
// http://molesoftware.hypermatr.net //
// e-mail:molesoftware@mail.ru //
///////////////////////////////////////////////////////////
package org.apache.struts.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;
import org.apache.struts.config.*;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;
// Referenced classes of package org.apache.struts.util:
// MessageResources, ErrorMessages
public class RequestUtils
{
protected static Log log = null;
private static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.util.LocalStrings");
private static final String PREFIXES_KEY = "org.apache.struts.util.PREFIXES";
private static Method encode = null;
private static Map scopes = null;
static Class class$org$apache$struts$util$RequestUtils = null; /* synthetic field */
static Class class$java$lang$String = null; /* synthetic field */
static Class class$java$net$URLEncoder = null; /* synthetic field */
public RequestUtils()
{
}
public static URL absoluteURL(HttpServletRequest request, String path)
throws MalformedURLException
{
return new URL(serverURL(request), request.getContextPath() + path);
}
public static Class applicationClass(String className)
throws ClassNotFoundException
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if(classLoader == null)
classLoader = (class$org$apache$struts$util$RequestUtils != null ? class$org$apache$struts$util$RequestUtils : (class$org$apache$struts$util$RequestUtils = class$("org.apache.struts.util.RequestUtils"))).getClassLoader();
return classLoader.loadClass(className);
}
public static Object applicationInstance(String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
return applicationClass(className).newInstance();
}
public static Map computeParameters(PageContext pageContext, String paramId, String paramName, String paramProperty, String paramScope, String name, String property, String scope,
boolean transaction)
throws JspException
{
if(paramId == null && name == null && !transaction)
return null;
Map map = null;
try
{
if(name != null)
map = (Map)lookup(pageContext, name, property, scope);
}
catch(ClassCastException e)
{
saveException(pageContext, e);
throw new JspException(messages.getMessage("parameters.multi", name, property, scope));
}
catch(JspException e)
{
saveException(pageContext, e);
throw e;
}
Map results = null;
if(map != null)
results = new HashMap(map);
else
results = new HashMap();
if(paramId != null && paramName != null)
{
Object paramValue = null;
try
{
paramValue = lookup(pageContext, paramName, paramProperty, paramScope);
}
catch(JspException e)
{
saveException(pageContext, e);
throw e;
}
if(paramValue != null)
{
String paramString = null;
if(paramValue instanceof String)
paramString = (String)paramValue;
else
paramString = paramValue.toString();
Object mapValue = results.get(paramId);
if(mapValue == null)
results.put(paramId, paramString);
else
if(mapValue instanceof String)
{
String newValues[] = new String[2];
newValues[0] = (String)mapValue;
newValues[1] = paramString;
results.put(paramId, newValues);
}
else
{
String oldValues[] = (String[])mapValue;
String newValues[] = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
newValues[oldValues.length] = paramString;
results.put(paramId, newValues);
}
}
}
if(transaction)
{
HttpSession session = pageContext.getSession();
String token = null;
if(session != null)
token = (String)session.getAttribute("org.apache.struts.action.TOKEN");
if(token != null)
results.put("org.apache.struts.taglib.html.TOKEN", token);
}
return results;
}
public static String computeURL(PageContext pageContext, String forward, String href, String page, Map params, String anchor, boolean redirect)
throws MalformedURLException
{
return computeURL(pageContext, forward, href, page, null, params, anchor, redirect);
}
public static String computeURL(PageContext pageContext, String forward, String href, String page, String action, Map params, String anchor, boolean redirect)
throws MalformedURLException
{
return computeURL(pageContext, forward, href, page, action, params, anchor, redirect, true);
}
public static String computeURL(PageContext pageContext, String forward, String href, String page, String action, Map params, String anchor, boolean redirect,
boolean encodeSeparator)
throws MalformedURLException
{
int n = 0;
if(forward != null)
n++;
if(href != null)
n++;
if(page != null)
n++;
if(action != null)
n++;
if(n != 1)
throw new MalformedURLException(messages.getMessage("computeURL.specifier"));
ModuleConfig config = (ModuleConfig)pageContext.getRequest().getAttribute("org.apache.struts.action.MODULE");
if(config == null)
{
config = (ModuleConfig)pageContext.getServletContext().getAttribute("org.apache.struts.action.MODULE");
pageContext.getRequest().setAttribute("org.apache.struts.action.MODULE", config);
}
StringBuffer url = new StringBuffer();
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
if(forward != null)
{
ForwardConfig fc = config.findForwardConfig(forward);
if(fc == null)
throw new MalformedURLException(messages.getMessage("computeURL.forward", forward));
if(fc.getRedirect())
redirect = true;
if(fc.getPath().startsWith("/"))
{
url.append(request.getContextPath());
url.append(forwardURL(request, fc));
}
else
{
url.append(fc.getPath());
}
}
else
if(href != null)
url.append(href);
else
if(action != null)
{
url.append(getActionMappingURL(action, pageContext));
}
else
{
url.append(request.getContextPath());
url.append(pageURL(request, page));
}
if(anchor != null)
{
String temp = url.toString();
int hash = temp.indexOf(35);
if(hash >= 0)
url.setLength(hash);
url.append('#');
url.append(encodeURL(anchor));
}
if(params != null && params.size() > 0)
{
String temp = url.toString();
int hash = temp.indexOf(35);
if(hash >= 0)
{
anchor = temp.substring(hash + 1);
url.setLength(hash);
temp = url.toString();
}
else
{
anchor = null;
}
String separator = null;
if(redirect)
separator = "&";
else
if(encodeSeparator)
separator = "&";
else
separator = "&";
boolean question = temp.indexOf(63) >= 0;
for(Iterator keys = params.keySet().iterator(); keys.hasNext();)
{
String key = (String)keys.next();
Object value = params.get(key);
if(value == null)
{
if(!question)
{
url.append('?');
question = true;
}
else
{
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
}
else
if(value instanceof String)
{
if(!question)
{
url.append('?');
question = true;
}
else
{
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL((String)value));
}
else
if(value instanceof String[])
{
String values[] = (String[])value;
for(int i = 0; i < values.length; i++)
{
if(!question)
{
url.append('?');
question = true;
}
else
{
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL(values[i]));
}
}
else
{
if(!question)
{
url.append('?');
question = true;
}
else
{
url.append(separator);
}
url.append(encodeURL(key));
url.append('=');
url.append(encodeURL(value.toString()));
}
}
if(anchor != null)
{
url.append('#');
url.append(encodeURL(anchor));
}
}
if(pageContext.getSession() != null)
{
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
if(redirect)
return response.encodeRedirectURL(url.toString());
else
return response.encodeURL(url.toString());
}
else
{
return url.toString();
}
}
public static String getActionMappingName(String action)
{
String value = action;
int question = action.indexOf("?");
if(question >= 0)
value = value.substring(0, question);
int slash = value.lastIndexOf("/");
int period = value.lastIndexOf(".");
if(period >= 0 && period > slash)
value = value.substring(0, period);
if(value.startsWith("/"))
return value;
else
return "/" + value;
}
public static String getActionMappingURL(String action, PageContext pageContext)
{
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
StringBuffer value = new StringBuffer(request.getContextPath());
ModuleConfig config = (ModuleConfig)pageContext.getRequest().getAttribute("org.apache.struts.action.MODULE");
if(config != null)
value.append(config.getPrefix());
String servletMapping = (String)pageContext.getAttribute("org.apache.struts.action.SERVLET_MAPPING", 4);
if(servletMapping != null)
{
String queryString = null;
int question = action.indexOf("?");
if(question >= 0)
queryString = action.substring(question);
String actionMapping = getActionMappingName(action);
if(servletMapping.startsWith("*."))
{
value.append(actionMapping);
value.append(servletMapping.substring(1));
}
else
if(servletMapping.endsWith("/*"))
{
value.append(servletMapping.substring(0, servletMapping.length() - 2));
value.append(actionMapping);
}
else
if(servletMapping.equals("/"))
value.append(actionMapping);
if(queryString != null)
value.append(queryString);
}
else
{
if(!action.startsWith("/"))
value.append("/");
value.append(action);
}
return value.toString();
}
public static ActionForm createActionForm(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet)
{
FormBeanConfig config;
ActionForm instance;
String attribute = mapping.getAttribute();
if(attribute == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -