001: /*
002: * $Id: CookieTag.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.Cookie;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.tagext.TagSupport;
030:
031: import java.util.ArrayList;
032:
033: /**
034: * Define a scripting variable based on the value(s) of the specified cookie
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 CookieTag 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 Cookies if <code>multiple</code> is non-null.
057: */
058: protected String multiple = null;
059:
060: /**
061: * The name of the cookie whose value is to be exposed.
062: */
063: protected String name = null;
064:
065: /**
066: * The default value to return if no cookie 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: // Retrieve the required cookie value(s)
112: ArrayList values = new ArrayList();
113: Cookie[] cookies = ((HttpServletRequest) pageContext
114: .getRequest()).getCookies();
115:
116: if (cookies == null) {
117: cookies = new Cookie[0];
118: }
119:
120: for (int i = 0; i < cookies.length; i++) {
121: if (name.equals(cookies[i].getName())) {
122: values.add(cookies[i]);
123: }
124: }
125:
126: if ((values.size() < 1) && (value != null)) {
127: values.add(new Cookie(name, value));
128: }
129:
130: if (values.size() < 1) {
131: JspException e = new JspException(messages.getMessage(
132: "cookie.get", name));
133:
134: TagUtils.getInstance().saveException(pageContext, e);
135: throw e;
136: }
137:
138: // Expose an appropriate variable containing these results
139: if (multiple == null) {
140: Cookie cookie = (Cookie) values.get(0);
141:
142: pageContext.setAttribute(id, cookie);
143: } else {
144: cookies = new Cookie[values.size()];
145: pageContext.setAttribute(id, values.toArray(cookies));
146: }
147:
148: return (SKIP_BODY);
149: }
150:
151: /**
152: * Release all allocated resources.
153: */
154: public void release() {
155: super.release();
156: id = null;
157: multiple = null;
158: name = null;
159: value = null;
160: }
161: }
|