1 package com.blandware.atleap.webapp.taglib.core.logic; 2 3 import com.blandware.atleap.common.Constants; 4 import com.blandware.atleap.webapp.util.core.SslUtil; 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import javax.servlet.jsp.JspTagException ; 11 import javax.servlet.jsp.PageContext ; 12 import javax.servlet.jsp.SkipPageException ; 13 import javax.servlet.jsp.tagext.SimpleTagSupport ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 18 43 public class SecureTag extends SimpleTagSupport { 44 46 public static final String MODE_SECURED = "secured"; 47 public static final String MODE_UNSECURED = "unsecured"; 48 public static final String MODE_EITHER = "either"; 49 50 52 protected transient final Log log = LogFactory.getLog(SecureTag.class); 53 protected String TAG_NAME = "Secure"; 54 private String mode = MODE_SECURED; 55 private String httpPort = null; 56 private String httpsPort = null; 57 58 60 67 public void setMode(String aMode) { 68 mode = aMode; 69 } 70 71 76 public void doTag() throws JspTagException { 77 78 PageContext pageContext = (PageContext ) getJspContext(); 79 80 Map config = 82 (HashMap ) pageContext.getServletContext().getAttribute(Constants.CONFIG); 83 84 httpPort = (String ) config.get(Constants.HTTP_PORT); 85 86 if ( httpPort == null ) { 87 httpPort = SslUtil.STD_HTTP_PORT; 88 } 89 90 httpsPort = (String ) config.get(Constants.HTTPS_PORT); 91 92 if ( httpsPort == null ) { 93 httpsPort = SslUtil.STD_HTTPS_PORT; 94 } 95 96 if ( mode.equalsIgnoreCase(MODE_SECURED) ) { 97 if ( pageContext.getRequest().isSecure() == false ) { 98 String vQueryString = 99 ((HttpServletRequest ) pageContext.getRequest()).getQueryString(); 100 String vPageUrl = 101 ((HttpServletRequest ) pageContext.getRequest()).getRequestURI(); 102 String vServer = pageContext.getRequest().getServerName(); 103 104 StringBuffer vRedirect = new StringBuffer (""); 105 vRedirect.append("https://"); 106 vRedirect.append(vServer + ":" + httpsPort + vPageUrl); 107 108 if ( vQueryString != null ) { 109 vRedirect.append("?"); 110 vRedirect.append(vQueryString); 111 } 112 113 if ( log.isDebugEnabled() ) { 114 log.debug("attempting to redirect to: " + vRedirect); 115 } 116 117 try { 118 ((HttpServletResponse ) pageContext.getResponse()).sendRedirect(vRedirect.toString()); 119 throw new SkipPageException (); 120 } catch ( Exception exc2 ) { 121 throw new JspTagException (exc2.getMessage()); 122 } 123 } 124 } else if ( mode.equalsIgnoreCase(MODE_UNSECURED) ) { 125 if ( pageContext.getRequest().isSecure() == true ) { 126 String vQueryString = 127 ((HttpServletRequest ) pageContext.getRequest()).getQueryString(); 128 String vPageUrl = 129 ((HttpServletRequest ) pageContext.getRequest()).getRequestURI(); 130 String vServer = pageContext.getRequest().getServerName(); 131 132 StringBuffer vRedirect = new StringBuffer (""); 133 vRedirect.append("http://"); 134 vRedirect.append(vServer + vPageUrl); 135 136 if ( vQueryString != null ) { 137 vRedirect.append("?"); 138 vRedirect.append(vQueryString); 139 } 140 141 try { 142 ((HttpServletResponse ) pageContext.getResponse()).sendRedirect(vRedirect.toString()); 143 throw new SkipPageException (); 144 } catch ( Exception exc2 ) { 145 throw new JspTagException (exc2.getMessage()); 146 } 147 } 148 } else if ( !mode.equalsIgnoreCase(MODE_EITHER) ) { 149 throw new JspTagException ("Illegal value for the attribute mode: " + 150 mode); 151 } 152 } 153 154 } 155 | Popular Tags |