1 18 19 20 package org.apache.struts.webapp.example; 21 22 23 import java.io.IOException ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspWriter ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.taglib.TagUtils; 33 import org.apache.struts.util.MessageResources; 34 35 36 42 43 public class LinkSubscriptionTag extends TagSupport { 44 45 46 48 49 52 protected String page = null; 53 54 55 58 protected static MessageResources messages = 59 MessageResources.getMessageResources 60 ("org.apache.struts.webapp.example.ApplicationResources"); 61 62 63 66 private String name = "subscription"; 67 68 69 71 72 75 public String getPage() { 76 77 return (this.page); 78 79 } 80 81 82 87 public void setPage(String page) { 88 89 this.page = page; 90 91 } 92 93 94 97 public String getName() { 98 99 return (this.name); 100 101 } 102 103 104 109 public void setName(String name) { 110 111 this.name = name; 112 113 } 114 115 116 118 119 124 public int doStartTag() throws JspException { 125 126 ModuleConfig config = (ModuleConfig) pageContext.getRequest() 128 .getAttribute(org.apache.struts.Globals.MODULE_KEY); 129 HttpServletRequest request = 130 (HttpServletRequest ) pageContext.getRequest(); 131 StringBuffer url = new StringBuffer (request.getContextPath()); 132 url.append(config.getPrefix()); 133 url.append(page); 134 Subscription subscription = null; 135 try { 136 subscription = (Subscription) pageContext.findAttribute(name); 137 } catch (ClassCastException e) { 138 subscription = null; 139 } 140 if (subscription == null) 141 throw new JspException 142 (messages.getMessage("linkSubscription.noSubscription", name)); 143 if (page.indexOf("?") < 0) 144 url.append("?"); 145 else 146 url.append("&"); 147 url.append("username="); 148 url.append(TagUtils.getInstance().filter(subscription.getUser().getUsername())); 149 url.append("&host="); 150 url.append(TagUtils.getInstance().filter(subscription.getHost())); 151 152 HttpServletResponse response = 154 (HttpServletResponse ) pageContext.getResponse(); 155 StringBuffer results = new StringBuffer ("<a HREF=\""); 156 results.append(response.encodeURL(url.toString())); 157 results.append("\">"); 158 159 JspWriter writer = pageContext.getOut(); 161 try { 162 writer.print(results.toString()); 163 } catch (IOException e) { 164 throw new JspException 165 (messages.getMessage("linkSubscription.io", e.toString())); 166 } 167 168 return (EVAL_BODY_INCLUDE); 170 171 } 172 173 174 175 180 public int doEndTag() throws JspException { 181 182 183 JspWriter writer = pageContext.getOut(); 185 try { 186 writer.print("</a>"); 187 } catch (IOException e) { 188 throw new JspException 189 (messages.getMessage("link.io", e.toString())); 190 } 191 192 return (EVAL_PAGE); 193 194 } 195 196 197 200 public void release() { 201 202 super.release(); 203 this.page = null; 204 this.name = "subscription"; 205 206 } 207 208 209 } 210 | Popular Tags |