001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.fmt;
040:
041: import java.io.UnsupportedEncodingException;
042:
043: import javax.servlet.jsp.JspException;
044: import javax.servlet.jsp.JspTagException;
045: import javax.servlet.jsp.PageContext;
046: import javax.servlet.jsp.tagext.TagSupport;
047:
048: /**
049: * Support for tag handlers for <requestEncoding>, the tag for setting
050: * the request character encoding in JSTL 1.0.
051: *
052: * @author Jan Luehe
053: * @author Pierre Delisle
054: */
055:
056: public abstract class RequestEncodingSupport extends TagSupport {
057:
058: //*********************************************************************
059: // Package-scoped constants
060:
061: static final String REQUEST_CHAR_SET = "javax.servlet.jsp.jstl.fmt.request.charset";
062:
063: //*********************************************************************
064: // Private constants
065:
066: private static final String DEFAULT_ENCODING = "ISO-8859-1";
067:
068: //*********************************************************************
069: // Tag attributes
070:
071: protected String value; // 'value' attribute
072:
073: //*********************************************************************
074: // Derived information
075:
076: protected String charEncoding; // derived from 'value' attribute
077:
078: //*********************************************************************
079: // Constructor and initialization
080:
081: public RequestEncodingSupport() {
082: super ();
083: init();
084: }
085:
086: private void init() {
087: value = null;
088: }
089:
090: //*********************************************************************
091: // Tag logic
092:
093: public int doEndTag() throws JspException {
094: charEncoding = value;
095: if ((charEncoding == null)
096: && (pageContext.getRequest().getCharacterEncoding() == null)) {
097: // Use charset from session-scoped attribute
098: charEncoding = (String) pageContext.getAttribute(
099: REQUEST_CHAR_SET, PageContext.SESSION_SCOPE);
100: if (charEncoding == null) {
101: // Use default encoding
102: charEncoding = DEFAULT_ENCODING;
103: }
104: }
105:
106: /*
107: * If char encoding was already set in the request, we don't need to
108: * set it again.
109: */
110: if (charEncoding != null) {
111: try {
112: pageContext.getRequest().setCharacterEncoding(
113: charEncoding);
114: } catch (UnsupportedEncodingException uee) {
115: throw new JspTagException(uee.toString(), uee);
116: }
117: }
118:
119: return EVAL_PAGE;
120: }
121:
122: // Releases any resources we may have (or inherit)
123: public void release() {
124: init();
125: }
126: }
|