📄 requestprocessor.java
字号:
// 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");
log.error(msg + " " + request.getRequestURI());
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
* @param response The servlet response we are creating
* @param form The ActionForm instance we are populating
* @param mapping The ActionMapping we are using
* @throws ServletException if thrown by RequestUtils.populate()
*/
protected void processPopulate(HttpServletRequest request,
HttpServletResponse response, ActionForm form, ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
// Populate the bean properties of this ActionForm instance
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
form.setServlet(this.servlet);
form.reset(mapping, request);
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
// Set the cancellation request attribute if appropriate
if ((request.getParameter(Globals.CANCEL_PROPERTY) != null)
|| (request.getParameter(Globals.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
}
/**
* <p>General-purpose preprocessing hook that can be overridden as
* required by subclasses. Return <code>true</code> if you want standard
* processing to continue, or <code>false</code> if the response has
* already been completed. The default implementation does nothing.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @return <code>true</code> to continue normal processing;
* <code>false</code> if a response has been created.
*/
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
return (true);
}
/**
* <p>If this action is protected by security roles, make sure that the
* current user possesses at least one of them. Return <code>true</code>
* to continue normal processing, or <code>false</code> if an appropriate
* response has been created and processing should terminate.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param mapping The mapping we are using
* @return <code>true</code> to continue normal processing;
* <code>false</code> if a response has been created.
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
*/
protected boolean processRoles(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws IOException, ServletException {
// Is this action protected by role requirements?
String[] roles = mapping.getRoleNames();
if ((roles == null) || (roles.length < 1)) {
return (true);
}
// Check the current user against the list of required roles
for (int i = 0; i < roles.length; i++) {
if (request.isUserInRole(roles[i])) {
if (log.isDebugEnabled()) {
log.debug(" User '" + request.getRemoteUser()
+ "' has role '" + roles[i] + "', granting access");
}
return (true);
}
}
// The current user is not authorized for this action
if (log.isDebugEnabled()) {
log.debug(" User '" + request.getRemoteUser()
+ "' does not have any required role, denying access");
}
response.sendError(HttpServletResponse.SC_FORBIDDEN,
getInternal().getMessage("notAuthorized", mapping.getPath()));
return (false);
}
/**
* <p>If this request was not cancelled, and the request's {@link
* ActionMapping} has not disabled validation, call the
* <code>validate</code> method of the specified {@link ActionForm}, and
* forward to the input path if there were any errors. Return
* <code>true</code> if we should continue processing, or
* <code>false</code> if we have already forwarded control back to the
* input form.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param form The ActionForm instance we are populating
* @param mapping The ActionMapping we are using
* @return <code>true</code> to continue normal processing;
* <code>false</code> if a response has been created.
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @throws InvalidCancelException if a cancellation is attempted
* without the proper action configuration.
*/
protected boolean processValidate(HttpServletRequest request,
HttpServletResponse response, ActionForm form, ActionMapping mapping)
throws IOException, ServletException, InvalidCancelException {
if (form == null) {
return (true);
}
// Has validation been turned off for this mapping?
if (!mapping.getValidate()) {
return (true);
}
// Was this request cancelled? If it has been, the mapping also
// needs to state whether the cancellation is permissable; otherwise
// the cancellation is considered to be a symptom of a programmer
// error or a spoof.
if (request.getAttribute(Globals.CANCEL_KEY) != null) {
if (mapping.getCancellable()) {
if (log.isDebugEnabled()) {
log.debug(" Cancelled transaction, skipping validation");
}
return (true);
} else {
request.removeAttribute(Globals.CANCEL_KEY);
throw new InvalidCancelException();
}
}
// Call the form bean's validation method
if (log.isDebugEnabled()) {
log.debug(" Validating input form properties");
}
ActionMessages errors = form.validate(mapping, request);
if ((errors == null) || errors.isEmpty()) {
if (log.isTraceEnabled()) {
log.trace(" No errors detected, accepting input");
}
return (true);
}
// Special handling for multipart request
if (form.getMultipartRequestHandler() != null) {
if (log.isTraceEnabled()) {
log.trace(" Rolling back multipart request");
}
form.getMultipartRequestHandler().rollback();
}
// Was an input path (or forward) specified for this mapping?
String input = mapping.getInput();
if (input == null) {
if (log.isTraceEnabled()) {
log.trace(" Validation failed but no input form available");
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("noInput", mapping.getPath()));
return (false);
}
// Save our error messages and return to the input form if possible
if (log.isDebugEnabled()) {
log.debug(" Validation failed, returning to '" + input + "'");
}
request.setAttribute(Globals.ERROR_KEY, errors);
if (moduleConfig.getControllerConfig().getInputForward()) {
ForwardConfig forward = mapping.findForward(input);
processForwardConfig(request, response, forward);
} else {
internalModuleRelativeForward(input, request, response);
}
return (false);
}
/**
* <p>Do a module relative forward to specified URI using request
* dispatcher. URI is relative to the current module. The real URI is
* compute by prefixing the module name.</p> <p>This method is used
* internally and is not part of the public API. It is advised to not use
* it in subclasses. </p>
*
* @param uri Module-relative URI to forward to
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void internalModuleRelativeForward(String uri,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Construct a request dispatcher for the specified path
uri = moduleConfig.getPrefix() + uri;
// Delegate the processing of this request
// :FIXME: - exception handling?
if (log.isDebugEnabled()) {
log.debug(" Delegating via forward to '" + uri + "'");
}
doForward(uri, request, response);
}
/**
* <p>Do a module relative include to specified URI using request
* dispatcher. URI is relative to the current module. The real URI is
* compute by prefixing the module name.</p> <p>This method is used
* internally and is not part of the public API. It is advised to not use
* it in subclasses.</p>
*
* @param uri Module-relative URI to include
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void internalModuleRelativeInclude(String uri,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Construct a request dispatcher for the specified path
uri = moduleConfig.getPrefix() + uri;
// Delegate the processing of this request
// FIXME - exception handling?
if (log.isDebugEnabled()) {
log.debug(" Delegating via include to '" + uri + "'");
}
doInclude(uri, request, response);
}
/**
* <p>Do a forward to specified URI using a <code>RequestDispatcher</code>.
* This method is used by all internal method needing to do a
* forward.</p>
*
* @param uri Context-relative URI to forward to
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void doForward(String uri, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.forward(request, response);
}
/**
* <p>Do an include of specified URI using a <code>RequestDispatcher</code>.
* This method is used by all internal method needing to do an
* include.</p>
*
* @param uri Context-relative URI to include
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void doInclude(String uri, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.include(request, response);
}
// -------------------------------------------------------- Support Methods
/**
* <p>Return the <code>MessageResources</code> instance containing our
* internal message strings.</p>
*
* @return The <code>MessageResources</code> instance containing our
* internal message strings.
*/
protected MessageResources getInternal() {
return (servlet.getInternal());
}
/**
* <p>Return the <code>ServletContext</code> for the web application in
* which we are running.</p>
*
* @return The <code>ServletContext</code> for the web application.
*/
protected ServletContext getServletContext() {
return (servlet.getServletContext());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -