From: <mr...@us...> - 2003-01-08 05:54:23
|
Update of /cvsroot/struts/struts-resume/src/web/org/appfuse/webapp/taglib In directory sc8-pr-cvs1:/tmp/cvs-serv30030 Added Files: LabelTag.java Secure.java Log Message: --- NEW FILE: LabelTag.java --- package org.appfuse.webapp.taglib; import java.util.Iterator; import java.util.Locale; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.validator.Field; import org.apache.commons.validator.Form; import org.apache.commons.validator.ValidatorResources; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; import org.apache.struts.validator.ValidatorPlugIn; /** * <p>This class is designed to render a <label> tag for labeling your forms and * adds an asterik (*) for required fields. It was originally written by Erik * Hatcher (http://www.ehatchersolutions.com/JavaDevWithAnt/).</p> * * <p>It is designed to be used as follows: * <pre><appfuse:label key="userForm.username" /></pre> * </p> * * @jsp.tag name="label" bodycontent="empty" */ public class LabelTag extends TagSupport { protected String key = null; protected String styleClass = null; protected String errorClass = null; public int doStartTag() throws JspException { // Look up this key to see if its a field of the current form boolean requiredField = false; boolean validationError = false; ValidatorResources resources = (ValidatorResources) pageContext.getServletContext().getAttribute( ValidatorPlugIn.VALIDATOR_KEY); ActionMapping mapping = (ActionMapping) pageContext.getAttribute( Globals.MAPPING_KEY, PageContext.REQUEST_SCOPE); Locale locale = (Locale) pageContext.getAttribute( Globals.LOCALE_KEY, PageContext.SESSION_SCOPE); if (locale == null) { locale = Locale.getDefault(); } String formName = mapping.getAttribute(); System.out.println("formName: " + formName); String fieldName = key.substring(formName.length() + 1); if (resources != null) { Form form = resources.get(locale, formName); if (form != null) { Field field = (Field) form.getFieldMap().get(fieldName); if (field != null) { if (field.isDependency("required")) { requiredField = true; } } } } ActionErrors errors = (ActionErrors) pageContext.getAttribute( Globals.ERROR_KEY, PageContext.REQUEST_SCOPE); if (errors != null) { Iterator errorIterator = errors.get(fieldName); if (errorIterator.hasNext()) { validationError = true; } } // Retrieve the message string we are looking for String message = RequestUtils.message( pageContext, Action.MESSAGES_KEY, Globals.LOCALE_KEY, key); if (message == null) { JspException e = new JspException("Cannot find message for key: " + key); RequestUtils.saveException(pageContext, e); throw e; } String cssClass = (styleClass != null) ? styleClass : "label"; String cssErrorClass = (errorClass != null) ? errorClass : "labelError"; if (message == null || "".equals(message.trim())) { message = ""; } else { message = "<label for=\"" + fieldName + "\" class=\"" + (validationError ? cssErrorClass : cssClass) + "\">" + message + (requiredField ? "*" : "") + ":</label>"; } // Print the retrieved message to our output writer ResponseUtils.write(pageContext, message); // Continue processing this page return (SKIP_BODY); } /** * @jsp.attribute required="true" */ public void setKey(String key) { this.key = key; } /** * Setter for assigning a CSS class, default is label. * * @jsp.attribute required="false" */ public void setStyleClass(String styleClass) { this.styleClass = styleClass; } /** * Setter for assigning a CSS class when errors occur, * defaults to labelError. * * @jsp.attribute required="false" */ public void setErrorClass(String errorClass) { this.errorClass = errorClass; } } --- NEW FILE: Secure.java --- package org.appfuse.webapp.taglib; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.appfuse.common.Constants; import org.appfuse.webapp.util.SslUtil; /** * This tag library is designed to be used on a JSP * to switch HTTP -> HTTPS protocols and vise versa. * * If you want to force the page to be viewed in SSL, * then you would do something like this:<br /><br /> * <pre> * <tag:secure /> * or * <tag:secure mode="secured" /> * </pre> * If you want the force the page to be viewed in * over standard http, then you would do something like:<br /> * <pre> * <tag:secure mode="unsecured" /> * </pre> * @jsp.tag name="secure" * bodycontent="empty" * * @author <a href="mailto:jon...@xe...">Jon Lipsky</a> * * Contributed by: * * XEsoft GmbH * Oskar-Messter-Strasse 18 * 85737 Ismaning, Germany * http://www.xesoft.com */ public class Secure extends BodyTagSupport { /** * The <code>Log</code> instance for this class. */ private Log log = LogFactory.getLog(Secure.class); public static String MODE_SECURED = "secured"; public static String MODE_UNSECURED = "unsecured"; public static String MODE_EITHER = "either"; protected String TAG_NAME = "Secure"; private String mode = MODE_SECURED; private String httpPort = null; private String httpsPort = null; /** * Sets the mode attribute. This is included in the tld file. * * @jsp.attribute * description="The mode attribute (secure | unsecured)" * required="false" * rtexprvalue="true" */ public void setMode(String aMode) { mode = aMode; } public int doStartTag() throws JspException { httpPort = (String)pageContext.getServletContext().getAttribute(Constants.HTTP_PORT); if (httpPort == null) { httpPort = SslUtil.STD_HTTP_PORT; } httpsPort = (String)pageContext.getServletContext().getAttribute(Constants.HTTPS_PORT); if (httpsPort == null) { httpsPort = SslUtil.STD_HTTPS_PORT; } return SKIP_BODY; } public int doAfterBody() throws JspException { return SKIP_BODY; } public int doEndTag() throws JspException { if (mode.equalsIgnoreCase(MODE_SECURED)) { if (pageContext.getRequest().isSecure() == false) { String vQueryString = ((HttpServletRequest) pageContext.getRequest()).getQueryString(); String vPageUrl = ((HttpServletRequest) pageContext.getRequest()).getRequestURI(); String vServer = pageContext.getRequest().getServerName(); StringBuffer vRedirect = new StringBuffer(""); vRedirect.append("https://"); vRedirect.append(vServer + ":" + httpsPort + vPageUrl); if (vQueryString != null) { vRedirect.append("?"); vRedirect.append(vQueryString); } if (log.isDebugEnabled()) { log.debug("attempting to redirect to: " + vRedirect); } try { ((HttpServletResponse) pageContext.getResponse()).sendRedirect(vRedirect.toString()); return SKIP_PAGE; } catch (Exception exc2) { throw new JspException(exc2.getMessage()); } } } else if (mode.equalsIgnoreCase(MODE_UNSECURED)) { if (pageContext.getRequest().isSecure() == true) { String vQueryString = ((HttpServletRequest) pageContext.getRequest()).getQueryString(); String vPageUrl = ((HttpServletRequest) pageContext.getRequest()).getRequestURI(); String vServer = pageContext.getRequest().getServerName(); StringBuffer vRedirect = new StringBuffer(""); vRedirect.append("http://"); vRedirect.append(vServer + vPageUrl); if (vQueryString != null) { vRedirect.append("?"); vRedirect.append(vQueryString); } try { ((HttpServletResponse) pageContext.getResponse()).sendRedirect(vRedirect.toString()); return SKIP_PAGE; } catch (Exception exc2) { throw new JspException(exc2.getMessage()); } } } else if (mode.equalsIgnoreCase(MODE_EITHER)) { return EVAL_PAGE; } else { throw new JspException("Illegal value for the attribute mode: " + mode); } return EVAL_PAGE; } } |