use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.
the class ActionServlet method initModuleActions.
/**
* <p>Initialize the action configs for the specified module.</p>
*
* @param config ModuleConfig information for this module
* @throws ServletException if initialization cannot be performed
* @since Struts 1.3
*/
protected void initModuleActions(ModuleConfig config) throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + config.getPrefix() + "' action configs");
}
// Process ActionConfig extensions.
ActionConfig[] actionConfigs = config.findActionConfigs();
for (int i = 0; i < actionConfigs.length; i++) {
ActionConfig actionConfig = actionConfigs[i];
processActionConfigExtension(actionConfig, config);
}
for (int i = 0; i < actionConfigs.length; i++) {
ActionConfig actionConfig = actionConfigs[i];
// Verify that required fields are all present for the forward
// configs
ForwardConfig[] forwards = actionConfig.findForwardConfigs();
for (int j = 0; j < forwards.length; j++) {
ForwardConfig forward = forwards[j];
if (forward.getPath() == null) {
handleValueRequiredException("path", forward.getName(), "action forward");
}
}
// ... and the exception configs
ExceptionConfig[] exceptions = actionConfig.findExceptionConfigs();
for (int j = 0; j < exceptions.length; j++) {
ExceptionConfig exception = exceptions[j];
if (exception.getKey() == null) {
handleValueRequiredException("key", exception.getType(), "action exception config");
}
}
}
}
use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.
the class ActionServlet method processForwardConfigClass.
/**
* <p>Checks if the current forwardConfig is using the correct class based
* on the class of its configuration ancestor. If actionConfig is
* provided, then this method will process the forwardConfig as part
* of that actionConfig. If actionConfig is null, the forwardConfig
* will be processed as a global forward.</p>
*
* @param forwardConfig The forward to check.
* @param moduleConfig The config for the current module.
* @param actionConfig If applicable, the config for the current action.
* @return The forward config using the correct class as determined by the
* config's ancestor and its own overridden value.
* @throws UnavailableException if an instance of the forward config class
* cannot be created.
* @throws ServletException on class creation error
*/
protected ForwardConfig processForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
String ancestor = forwardConfig.getExtends();
if (ancestor == null) {
// Nothing to do, then
return forwardConfig;
}
// Make sure that this config is of the right class
ForwardConfig baseConfig = null;
if (actionConfig != null) {
// Look for this in the actionConfig
baseConfig = actionConfig.findForwardConfig(ancestor);
}
if (baseConfig == null) {
// Either this is a forwardConfig that inherits a global config,
// or actionConfig is null
baseConfig = moduleConfig.findForwardConfig(ancestor);
}
if (baseConfig == null) {
throw new UnavailableException("Unable to find " + "forward '" + ancestor + "' to extend.");
}
// Was our forwards's class overridden already?
if (forwardConfig.getClass().equals(ActionForward.class)) {
// Ensure that our forward is using the correct class
if (!baseConfig.getClass().equals(forwardConfig.getClass())) {
// Replace the config with an instance of the correct class
ForwardConfig newForwardConfig = null;
String baseConfigClassName = baseConfig.getClass().getName();
try {
newForwardConfig = (ForwardConfig) RequestUtils.applicationInstance(baseConfigClassName);
// copy the values
BeanUtils.copyProperties(newForwardConfig, forwardConfig);
} catch (Exception e) {
handleCreationException(baseConfigClassName, e);
}
// replace forwardConfig with newForwardConfig
if (actionConfig != null) {
actionConfig.removeForwardConfig(forwardConfig);
actionConfig.addForwardConfig(newForwardConfig);
} else {
// this is a global forward
moduleConfig.removeForwardConfig(forwardConfig);
moduleConfig.addForwardConfig(newForwardConfig);
}
forwardConfig = newForwardConfig;
}
}
return forwardConfig;
}
use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.
the class RequestProcessor method processForwardConfig.
/**
* <p>Forward or redirect to the specified destination, by the specified
* mechanism. This method uses a <code>ForwardConfig</code> object
* instead an <code>ActionForward</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param forward The ForwardConfig controlling where we go next
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
*/
protected void processForwardConfig(HttpServletRequest request, HttpServletResponse response, ForwardConfig forward) throws IOException, ServletException {
if (forward == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug("processForwardConfig(" + forward + ")");
}
String forwardPath = forward.getPath();
String uri;
// If the forward can be unaliased into an action, then use the path of the action
String actionIdPath = RequestUtils.actionIdURL(forward, request, servlet);
if (actionIdPath != null) {
forwardPath = actionIdPath;
ForwardConfig actionIdForward = new ForwardConfig(forward);
actionIdForward.setPath(actionIdPath);
forward = actionIdForward;
}
// processing (ie. they're absolute)
if (forwardPath.startsWith("/")) {
// get module relative uri
uri = RequestUtils.forwardURL(request, forward, null);
} else {
uri = forwardPath;
}
if (forward.getRedirect()) {
// only prepend context path for relative uri
if (uri.startsWith("/")) {
uri = request.getContextPath() + uri;
}
response.sendRedirect(response.encodeRedirectURL(uri));
} else {
doForward(uri, request, response);
}
}
use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.
the class RequestProcessor method processValidate.
/**
* <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);
}
// 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);
}
use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.
the class TestActionServlet method testProcessForwardConfigClass.
/**
* Make sure processForwardConfigClass() returns an instance of the
* correct class if the base config is using a custom class.
*/
public void testProcessForwardConfigClass() throws Exception {
CustomForwardConfig customBase = new CustomForwardConfig("success", "/success.jsp");
moduleConfig.addForwardConfig(customBase);
ActionForward customSub = new ActionForward();
customSub.setName("failure");
customSub.setExtends("success");
moduleConfig.addForwardConfig(customSub);
ForwardConfig result = actionServlet.processForwardConfigClass(customSub, moduleConfig, null);
assertTrue("Incorrect class of forward config", result instanceof CustomForwardConfig);
assertEquals("Incorrect name", customSub.getName(), result.getName());
assertEquals("Incorrect path", customSub.getPath(), result.getPath());
assertEquals("Incorrect extends", customSub.getExtends(), result.getExtends());
assertSame("Result was not registered in the module config", result, moduleConfig.findForwardConfig("failure"));
}
Aggregations