use of org.apache.struts.action.ActionForm in project sonarqube by SonarSource.
the class RequestUtils method createActionForm.
/**
* <p>Create (if necessary) and return an <code>ActionForm</code> instance
* appropriate for this request. If no <code>ActionForm</code> instance
* is required, return <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param mapping The action mapping for this request
* @param moduleConfig The configuration for this module
* @param servlet The action servlet
* @return ActionForm instance associated with this request
*/
public static ActionForm createActionForm(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) {
// Is there a form bean associated with this mapping?
String attribute = mapping.getAttribute();
if (attribute == null) {
return (null);
}
// Look up the form bean configuration information to use
String name = mapping.getName();
FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
if (config == null) {
log.warn("No FormBeanConfig found under '" + name + "'");
return (null);
}
ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
// Can we recycle the existing form bean instance (if there is one)?
if ((instance != null) && config.canReuse(instance)) {
return (instance);
}
return createActionForm(config, servlet);
}
use of org.apache.struts.action.ActionForm in project sonarqube by SonarSource.
the class RequestUtils method lookupActionForm.
private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope) {
// Look up any existing form bean instance
if (log.isDebugEnabled()) {
log.debug(" Looking for ActionForm bean instance in scope '" + scope + "' under attribute key '" + attribute + "'");
}
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(scope)) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
return (instance);
}
use of org.apache.struts.action.ActionForm in project sonarqube by SonarSource.
the class RequestUtils method populate.
/**
* <p>Populate the properties of the specified JavaBean from the specified
* HTTP request, based on matching each parameter name (plus an optional
* prefix and/or suffix) against the corresponding JavaBeans "property
* setter" methods in the bean's class. Suitable conversion is done for
* argument types as described under <code>setProperties</code>.</p>
*
* <p>If you specify a non-null <code>prefix</code> and a non-null
* <code>suffix</code>, the parameter name must match
* <strong>both</strong> conditions for its value(s) to be used in
* populating bean properties. If the request's content type is
* "multipart/form-data" and the method is "POST", the
* <code>HttpServletRequest</code> object will be wrapped in a
* <code>MultipartRequestWrapper</code object.</p>
*
* @param bean The JavaBean whose properties are to be set
* @param prefix The prefix (if any) to be prepend to bean property names
* when looking for matching parameters
* @param suffix The suffix (if any) to be appended to bean property
* names when looking for matching parameters
* @param request The HTTP request whose parameters are to be used to
* populate bean properties
* @throws ServletException if an exception is thrown while setting
* property values
*/
public static void populate(Object bean, String prefix, String suffix, HttpServletRequest request) throws ServletException {
// Build a list of relevant request parameters from this request
HashMap properties = new HashMap();
// Iterator of parameter names
Enumeration names = null;
// Map for multipart parameters
Map multipartParameters = null;
String contentType = request.getContentType();
String method = request.getMethod();
boolean isMultipart = false;
if (bean instanceof ActionForm) {
((ActionForm) bean).setMultipartRequestHandler(null);
}
MultipartRequestHandler multipartHandler = null;
if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST"))) {
// Get the ActionServletWrapper from the form bean
ActionServletWrapper servlet;
if (bean instanceof ActionForm) {
servlet = ((ActionForm) bean).getServletWrapper();
} else {
throw new ServletException("bean that's supposed to be " + "populated from a multipart request is not of type " + "\"org.apache.struts.action.ActionForm\", but type " + "\"" + bean.getClass().getName() + "\"");
}
// Obtain a MultipartRequestHandler
multipartHandler = getMultipartHandler(request);
if (multipartHandler != null) {
isMultipart = true;
// Set servlet and mapping info
servlet.setServletFor(multipartHandler);
multipartHandler.setMapping((ActionMapping) request.getAttribute(Globals.MAPPING_KEY));
// Initialize multipart request class handler
multipartHandler.handleRequest(request);
//stop here if the maximum length has been exceeded
Boolean maxLengthExceeded = (Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
return;
}
//retrieve form values and put into properties
multipartParameters = getAllParametersForMultipartRequest(request, multipartHandler);
names = Collections.enumeration(multipartParameters.keySet());
}
}
if (!isMultipart) {
names = request.getParameterNames();
}
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String stripped = name;
if (prefix != null) {
if (!stripped.startsWith(prefix)) {
continue;
}
stripped = stripped.substring(prefix.length());
}
if (suffix != null) {
if (!stripped.endsWith(suffix)) {
continue;
}
stripped = stripped.substring(0, stripped.length() - suffix.length());
}
Object parameterValue = null;
if (isMultipart) {
parameterValue = multipartParameters.get(name);
parameterValue = rationalizeMultipleFileProperty(bean, name, parameterValue);
} else {
parameterValue = request.getParameterValues(name);
}
// such as 'org.apache.struts.action.CANCEL'
if (!(stripped.startsWith("org.apache.struts."))) {
properties.put(stripped, parameterValue);
}
}
// Set the corresponding properties of our bean
try {
BeanUtils.populate(bean, properties);
} catch (Exception e) {
throw new ServletException("BeanUtils.populate", e);
} finally {
if (multipartHandler != null) {
// Set the multipart request handler for our ActionForm.
// If the bean isn't an ActionForm, an exception would have been
// thrown earlier, so it's safe to assume that our bean is
// in fact an ActionForm.
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
}
}
}
use of org.apache.struts.action.ActionForm in project sonarqube by SonarSource.
the class CopyFormToContext method findOrCreateForm.
/**
* <p>Actually find or create an instance of ActionForm configured under
* the form-bean-name <code>effectiveFormName</code>, looking in in the
* <code>ActionContext's</code> scope as identified by
* <code>effectiveScope</code>. If a form is created, it will also be
* stored in that scope.</p>
*
* <p><b>NOTE:</b> This specific method depends on the instance of
* <code>ActionContext</code> which is passed being a subclass of
* <code>ActionContextBase</code>, which implements the utility method
* <code>findOrCreateActionForm</code>. </p>
*
* @param ctx The ActionContext we are processing
* @param effectiveFormName the target form name
* @param effectiveScope The target scope
* @return ActionForm instnace, storing in scope if created
* @throws InstantiationException If ActionContext is not subsclass of
* ActionContextBase
* @throws InstantiationException If object cannot be created
* @throws IllegalArgumentException On form not found in/ scope
* @throws IllegalAccessException On failed instantiation
* @throws IllegalStateException If ActionContext is not a subclass of
* ActionBase
*/
protected ActionForm findOrCreateForm(ActionContext ctx, String effectiveFormName, String effectiveScope) throws IllegalAccessException, InstantiationException {
ActionContextBase context;
try {
context = (ActionContextBase) ctx;
} catch (ClassCastException e) {
throw new IllegalStateException("ActionContext [" + ctx + "]" + " must be subclass of ActionContextBase");
}
ActionForm form = context.findOrCreateActionForm(effectiveFormName, effectiveScope);
if (form == null) {
throw new IllegalArgumentException("No form found under scope [" + effectiveScope + "] and formName [" + effectiveFormName + "]");
}
return form;
}
use of org.apache.struts.action.ActionForm in project sonarqube by SonarSource.
the class CopyFormToContext method execute.
// ------------------------------------------------------
/**
* <p>Look up an ActionForm instance based on the configured properties of
* this command and copy it into the <code>Context</code>. After this
* command successfully executes, an ActionForm instance will exist in the
* specified scope and will be available, for example for backing fields
* in an HTML form. It will also be in the <code>ActionContext</code>
* available for another command to do prepopulation of values or other
* preparation.</p>
*
* @param actionContext Our ActionContext
* @return TRUE if processing should halt
* @throws Exception on any error
*/
public boolean execute(ActionContext actionContext) throws Exception {
ActionForm form = findOrCreateForm(actionContext);
if (isEmpty(getToKey())) {
throw new IllegalStateException("Property 'toKey' must be defined.");
}
actionContext.put(getToKey(), form);
return false;
}
Aggregations