CHAPTER 7 Building a tag library for sending email validate them during the translation phase. Chapter 9 discusses implementing such tag-contained action relationships, wherein a tag may validate the properties of some internal condition operation with ease. For now, we will hold off on providing a fully extensible and generic assertion tag and not use Class.forName() to instantiate a set of configured assertion objects. Validating user input While implementing AssertTag, we need to also consider validating the attribute values provided by the user. The user should provide attribute values to be used as condition parameters to the assertions, and it is clear that not all possible values are accepted. Hence, we should validate them during the translation time. To facilitate, AssertExtraInfo was created to accompany AssertTag; this class validates the conditions posed by the user and ensures that their provided values are correct. Listing 7.19 Source code for AssertExtraInfo package book.assert; import javax.servlet.jsp.tagext.TagData; import javax.servlet.jsp.tagext.TagExtraInfo; public class AssertExtraInfo extends TagExtraInfo { public boolean isValid(TagData data) { String cond = b (String)data.getAttribute(AssertTag.EXISTS_TAG); if((null != cond) && !AssertTag.existsAssertion.isValidCondition(cond)) { return false; } cond = (String)data.getAttribute(AssertTag.TYPE_TAG); if((null != cond) && !AssertTag.typeAssertion.isValidCondition(cond)) { return false; } cond = (String)data.getAttribute(AssertTag.ONEOF_TAG); if((null != cond) && !AssertTag.oneofAssertion.isValidCondition(cond)) { return false; } return true; } }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
Adding assertions and input validation public boolean isValidCondition(String condition) { return condition.equals(”true”) || condition.equals(”false”); } } static class TypeAssertion implements Assertion { // Implementation omitted for clarity } static class OneOfAssertion implements Assertion { // Implementation omitted for clarity } } B stringUtil is a utility class with string-related methods. c Gets the named parameter. d Runs the assertions The exists assertion is always applied. e If an assertion fails, we abort the page execution. f Adds a request attribute with the name of the failed parameter. g Forwards the request to the error handler. h Runs the superclass so that the cleanup logic will run. i Sample implemenation of an assertion object All the assertion objects in this listing are implemented as inner classes of the AssertTag handler. We did not have to do it this way, but the typical assertion object was small and was used only within the tag handler. The tag handler implements common operations, such as property setters, for all the different conditions that we may pose (and the values are stored internally within the tag). During the later service execution, any non-null condition will trigger an assertion whose failure triggers the execution of the error handler. One can argue that AssertTag implementation is not as generic as it could be. For example, we could instantiate all assertion objects dynamically using Class.for- Name(), thereby removing the hard-coded dependency between the tag handler and the different assertions. There is some truth in this claim; however, there is a problem in implementing this approach, namely implementing the property setters and validation for each condition. As you saw in listing 7.17, each assertion object accepts two arguments: the value to assert on and a condition parameter. The handler tag implementation should know in advance all the different assertions, so that it will be able to receive the properties via its property setters when running, and
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
Adding assertions and input validation protected boolean isAsserted = false; protected String parameterName = null; protected String handlerUri = null; protected String type = null; protected String exists = null; protected String oneof = null; public void setParameter(String parameterName) { this.parameterName = parameterName; } public void setHandler(String handlerUri) { this.handlerUri = handlerUri; } public void setType(String type) { this.type = StringUtil.trimit(type); } public void setExists(String exists) { this.exists = StringUtil.trimit(exists); } public void setOneof(String oneof) { this.oneof = StringUtil.trimit(oneof); } public int doStartTag() throws JspException { ServletRequest req = pageContext.getRequest(); String value = req.getParameter(parameterName); c if(null == exists) { exists = “true”; } if(!existsAssertion.assert(value, exists)) { d isAsserted = true; } else if((null != type) && !typeAssertion.assert(value, type)) { isAsserted = true; } else if((null != oneof) && !oneofAssertion.assert(value, oneof)) { isAsserted = true; } return SKIP_BODY; }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
CHAPTER 7 Building a tag library for sending email public int doEndTag() throws JspException { int rc = isAsserted ? SKIP_PAGE : EVAL_PAGE; e if(isAsserted) { pageContext.setAttribute(GUILTY_VAR_TAG, f parameterName, PageContext.REQUEST_SCOPE); ServletRequest req = pageContext.getRequest() g RequestDispatcher rd = req.getRequestDispatcher(handlerUri); try { rd.forward(pageContext.getRequest(), pageContext.getResponse()); } catch(Throwable t) { // Log and throw an exception } } super.doEndTag(); h return rc; } protected void clearServiceState() { isAsserted = false; super.clearServiceState(); } protected void clearProperties() { parameterName = null; handlerUri = null; type = null; exists = null; oneof = null; super.clearProperties(); } static class ExistsAssertion implements Assertion i { public boolean assert(String value, String condition) { boolean exists = new Boolean(condition).booleanValue(); if(exists && null != value) { return true; } return false; }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
CHAPTER 7 Building a tag library for sending email The assertion developer should implement two methods: assert() accepts a value and a condition parameter and performs the assertion logic. It should return true if successful, and false otherwise. For example, a type assertion will get a type specification as the condition parameter and will have to check whether the value is of the given type. AssertTag should use this method while the page is executed. isValidCondition() verifies whether its input is a valid assertion parameter. For example, in a type assertion the method should check if the condition parameter is one of the types known to the assertion. This method should be used by the TagExtraInfo to be implemented for AssertTag. Creating AssertTag Now that the assertion logic is implemented by different assertion objects, AssertTag (listing 7.18) is more concerned with instantiating the assertion objects, accepting the conditions posed on the assertion, and calling the correct assertion with the correct condition. When the condition fails, the tag takes the failed parameter s name, adds it to the request attributes, and forwards the request to the required handler. Listing 7.18 Source code for AssertTag package book.assert; import javax.servlet.RequestDispatcher; import javax.servlet.ServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.mail.internet.InternetAddress; import book.util.LocalStrings; import book.util.StringUtil; b import book.util.ExTagSupport; public class AssertTag extends ExTagSupport { public static final String GUILTY_VAR_TAG = “guilty_variable”; public static final String TYPE_TAG = “type”; public static final String EXISTS_TAG = “exists”; public static final String ONEOF_TAG = “oneof”; // Additional static final objects where omitted for // clarity. static Assertion typeAssertion = new TypeAssertion(); static Assertion existsAssertion = new ExistsAssertion(); static Assertion oneofAssertion = new OneOfAssertion();
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
Adding assertions and input validation implement a brand new language for defining conditions, and this is something we d like to avoid. Luckily, most cases require only a relatively simple condition, such as the one presented in our email assertion sample, so assertions will satisfy our need for declarative parameter checking. Having chosen a model for parameter verification, we ask ourselves what conditions we might ever pose over incoming parameters: Existence conditions To check if a certain parameter is available in the input parameters. Type conditions The parameter may be of type number, alphanumeric string, alpha-only string, email address, and so forth. One of condition We may want the parameter to be one of the items in a list. Interval condition We may want the parameter to be in a certain range. Equality condition We may want the parameter to be of a certain value. 7.5.2 Creating a tag for the send mail tag library There is no end to the conditions we may want to pose; hence, one important characteristic of the assert tag should be its capacity to be extended by adding new assertion conditions. To meet this requirement, the following interface and tag were developed. The Assertion interface The cornerstone to assert tag is the Assertion interface (listing 7.17) that defines what is to be implemented by the developer in adding new assertion logic to our tag. Listing 7.17 Source code for the Assertion interface package book.assert; public interface Assertion { public boolean assert(String value, String condition); public boolean isValidCondition(String condition); }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
CHAPTER 7 Building a tag library for sending email world, of course, we cannot make such assumptions. The reason is a well-known law of development: If users can break it, they will. 7.5.1 Performing validation using custom tags Validating input parameters is by no means limited to a specific registration application. Input validation has a broad range and yet, our application won t be complete without it, so let s check how validation might be implemented with custom tags. If tags should be declarative, it would then be a design mistake to define a set of tags that lets the user program validation into the page. Instead, we want to look for a declarative and reusable approach that allows a relatively na ve user to specify limitations over the incoming parameters, and an action to take in case said limitation is not met. Fortunately, there is such an approach and it is in widespread use assertions. Using assertions An assertion is a programming element with a condition and action which is sometimes implicit. When working with an assertion, the developer can specify its condition and, if this condition is not met, the assertion s action takes place (a failed assertion may trigger a program exit). We might, for example, use an assertion to check whether a particular HTTP form parameter is a number, or to check whether a form field has been left empty by the user. We can use assertions in our page to specify a condition or set of conditions on our parameters, and the action can be to forward the request to an assertion failed handler URI. Specifying conditions using assertions A possible syntax is provided in the following JSP fragment: The conditions specified in the assertion may range from checking the existence of the parameter to a condition on the parameter value or its type. For example, one could assert on our email address parameter in the following way: We can imagine some complex conditions that would not work with this approach to evaluating assertions. Supporting increasingly complex assertions will force us to
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Adding assertions and input validation < % reg.register(); %>
Dear , you were registered successfully.
We are about to email you your user name and password.
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
Blogroll:
Blogroll:
Powered by Java Web Hosting