KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > logic > SecureTag


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 JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import javax.servlet.jsp.JspTagException JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12 import javax.servlet.jsp.SkipPageException JavaDoc;
13 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * This tag library is designed to be used on a JSP
20  * to switch HTTP -> HTTPS protocols and vice versa.
21  * <p>If you want to force the page to be viewed in SSL,
22  * then you would do something like this:<br /><br />
23  * <pre>
24  * &lt;tag:secure /&gt;
25  * or
26  * &lt;tag:secure mode="secured" /&gt;
27  * </pre>
28  * If you want the force the page to be viewed in
29  * over standard http, then you would do something like:<br />
30  * <pre>
31  * &lt;tag:secure mode="unsecured" /&gt;
32  * </pre>
33  *
34  * @author <a HREF="mailto:jon.lipsky@xesoft.com">Jon Lipsky</a>
35  * <p>Contributed by:
36  * <p>XEsoft GmbH
37  * Oskar-Messter-Strasse 18
38  * 85737 Ismaning, Germany
39  * http://www.xesoft.com
40  * @jsp.tag name="secure"
41  * body-content="empty"
42  */

43 public class SecureTag extends SimpleTagSupport JavaDoc {
44     //~ Static fields/initializers =============================================
45

46     public static final String JavaDoc MODE_SECURED = "secured";
47     public static final String JavaDoc MODE_UNSECURED = "unsecured";
48     public static final String JavaDoc MODE_EITHER = "either";
49
50     //~ Instance fields ========================================================
51

52     protected transient final Log log = LogFactory.getLog(SecureTag.class);
53     protected String JavaDoc TAG_NAME = "Secure";
54     private String JavaDoc mode = MODE_SECURED;
55     private String JavaDoc httpPort = null;
56     private String JavaDoc httpsPort = null;
57
58     //~ Methods ================================================================
59

60     /**
61      * Sets the mode attribute. This is included in the tld file.
62      *
63      * @jsp.attribute description="The mode attribute (secured | unsecured)"
64      * required="false"
65      * rtexprvalue="true"
66      */

67     public void setMode(String JavaDoc aMode) {
68         mode = aMode;
69     }
70
71     /**
72      * Processes the tag
73      *
74      * @throws JspTagException
75      */

76     public void doTag() throws JspTagException JavaDoc {
77
78         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
79
80         // get the port numbers from the application context
81
Map JavaDoc config =
82                 (HashMap JavaDoc) pageContext.getServletContext().getAttribute(Constants.CONFIG);
83
84         httpPort = (String JavaDoc) config.get(Constants.HTTP_PORT);
85
86         if ( httpPort == null ) {
87             httpPort = SslUtil.STD_HTTP_PORT;
88         }
89
90         httpsPort = (String JavaDoc) 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 JavaDoc vQueryString =
99                         ((HttpServletRequest JavaDoc) pageContext.getRequest()).getQueryString();
100                 String JavaDoc vPageUrl =
101                         ((HttpServletRequest JavaDoc) pageContext.getRequest()).getRequestURI();
102                 String JavaDoc vServer = pageContext.getRequest().getServerName();
103
104                 StringBuffer JavaDoc vRedirect = new StringBuffer JavaDoc("");
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 JavaDoc) pageContext.getResponse()).sendRedirect(vRedirect.toString());
119                     throw new SkipPageException JavaDoc();
120                 } catch ( Exception JavaDoc exc2 ) {
121                     throw new JspTagException JavaDoc(exc2.getMessage());
122                 }
123             }
124         } else if ( mode.equalsIgnoreCase(MODE_UNSECURED) ) {
125             if ( pageContext.getRequest().isSecure() == true ) {
126                 String JavaDoc vQueryString =
127                         ((HttpServletRequest JavaDoc) pageContext.getRequest()).getQueryString();
128                 String JavaDoc vPageUrl =
129                         ((HttpServletRequest JavaDoc) pageContext.getRequest()).getRequestURI();
130                 String JavaDoc vServer = pageContext.getRequest().getServerName();
131
132                 StringBuffer JavaDoc vRedirect = new StringBuffer JavaDoc("");
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 JavaDoc) pageContext.getResponse()).sendRedirect(vRedirect.toString());
143                     throw new SkipPageException JavaDoc();
144                 } catch ( Exception JavaDoc exc2 ) {
145                     throw new JspTagException JavaDoc(exc2.getMessage());
146                 }
147             }
148         } else if ( !mode.equalsIgnoreCase(MODE_EITHER) ) {
149             throw new JspTagException JavaDoc("Illegal value for the attribute mode: " +
150                     mode);
151         }
152     }
153
154 }
155
Popular Tags