1 18 19 package org.apache.struts.taglib.bean; 20 21 import java.io.BufferedInputStream ; 22 import java.io.InputStreamReader ; 23 import java.net.HttpURLConnection ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.util.Map ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.tagext.TagSupport ; 31 import org.apache.struts.util.MessageResources; 32 import org.apache.struts.util.RequestUtils; 33 import org.apache.struts.taglib.TagUtils; 34 35 47 48 public class IncludeTag extends TagSupport { 49 50 52 55 protected static final int BUFFER_SIZE = 256; 56 57 60 protected String anchor = null; 61 62 public String getAnchor() { 63 return (this.anchor); 64 } 65 66 public void setAnchor(String anchor) { 67 this.anchor = anchor; 68 } 69 70 74 protected String forward = null; 75 76 public String getForward() { 77 return (this.forward); 78 } 79 80 public void setForward(String forward) { 81 this.forward = forward; 82 } 83 84 87 protected String href = null; 88 89 public String getHref() { 90 return (this.href); 91 } 92 93 public void setHref(String href) { 94 this.href = href; 95 } 96 97 101 protected String id = null; 102 103 public String getId() { 104 return (this.id); 105 } 106 107 public void setId(String id) { 108 this.id = id; 109 } 110 111 114 protected static MessageResources messages = 115 MessageResources.getMessageResources("org.apache.struts.taglib.bean.LocalStrings"); 116 117 123 public void setName(String name) { 124 this.page = name; 125 } 126 127 130 protected String page = null; 131 132 public String getPage() { 133 return (this.page); 134 } 135 136 public void setPage(String page) { 137 this.page = page; 138 } 139 140 143 protected boolean transaction = false; 144 145 public boolean getTransaction() { 146 return (this.transaction); 147 } 148 149 public void setTransaction(boolean transaction) { 150 this.transaction = transaction; 151 } 152 153 protected boolean useLocalEncoding = false; 154 155 public boolean isUseLocalEncoding() { 156 return useLocalEncoding; 157 } 158 159 public void setUseLocalEncoding(boolean b) { 160 useLocalEncoding = b; 161 } 162 163 165 171 public int doStartTag() throws JspException { 172 173 Map params = 175 TagUtils.getInstance().computeParameters( 176 pageContext, 177 null, 178 null, 179 null, 180 null, 181 null, 182 null, 183 null, 184 transaction); 185 String urlString = null; 187 URL url = null; 188 try { 189 urlString = 190 TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, page, null,null, params, anchor, false, useLocalEncoding); 191 if (urlString.indexOf(':') < 0) { 192 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 193 url = new URL (RequestUtils.requestURL(request), urlString); 194 } else { 195 url = new URL (urlString); 196 } 197 } catch (MalformedURLException e) { 198 TagUtils.getInstance().saveException(pageContext, e); 199 throw new JspException (messages.getMessage("include.url", e.toString())); 200 } 201 202 URLConnection conn = null; 203 try { 204 conn = url.openConnection(); 206 conn.setAllowUserInteraction(false); 207 conn.setDoInput(true); 208 conn.setDoOutput(false); 209 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 211 addCookie(conn, urlString, request); 212 conn.connect(); 214 } catch (Exception e) { 215 TagUtils.getInstance().saveException(pageContext, e); 216 throw new JspException ( 217 messages.getMessage("include.open", url.toString(), e.toString())); 218 } 219 220 StringBuffer sb = new StringBuffer (); 222 try { 223 BufferedInputStream is = new BufferedInputStream (conn.getInputStream()); 224 InputStreamReader in = new InputStreamReader (is); char buffer[] = new char[BUFFER_SIZE]; 226 int n = 0; 227 while (true) { 228 n = in.read(buffer); 229 if (n < 1) 230 break; 231 sb.append(buffer, 0, n); 232 } 233 in.close(); 234 } catch (Exception e) { 235 TagUtils.getInstance().saveException(pageContext, e); 236 throw new JspException ( 237 messages.getMessage("include.read", url.toString(), e.toString())); 238 } 239 240 pageContext.setAttribute(id, sb.toString()); 242 243 return (SKIP_BODY); 245 } 246 254 protected void addCookie(URLConnection conn, String urlString, HttpServletRequest request) { 255 if ((conn instanceof HttpURLConnection ) 256 && urlString.startsWith(request.getContextPath()) 257 && (request.getRequestedSessionId() != null) 258 && request.isRequestedSessionIdFromCookie()) { 259 StringBuffer sb = new StringBuffer ("JSESSIONID="); 260 sb.append(request.getRequestedSessionId()); 261 conn.setRequestProperty("Cookie", sb.toString()); 262 } 263 } 264 265 268 public void release() { 269 super.release(); 270 anchor = null; 271 forward = null; 272 href = null; 273 id = null; 274 page = null; 275 transaction = false; 276 } 277 278 } 279 | Popular Tags |