001: /*
002: * $Id: HeaderTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.jsp.JspException;
028: import javax.servlet.jsp.tagext.TagSupport;
029:
030: import java.util.ArrayList;
031: import java.util.Enumeration;
032:
033: /**
034: * Define a scripting variable based on the value(s) of the specified header
035: * received with this request.
036: *
037: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
038: * $
039: */
040: public class HeaderTag extends TagSupport {
041: /**
042: * The message resources for this package.
043: */
044: protected static MessageResources messages = MessageResources
045: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
046:
047: // ------------------------------------------------------------- Properties
048:
049: /**
050: * The name of the scripting variable that will be exposed as a page scope
051: * attribute.
052: */
053: protected String id = null;
054:
055: /**
056: * Return an array of header values if <code>multiple</code> is non-null.
057: */
058: protected String multiple = null;
059:
060: /**
061: * The name of the header whose value is to be exposed.
062: */
063: protected String name = null;
064:
065: /**
066: * The default value to return if no header of the specified name is
067: * found.
068: */
069: protected String value = null;
070:
071: public String getId() {
072: return (this .id);
073: }
074:
075: public void setId(String id) {
076: this .id = id;
077: }
078:
079: public String getMultiple() {
080: return (this .multiple);
081: }
082:
083: public void setMultiple(String multiple) {
084: this .multiple = multiple;
085: }
086:
087: public String getName() {
088: return (this .name);
089: }
090:
091: public void setName(String name) {
092: this .name = name;
093: }
094:
095: public String getValue() {
096: return (this .value);
097: }
098:
099: public void setValue(String value) {
100: this .value = value;
101: }
102:
103: // --------------------------------------------------------- Public Methods
104:
105: /**
106: * Retrieve the required property and expose it as a scripting variable.
107: *
108: * @throws JspException if a JSP exception has occurred
109: */
110: public int doStartTag() throws JspException {
111: if (this .multiple == null) {
112: this .handleSingleHeader();
113: } else {
114: this .handleMultipleHeaders();
115: }
116:
117: return SKIP_BODY;
118: }
119:
120: /**
121: * Expose an array of header values.
122: *
123: * @throws JspException
124: * @since Struts 1.2
125: */
126: protected void handleMultipleHeaders() throws JspException {
127: ArrayList values = new ArrayList();
128: Enumeration items = ((HttpServletRequest) pageContext
129: .getRequest()).getHeaders(name);
130:
131: while (items.hasMoreElements()) {
132: values.add(items.nextElement());
133: }
134:
135: if (values.isEmpty() && (this .value != null)) {
136: values.add(this .value);
137: }
138:
139: String[] headers = new String[values.size()];
140:
141: if (headers.length == 0) {
142: JspException e = new JspException(messages.getMessage(
143: "header.get", name));
144:
145: TagUtils.getInstance().saveException(pageContext, e);
146: throw e;
147: }
148:
149: pageContext.setAttribute(id, values.toArray(headers));
150: }
151:
152: /**
153: * Expose a single header value.
154: *
155: * @throws JspException
156: * @since Struts 1.2
157: */
158: protected void handleSingleHeader() throws JspException {
159: String value = ((HttpServletRequest) pageContext.getRequest())
160: .getHeader(name);
161:
162: if ((value == null) && (this .value != null)) {
163: value = this .value;
164: }
165:
166: if (value == null) {
167: JspException e = new JspException(messages.getMessage(
168: "header.get", name));
169:
170: TagUtils.getInstance().saveException(pageContext, e);
171: throw e;
172: }
173:
174: pageContext.setAttribute(id, value);
175: }
176:
177: /**
178: * Release all allocated resources.
179: */
180: public void release() {
181: super.release();
182: id = null;
183: multiple = null;
184: name = null;
185: value = null;
186: }
187: }
|