1 16 17 package org.apache.taglibs.xsl; 18 19 20 import java.io.BufferedInputStream ; 21 import java.io.InputStreamReader ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 30 37 38 public class ImportTag extends TagSupport { 39 40 41 43 44 47 private String id = null; 48 49 public String getId() { 50 return (this.id); 51 } 52 53 public void setId(String id) { 54 this.id = id; 55 } 56 57 58 61 private String page = null; 62 63 public String getPage() { 64 return (this.page); 65 } 66 67 public void setPage(String page) { 68 this.page = page; 69 } 70 71 72 75 private String scope = "page"; 76 77 public String getScope() { 78 return (this.scope); 79 } 80 81 public void setScope(String scope) { 82 this.scope = scope; 83 } 84 85 86 88 89 95 public int doStartTag() throws JspException { 96 97 if (!page.startsWith("/")) 100 throw new JspException ("Page value must start with '/'"); 101 102 int scopeId = 0; 104 if ("page".equalsIgnoreCase(scope)) 105 scopeId = PageContext.PAGE_SCOPE; 106 else if ("request".equalsIgnoreCase(scope)) 107 scopeId = PageContext.REQUEST_SCOPE; 108 else if ("session".equalsIgnoreCase(scope)) 109 scopeId = PageContext.SESSION_SCOPE; 110 else if ("application".equalsIgnoreCase(scope)) 111 scopeId = PageContext.APPLICATION_SCOPE; 112 else 113 throw new JspException ("Invalid scope value '" + scope + "'"); 114 115 StringBuffer sb = new StringBuffer (); 117 118 HttpServletRequest request = 122 (HttpServletRequest ) pageContext.getRequest(); 123 StringBuffer url = new StringBuffer (); 124 url.append(request.getScheme()); 125 url.append("://"); 126 url.append(request.getServerName()); 127 if (request.getServerPort() != 80) { 128 url.append(':'); 129 url.append(request.getServerPort()); 130 } 131 if (request.getContextPath() != null) 132 url.append(request.getContextPath()); 133 url.append(page); URLConnection conn = null; 135 try { 136 conn = (new URL (url.toString())).openConnection(); 137 conn.setAllowUserInteraction(false); 138 conn.setDoInput(true); 139 conn.setDoOutput(false); 140 conn.connect(); 141 } catch (Exception e) { 142 throw new JspException ("Error opening connection: " + 143 e.toString()); 144 } 145 146 try { 148 BufferedInputStream is = 149 new BufferedInputStream (conn.getInputStream()); 150 InputStreamReader in = new InputStreamReader (is); 151 while (true) { 152 int ch = in.read(); 153 if (ch < 0) 154 break; 155 sb.append((char) ch); 156 } 157 } catch (Exception e) { 158 throw new JspException ("Error reading connection: " + 159 e.toString()); 160 } 161 162 pageContext.setAttribute(id, sb.toString(), scopeId); 164 165 return (SKIP_BODY); 167 168 } 169 170 171 } 172 | Popular Tags |