📄 requestprocessor.java
字号:
response.sendRedirect(response.encodeRedirectURL(uri));
} else {
doForward(uri, request, response);
}
}
// :FIXME: if Action.execute throws Exception, and Action.process has been removed,
// should the process* methods still throw IOException, ServletException?
/**
* <P>Ask the specified <code>Action</code> instance to handle this
* request. Return the <code>ActionForward</code> instance (if any)
* returned by the called <code>Action</code> for further processing.
* </P>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param action The Action instance to be used
* @param form The ActionForm instance to pass to this Action
* @param mapping The ActionMapping instance to pass to this Action
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
protected ActionForward
processActionPerform(HttpServletRequest request,
HttpServletResponse response,
Action action,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response,
e, form, mapping));
}
}
/**
* <p>Removes any <code>ActionMessages</code> object stored in the session under
* <code>Globals.MESSAGE_KEY</code> if the messages'
* <code>isAccessed</code> method returns true. This allows messages to
* be stored in the session, display one time, and be released here.</p>
*
* @param request The servlet request we are processing.
* @param response The servlet response we are creating.
*
* @since Struts 1.2
*/
protected void processCachedMessages(
HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
if (session == null) {
return;
}
ActionMessages messages =
(ActionMessages) session.getAttribute(Globals.MESSAGE_KEY);
if (messages == null) {
return;
}
if (messages.isAccessed()) {
session.removeAttribute(Globals.MESSAGE_KEY);
}
}
/**
* <p>Set the default content type (with optional character encoding) for
* all responses if requested. <strong>NOTE</strong> - This header will
* be overridden automatically if a
* <code>RequestDispatcher.forward</code> call is
* ultimately invoked.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*/
protected void processContent(HttpServletRequest request,
HttpServletResponse response) {
String contentType = moduleConfig.getControllerConfig().getContentType();
if (contentType != null) {
response.setContentType(contentType);
}
}
/**
* <p>Ask our exception handler to handle the exception. Return the
* <code>ActionForward</code> instance (if any) returned by the
* called <code>ExceptionHandler</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are processing
* @param exception The exception being handled
* @param form The ActionForm we are processing
* @param mapping The ActionMapping we are using
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
protected ActionForward processException(HttpServletRequest request,
HttpServletResponse response,
Exception exception,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
// Is there a defined handler for this exception?
ExceptionConfig config = mapping.findException(exception.getClass());
if (config == null) {
log.warn(getInternal().getMessage("unhandledException",
exception.getClass()));
if (exception instanceof IOException) {
throw (IOException) exception;
} else if (exception instanceof ServletException) {
throw (ServletException) exception;
} else {
throw new ServletException(exception);
}
}
// Use the configured exception handling
try {
ExceptionHandler handler = (ExceptionHandler)
RequestUtils.applicationInstance(config.getHandler());
return (handler.execute(exception, config, mapping, form,
request, response));
} catch (Exception e) {
throw new ServletException(e);
}
}
/**
* <p>Process a forward requested by this mapping (if any). Return
* <code>true</code> if standard processing should continue, or
* <code>false</code> if we have already handled this request.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param mapping The ActionMapping we are using
*/
protected boolean processForward(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping)
throws IOException, ServletException {
// Are we going to processing this request?
String forward = mapping.getForward();
if (forward == null) {
return (true);
}
internalModuleRelativeForward(forward, request, response);
return (false);
}
/**
* <p>Process an include requested by this mapping (if any). Return
* <code>true</code> if standard processing should continue, or
* <code>false</code> if we have already handled this request.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param mapping The ActionMapping we are using
*/
protected boolean processInclude(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping)
throws IOException, ServletException {
// Are we going to processing this request?
String include = mapping.getInclude();
if (include == null) {
return (true);
}
internalModuleRelativeInclude(include, request, response);
return (false);
}
/**
* <p>Automatically select a <code>Locale</code> for the current user, if requested.
* <strong>NOTE</strong> - configuring Locale selection will trigger
* the creation of a new <code>HttpSession</code> if necessary.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*/
protected void processLocale(HttpServletRequest request,
HttpServletResponse response) {
// Are we configured to select the Locale automatically?
if (!moduleConfig.getControllerConfig().getLocale()) {
return;
}
// Has a Locale already been selected?
HttpSession session = request.getSession();
if (session.getAttribute(Globals.LOCALE_KEY) != null) {
return;
}
// Use the Locale returned by the servlet container (if any)
Locale locale = request.getLocale();
if (locale != null) {
if (log.isDebugEnabled()) {
log.debug(" Setting user locale '" + locale + "'");
}
session.setAttribute(Globals.LOCALE_KEY, locale);
}
}
/**
* <p>Select the mapping used to process the selection path for this request.
* If no mapping can be identified, create an error response and return
* <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param path The portion of the request URI for selecting a mapping
*
* @exception IOException if an input/output error occurs
*/
protected ActionMapping processMapping(HttpServletRequest request,
HttpServletResponse response,
String path)
throws IOException {
// Is there a mapping for this path?
ActionMapping mapping = (ActionMapping)
moduleConfig.findActionConfig(path);
// If a mapping is found, put it in the request and return it
if (mapping != null) {
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
// Locate the mapping for unknown paths (if any)
ActionConfig configs[] = moduleConfig.findActionConfigs();
for (int i = 0; i < configs.length; i++) {
if (configs[i].getUnknown()) {
mapping = (ActionMapping) configs[i];
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
}
// No mapping can be found to process this request
String msg = getInternal().getMessage("processInvalid", path);
log.error(msg);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return null;
}
/**
* <p>If this is a multipart request, wrap it with a special wrapper.
* Otherwise, return the request unchanged.</p>
*
* @param request The HttpServletRequest we are processing
*/
protected HttpServletRequest processMultipart(HttpServletRequest request) {
if (!"POST".equalsIgnoreCase(request.getMethod())) {
return (request);
}
String contentType = request.getContentType();
if ((contentType != null) &&
contentType.startsWith("multipart/form-data")) {
return (new MultipartRequestWrapper(request));
} else {
return (request);
}
}
/**
* <p>Set the no-cache headers for all responses, if requested.
* <strong>NOTE</strong> - This header will be overridden
* automatically if a <code>RequestDispatcher.forward</code> call is
* ultimately invoked.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*/
protected void processNoCache(HttpServletRequest request,
HttpServletResponse response) {
if (moduleConfig.getControllerConfig().getNocache()) {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
response.setDateHeader("Expires", 1);
}
}
/**
* <p>Identify and return the path component (from the request URI) that
* we will use to select an <code>ActionMapping</code> with which to dispatch.
* If no such path can be identified, create an error response and return
* <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
*/
protected String processPath(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
String path = null;
// For prefix matching, match on the path info (if any)
path = (String) request.getAttribute(INCLUDE_PATH_INFO);
if (path == null) {
path = request.getPathInfo();
}
if ((path != null) && (path.length() > 0)) {
return (path);
}
// For extension matching, strip the module prefix and extension
path = (String) request.getAttribute(INCLUDE_SERVLET_PATH);
if (path == null) {
path = request.getServletPath();
}
String prefix = moduleConfig.getPrefix();
if (!path.startsWith(prefix)) {
String msg =
getInternal().getMessage("processPath", request.getRequestURI());
log.error(msg);
response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
return null;
}
path = path.substring(prefix.length());
int slash = path.lastIndexOf("/");
int period = path.lastIndexOf(".");
if ((period >= 0) && (period > slash)) {
path = path.substring(0, period);
}
return (path);
}
/**
* <p>Populate the properties of the specified <code>ActionForm</code> instance from
* the request parameters included with this request. In addition,
* request attribute <code>Globals.CANCEL_KEY</code> will be set if
* the request was submitted with a button created by
* <code>CancelTag</code>.</p>
*
* @param request The servlet request we are processing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -