1 18 19 package org.apache.struts.taglib.bean; 20 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 import org.apache.struts.util.MessageResources; 29 import org.apache.struts.taglib.TagUtils; 30 31 37 public class HeaderTag extends TagSupport { 38 39 41 45 protected String id = null; 46 47 public String getId() { 48 return (this.id); 49 } 50 51 public void setId(String id) { 52 this.id = id; 53 } 54 55 58 protected static MessageResources messages = 59 MessageResources.getMessageResources( 60 "org.apache.struts.taglib.bean.LocalStrings"); 61 62 65 protected String multiple = null; 66 67 public String getMultiple() { 68 return (this.multiple); 69 } 70 71 public void setMultiple(String multiple) { 72 this.multiple = multiple; 73 } 74 75 78 protected String name = null; 79 80 public String getName() { 81 return (this.name); 82 } 83 84 public void setName(String name) { 85 this.name = name; 86 } 87 88 91 protected String value = null; 92 93 public String getValue() { 94 return (this.value); 95 } 96 97 public void setValue(String value) { 98 this.value = value; 99 } 100 101 103 108 public int doStartTag() throws JspException { 109 110 if (this.multiple == null) { 111 this.handleSingleHeader(); 112 } else { 113 this.handleMultipleHeaders(); 114 } 115 116 return SKIP_BODY; 117 } 118 119 124 protected void handleMultipleHeaders() throws JspException { 125 ArrayList values = new ArrayList (); 126 Enumeration items = 127 ((HttpServletRequest ) pageContext.getRequest()).getHeaders(name); 128 129 while (items.hasMoreElements()){ 130 values.add(items.nextElement()); 131 } 132 133 if (values.isEmpty() && (this.value != null)){ 134 values.add(this.value); 135 } 136 137 String headers[] = new String [values.size()]; 138 if (headers.length == 0) { 139 JspException e = 140 new JspException (messages.getMessage("header.get", name)); 141 TagUtils.getInstance().saveException(pageContext, e); 142 throw e; 143 } 144 145 pageContext.setAttribute(id, values.toArray(headers)); 146 } 147 148 153 protected void handleSingleHeader() throws JspException { 154 String value = 155 ((HttpServletRequest ) pageContext.getRequest()).getHeader(name); 156 157 if ((value == null) && (this.value != null)) { 158 value = this.value; 159 } 160 161 if (value == null) { 162 JspException e = 163 new JspException (messages.getMessage("header.get", name)); 164 TagUtils.getInstance().saveException(pageContext, e); 165 throw e; 166 } 167 168 pageContext.setAttribute(id, value); 169 } 170 171 174 public void release() { 175 176 super.release(); 177 id = null; 178 multiple = null; 179 name = null; 180 value = null; 181 182 } 183 184 } 185 | Popular Tags |