/hostadmiral/trunk/src/ak/strutsx/taglib/EmptyTag.java |
---|
0,0 → 1,56 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx.taglib; |
import java.util.Iterator; |
import java.util.Collection; |
import java.util.Map; |
import javax.servlet.jsp.JspException; |
import org.apache.struts.util.RequestUtils; |
import org.apache.struts.taglib.logic.ConditionalTagBase; |
public class EmptyTag |
extends ConditionalTagBase |
{ |
protected boolean condition() |
throws JspException |
{ |
return condition(true); |
} |
protected boolean condition(boolean desired) |
throws JspException |
{ |
if(this.name == null) { |
JspException e = new JspException(messages.getMessage("empty.noNameAttribute")); |
RequestUtils.saveException(pageContext, e); |
throw e; |
} |
boolean empty = true; |
Object value = null; |
if(this.property == null) |
value = RequestUtils.lookup(pageContext, name, scope); |
else |
value = RequestUtils.lookup(pageContext, name, property, scope); |
if(value != null) { |
if(value instanceof String) |
empty = (((String)value).length() < 1); |
else if(value instanceof Collection) |
empty = ((Collection)value).isEmpty(); |
else if(value instanceof Map) |
empty = ((Map)value).isEmpty(); |
else if(value instanceof Iterator) |
empty = !((Iterator)value).hasNext(); |
else |
empty = false; |
} |
return (empty == desired); |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/taglib/NotEmptyTag.java |
---|
0,0 → 1,18 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx.taglib; |
import javax.servlet.jsp.JspException; |
public class NotEmptyTag |
extends EmptyTag |
{ |
protected boolean condition() |
throws JspException |
{ |
return condition(false); |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/taglib/RootTag.java |
---|
0,0 → 1,37 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx.taglib; |
import java.io.IOException; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.jsp.JspException; |
import javax.servlet.jsp.JspWriter; |
import javax.servlet.jsp.PageContext; |
import javax.servlet.jsp.tagext.TagSupport; |
import org.apache.struts.Globals; |
import org.apache.struts.util.MessageResources; |
import org.apache.struts.util.RequestUtils; |
public class RootTag |
extends TagSupport |
{ |
public int doStartTag() |
throws JspException |
{ |
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); |
try { |
pageContext.getOut().write(request.getContextPath()); |
} |
catch(IOException ex) { |
throw new JspException("Cannot write to output" + ex); |
} |
return EVAL_BODY_INCLUDE; |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/taglib/MessageTag.java |
---|
0,0 → 1,91 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx.taglib; |
import java.util.Locale; |
import javax.servlet.jsp.JspException; |
import javax.servlet.jsp.tagext.TagSupport; |
import org.apache.struts.Globals; |
import org.apache.struts.util.MessageResources; |
import org.apache.struts.util.RequestUtils; |
import org.apache.struts.util.ResponseUtils; |
public class MessageTag |
extends org.apache.struts.taglib.bean.MessageTag |
{ |
protected String valuesName = null; |
public String getValuesName() |
{ |
return this.valuesName; |
} |
public void setValuesName(String valuesName) |
{ |
this.valuesName = valuesName; |
} |
protected String valuesProperty = null; |
public String getValuesProperty() |
{ |
return this.valuesProperty; |
} |
public void setValuesProperty(String valuesProperty) |
{ |
this.valuesProperty = valuesProperty; |
} |
public int doStartTag() |
throws JspException |
{ |
String key = this.key; |
if(key == null) { |
Object value = RequestUtils.lookup(pageContext, name, property, scope); |
if(value != null && !(value instanceof String)) { |
JspException e = new JspException(messages.getMessage("message.property", key)); |
RequestUtils.saveException(pageContext, e); |
throw e; |
} |
key = (String)value; |
} |
Object[] values = null; |
if(valuesName != null || valuesProperty != null) { |
Object valuesObj = RequestUtils.lookup(pageContext, |
(valuesName == null) ? name : valuesName, valuesProperty, scope); |
if(valuesObj != null && !(valuesObj instanceof Object[])) { |
JspException e = new JspException("Properties are not an array"); |
RequestUtils.saveException(pageContext, e); |
throw e; |
} |
values = (Object[])valuesObj; |
} |
String message = RequestUtils.message(pageContext, this.bundle, |
this.localeKey, key, values); |
if(message == null) { |
JspException e = new JspException |
(messages.getMessage("message.message", "\"" + key + "\"")); |
RequestUtils.saveException(pageContext, e); |
throw e; |
} |
ResponseUtils.write(pageContext, message); |
return SKIP_BODY; |
} |
public void release() |
{ |
super.release(); |
valuesName = null; |
valuesProperty = null; |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/taglib/ErrorsIteratorTag.java |
---|
0,0 → 1,46 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx.taglib; |
import javax.servlet.jsp.JspException; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.taglib.html.ErrorsTag; |
import org.apache.struts.util.RequestUtils; |
public class ErrorsIteratorTag |
extends ErrorsTag |
{ |
protected String id = null; |
public String getId() |
{ |
return this.id; |
} |
public void setId(String id) |
{ |
this.id = id; |
} |
public int doStartTag() |
throws JspException |
{ |
ActionErrors errors = RequestUtils.getActionErrors(pageContext, name); |
if(errors != null) |
pageContext.setAttribute(id, (property == null) ? errors.get() : errors.get(property)); |
return EVAL_BODY_INCLUDE; |
} |
public void release() |
{ |
super.release(); |
id = null; |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/ResizeableDynaValidatorForm.java |
---|
0,0 → 1,91 |
package ak.strutsx; |
import java.util.Iterator; |
import java.util.TreeSet; |
import java.lang.reflect.Array; |
import javax.servlet.http.HttpServletRequest; |
import org.apache.struts.validator.DynaValidatorForm; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.config.FormBeanConfig; |
import org.apache.struts.config.FormPropertyConfig; |
public final class ResizeableDynaValidatorForm |
extends DynaValidatorForm |
{ |
public void reset(ActionMapping mapping, HttpServletRequest request) |
{ |
super.reset(mapping, request); |
resize(mapping, request); |
} |
protected void resize(ActionMapping mapping, HttpServletRequest request) |
{ |
String name = mapping.getName(); |
if (name == null) return; |
FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(name); |
if (config == null) return; |
FormPropertyConfig props[] = config.findFormPropertyConfigs(); |
for (int i = 0; i < props.length; i++) { |
if(props[i].getSize() > 0) // for arrays only |
resize(mapping, request, props[i]); |
} |
} |
protected void resize(ActionMapping mapping, HttpServletRequest request, |
FormPropertyConfig prop) |
{ |
String nameStart = prop.getName() + "["; |
// get all indices |
TreeSet indices = new TreeSet(); |
for(Iterator i = request.getParameterMap().keySet().iterator(); i.hasNext(); ) { |
String name = (String)i.next(); |
if(name.startsWith(nameStart)) { |
int p = name.indexOf("]"); |
if(p > 0) { |
String index = name.substring(nameStart.length(), p); |
try { |
indices.add(new Integer(index)); |
} |
catch(NumberFormatException ex) { |
} |
} |
} |
} |
// find last index in sequence |
int lastIndex = -1; |
for(Iterator i = indices.iterator(); i.hasNext(); ) { |
Integer idx = (Integer)i.next(); |
if(idx.intValue() == lastIndex+1) |
lastIndex++; |
else |
break; |
} |
// grow |
if(lastIndex > 0) { |
Class clazz = prop.getTypeClass(); |
Object initialValue = Array.newInstance(clazz.getComponentType(), lastIndex+1); |
for (int i = 0; i < lastIndex+1; i++) { |
try { |
Array.set(initialValue, i, clazz.getComponentType().newInstance()); |
} |
catch (Throwable t) { |
; // Probably does not have a zero-args constructor |
} |
} |
set(prop.getName(), initialValue); |
} |
} |
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) |
{ |
ActionErrors errors = super.validate(mapping, request); |
return errors; |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/ErrorHandlerX.java |
---|
0,0 → 1,23 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.action.ActionForm; |
public interface ErrorHandlerX { |
public void handleErrors(ActionMapping mapping, |
ActionForm form, |
HttpServletRequest request, |
HttpServletResponse response) |
throws Exception; |
} |
/hostadmiral/trunk/src/ak/strutsx/RequestProcessorX.java |
---|
0,0 → 1,231 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx; |
import java.io.IOException; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
import org.apache.struts.Globals; |
import org.apache.struts.action.Action; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.action.ActionForward; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.action.ActionForm; |
import org.apache.struts.action.RequestProcessor; |
import org.apache.struts.config.ForwardConfig; |
/** |
* To use this processor add |
* <controller processorClass="ak.strutsx.RequestProcessorX" /> |
* into the struts-config.xml file. |
*/ |
public class RequestProcessorX |
extends RequestProcessor |
{ |
/** |
* <p>Process an <code>HttpServletRequest</code> and create the |
* corresponding <code>HttpServletResponse</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 |
* @exception ServletException if a processing exception occurs |
*/ |
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; |
} |
if (log.isDebugEnabled()) { |
log.debug("Processing a '" + request.getMethod() + |
"' for path '" + path + "'"); |
} |
// 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; |
} |
// 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; |
} |
// Create or acquire the Action instance to process this request |
// Do this here, because we need the action in case form validation is failed |
Action action = null; |
if (mapping.getType() != null) { |
action = processActionCreate(request, response, mapping); |
} |
// Process any ActionForm bean related to this request |
ActionForm form = processActionForm(request, response, mapping); |
processPopulate(request, response, form, mapping); |
if (!processValidate(request, response, action, form, mapping)) { |
return; |
} |
// Process a forward or include specified by this mapping |
if (!processForward(request, response, mapping)) { |
return; |
} |
if (!processInclude(request, response, mapping)) { |
return; |
} |
// no action |
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); |
} |
/** |
* <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 back to the input form 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 action The Action instance we are populating |
* @param form The ActionForm instance we are populating |
* @param mapping The ActionMapping we are using |
* |
* @exception IOException if an input/output error occurs |
* @exception ServletException if a servlet exception occurs |
*/ |
protected boolean processValidate(HttpServletRequest request, |
HttpServletResponse response, |
Action action, |
ActionForm form, |
ActionMapping mapping) |
throws IOException, ServletException { |
if (form == null) { |
return (true); |
} |
// Was this request cancelled? |
if (request.getAttribute(Globals.CANCEL_KEY) != null) { |
if (log.isDebugEnabled()) { |
log.debug(" Cancelled transaction, skipping validation"); |
} |
return (true); |
} |
// Has validation been turned off for this mapping? |
if (!mapping.getValidate()) { |
return (true); |
} |
// Call the form bean's validation method |
if (log.isDebugEnabled()) { |
log.debug(" Validating input form properties"); |
} |
ActionErrors errors = form.validate(mapping, request); |
if ((errors == null) || errors.isEmpty()) { |
if (log.isTraceEnabled()) { |
log.trace(" No errors detected, accepting input"); |
} |
return (true); |
} |
// Form validation failed, try to inform the action |
if((action != null) && (action instanceof ErrorHandlerX)) { |
processValidationFailed(request, response, |
(ErrorHandlerX)action, form, mapping); |
} |
// Special handling for multipart request |
if (form.getMultipartRequestHandler() != null) { |
if (log.isTraceEnabled()) { |
log.trace(" Rolling back multipart request"); |
} |
form.getMultipartRequestHandler().rollback(); |
} |
// Has an input form been 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); |
} |
protected void |
processValidationFailed(HttpServletRequest request, |
HttpServletResponse response, |
ErrorHandlerX errorHandler, |
ActionForm form, |
ActionMapping mapping) |
throws IOException, ServletException { |
try { |
errorHandler.handleErrors(mapping, form, request, response); |
} catch (Exception e) { |
processException(request, response, e, form, mapping); |
} |
} |
} |
/hostadmiral/trunk/src/ak/strutsx/RequestUtilsX.java |
---|
0,0 → 1,107 |
/** |
* Extends and based on Apache Struts source code. |
* |
* Copyleft Anatoli Klassen (anatoli@aksoft.net) |
*/ |
package ak.strutsx; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServletRequest; |
import org.apache.struts.Globals; |
import org.apache.struts.action.Action; |
import org.apache.struts.action.ActionForm; |
import org.apache.struts.action.ActionServlet; |
import org.apache.struts.action.DynaActionForm; |
import org.apache.struts.action.DynaActionFormClass; |
import org.apache.struts.config.ModuleConfig; |
import org.apache.struts.config.FormBeanConfig; |
import org.apache.struts.config.FormPropertyConfig; |
import org.apache.struts.util.RequestUtils; |
public class RequestUtilsX |
extends RequestUtils |
{ |
public static ActionForm populateActionForm( |
Action action, |
HttpServletRequest request, |
String name) |
throws ServletException { |
ActionForm form = createActionForm(action, request, name); |
RequestUtils.populate(form, request); |
request.setAttribute(name, form); |
return form; |
} |
public static ActionForm createActionForm( |
Action action, |
HttpServletRequest request, |
String name) { |
return createActionForm(name, |
(ModuleConfig)request.getAttribute(Globals.MODULE_KEY), action.getServlet()); |
} |
public static ActionForm createActionForm( |
String name, |
ModuleConfig moduleConfig, |
ActionServlet servlet) { |
FormBeanConfig config = moduleConfig.findFormBeanConfig(name); |
ActionForm instance = null; |
// Create and return a new form bean instance |
if (config.getDynamic()) { |
try { |
DynaActionFormClass dynaClass = |
DynaActionFormClass.createDynaActionFormClass(config); |
instance = (ActionForm) dynaClass.newInstance(); |
initializeDynaActionForm((DynaActionForm) instance, config); |
if (log.isDebugEnabled()) { |
log.debug( |
" Creating new DynaActionForm instance " |
+ "of type '" |
+ config.getType() |
+ "'"); |
log.trace(" --> " + instance); |
} |
} catch (Throwable t) { |
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t); |
return (null); |
} |
} else { |
try { |
instance = (ActionForm) applicationInstance(config.getType()); |
if (log.isDebugEnabled()) { |
log.debug( |
" Creating new ActionForm instance " |
+ "of type '" |
+ config.getType() |
+ "'"); |
log.trace(" --> " + instance); |
} |
} catch (Throwable t) { |
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t); |
return (null); |
} |
} |
instance.setServlet(servlet); |
return (instance); |
} |
public static void initializeDynaActionForm(DynaActionForm instance, FormBeanConfig config) { |
if (config == null) { |
return; |
} |
FormPropertyConfig props[] = config.findFormPropertyConfigs(); |
for (int i = 0; i < props.length; i++) { |
instance.set(props[i].getName(), props[i].initial()); |
} |
} |
} |