全部博文(282)
分类: Java
2006-06-26 12:11:24
className="com.jadecove.chain.sample.GetCustomerInfo"/>
className="com.jadecove.chain.sample.TestDriveVehicle"/>
className="com.jadecove.chain.sample.NegotiateSale"/>
className="com.jadecove.chain.sample.ArrangeFinancing"/>
className="com.jadecove.chain.sample.CloseSale"/>
public void init(ActionServlet servlet,
ModuleConfig moduleConfig)
throws ServletException {
synchronized (actions) {
actions.clear();
}
this.servlet = servlet;
this.moduleConfig = moduleConfig;
}
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Wrap multipart requests with a special wrapper
request = processMultipart(request);
// Identify the path component we will use to select a mapping
String path = processPath(request, response);
if (path == null) {
return;
}
// Select a Locale for the current user if requested
processLocale(request, response);
// Set the content type and no-caching headers if requested
processContent(request, response);
processNoCache(request, response);
// General purpose preprocessing hook
if (!processPreprocess(request, response)) {
return;
}
this.processCachedMessages(request, response);
// Identify the mapping for this request
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
}
// Check for any role required to perform this action
if (!processRoles(request, response, mapping)) {
return;
}
// Process any ActionForm bean related to this request
ActionForm form = processActionForm(request, response, mapping);
processPopulate(request, response, form, mapping);
if (!processValidate(request, response, form, mapping)) {
return;
}
// Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
return;
}
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
// Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping);
// Process the returned ActionForward instance
processForwardConfig(request, response, forward);
}
public void init(ActionServlet servlet,
ModuleConfig moduleConfig)
throws ServletException {
super.init(servlet, moduleConfig);
ControllerConfig controllerConfig =
moduleConfig.getControllerConfig();
String catalogName = controllerConfig.getCatalog();
catalog = CatalogFactory.getInstance().getCatalog(catalogName);
if (catalog == null) {
throw new ServletException("Cannot find catalog '" +
catalogName + "'");
}
String commandName = controllerConfig.getCommand();
command = catalog.getCommand(commandName);
if (command == null) {
throw new ServletException("Cannot find command '" +
commandName + "'");
}
}
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Wrap the request in the case of a multipart request
request = processMultipart(request);
// Create and populate a Context for this request
ActionContext context = contextInstance(request, response);
// Create and execute the command.
try {
command.execute(context);
} catch (Exception e) {
// Execute the exception processing chain??
throw new ServletException(e);
}
// Release the context.
context.release();
}
className="org.apache.struts.chain.commands.ExceptionCatcher"
catalogName="struts"
exceptionCommand="servlet-exception"/>
catalogName="struts"
name="process-action"
optional="false"/>
catalogName="struts"
name="process-view"
optional="false"/>
optional="true"/>
"org.apache.struts.chain.commands.servlet.SelectLocale"
/>
"org.apache.struts.chain.commands.servlet.RequestNoCache"
/>
"org.apache.struts.chain.commands.servlet.SetContentType"
/>
"org.apache.struts.chain.commands.servlet.SelectAction"
/>
"org.apache.struts.chain.commands.servlet.AuthorizeAction"
/>
"org.apache.struts.chain.commands.servlet.CreateActionForm"
/>
"org.apache.struts.chain.commands.servlet.PopulateActionForm"
/>
"org.apache.struts.chain.commands.servlet.ValidateActionForm"
/>
"org.apache.struts.chain.commands.servlet.SelectInput"
/>
"org.apache.struts.chain.commands.ExecuteCommand"
/>
"org.apache.struts.chain.commands.servlet.SelectForward"
/>
"org.apache.struts.chain.commands.SelectInclude"
/>
"org.apache.struts.chain.commands.servlet.PerformInclude"
/>
"org.apache.struts.chain.commands.servlet.CreateAction"
/>
"org.apache.struts.chain.commands.servlet.ExecuteAction"
/>
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) {
session.setAttribute(Globals.LOCALE_KEY, locale);
}
}
public boolean execute(Context context) throws Exception {
ActionContext actionCtx = (ActionContext) context;
// Are we configured to select Locale automatically?
ModuleConfig moduleConfig = actionCtx.getModuleConfig();
if (!moduleConfig.getControllerConfig().getLocale()) {
return (false);
}
// Retrieve and cache appropriate Locale for this request
Locale locale = getLocale(actionCtx);
actionCtx.setLocale(locale);
return (false);
}
protected Locale getLocale(ActionContext context) {
ServletActionContext saContext = (ServletActionContext) context;
// Has a Locale already been selected?
HttpSession session = saContext.getRequest().getSession();
Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
if (locale != null) {
return (locale);
}
// Select and cache the Locale to be used
locale = saContext.getRequest().getLocale();
if (locale == null) {
locale = Locale.getDefault();
}
session.setAttribute(Globals.LOCALE_KEY, locale);
return (locale);
}
name="servlet-standard-preprocess"
optional="true"/>
package com.jadecove.chain.commands;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.struts.apps.mailreader.Constants;
import org.apache.struts.chain.contexts.ActionContext;
import org.apache.struts.chain.contexts.ServletActionContext;
public class CheckUser implements Command {
public boolean execute(Context ctx) throws Exception {
ActionContext context = (ActionContext) ctx;
Object user = context.getSessionScope().get(Constants.USER_KEY);
if (user == null &&
context.getParameterMap().containsKey("checkUser")) {
HttpServletResponse response =
((ServletActionContext) ctx).getResponse();
response.sendError(403, "User not logged in.");
return true;
}
return false;
}
}
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml,
/WEB-INF/struts-config-registration.xml
chainConfig
/WEB-INF/chain-config.xml,
/WEB-INF/custom-chain-config.xml
1
catalogName="struts"
exceptionCommand="servlet-exception"/>
exceptionCatcher.setCatalogName("struts");
exceptionCatcher.setExceptionCommand("servlet-exception");
// (From the Command interface) Called when the Filter is first
// processed in sequence
public boolean execute(Context context);
// Called after a chain command throws an exception
// or the chain reaches its end
public boolean postprocess(Context context, Exception exception);
public boolean postprocess(Context context, Exception exception) {
// Do nothing if there was no exception thrown
if (exception == null) {
return (false);
}
// Store the exception in the context
ActionContext actionCtx = (ActionContext) context;
actionCtx.setException(exception);
// Execute the specified command
try {
Command command = lookupExceptionCommand();
if (command == null) {
throw new IllegalStateException
("Cannot find exceptionCommand '" +
exceptionCommand + "'");
}
command.execute(context);
} catch (Exception e) {
throw new IllegalStateException
("Exception chain threw exception");
}
return (true);
}
"org.apache.struts.chain.commands.servlet.ExceptionHandler"
/>
"org.apache.struts.chain.commands.servlet.PerformForward"
/>