1 16 17 package org.apache.taglibs.io; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.Reader ; 26 import java.io.Writer ; 27 import java.net.HttpURLConnection ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 31 import javax.servlet.ServletContext ; 32 import javax.servlet.jsp.JspException ; 33 import javax.servlet.jsp.JspWriter ; 34 import javax.servlet.jsp.tagext.BodyContent ; 35 import javax.servlet.jsp.tagext.Tag ; 36 37 42 public class URLTag extends AbstractTag implements PipeProducer, PipeConsumer { 43 44 45 private String url; 46 47 48 private String fullURL; 49 50 51 private String encoding; 52 53 54 private URLConnection connection; 55 56 57 private Writer writer; 58 59 60 private Reader reader; 61 62 63 private byte[] buffer = new byte[ 32*1024 ]; 64 65 66 private boolean output = true; 67 68 69 private boolean input = false; 70 71 public URLTag() { 72 } 73 74 75 77 public void addHeader( String name, String value ) throws JspException { 78 if ( TRACE ) { 79 log( "Setting header: " + name + ": " + value ); 80 } 81 try { 82 getConnection().setRequestProperty( name, value ); 83 } 84 catch (IOException e) { 85 handleException(e); 86 } 87 } 88 89 91 public void addParameter( String name, String value ) { 92 if ( fullURL == null ) { 93 fullURL = url; 94 } 95 String prefix = ( fullURL.indexOf( '?' ) < 0 ) ? "?" : "&"; 96 fullURL += prefix + name + "=" + value; 97 98 100 if ( TRACE ) { 101 log( "Adding parameter: " + name + ": " + value ); 102 log( "URL is now: " + fullURL ); 103 } 104 } 105 106 107 public void setWriter(Writer writer) { 110 this.writer = writer; 111 } 112 113 public void setReader(Reader reader) { 116 this.reader = reader; 117 this.input = true; 118 } 119 120 121 public int doStartTag() throws JspException { 124 fullURL = null; 125 connection = null; 126 return EVAL_BODY_INCLUDE; 127 } 128 129 public int doEndTag() throws JspException { 130 Writer writer = null; 131 try { 132 if ( reader != null ) { 133 writer = getURLWriter(); 134 PipeHelper.pipe( reader, writer ); 135 reader.close(); 136 writer.close(); 137 } 138 if ( output ) { 139 readURL(); 140 } 141 else { 142 getConnection().connect(); 143 } 144 } 145 catch (IOException e) { 146 handleException(e); 147 } 148 finally { 149 if ( writer != null ) { 152 try { 153 writer.close(); 154 } 155 catch (Exception e) { 156 } 157 } 158 reader = null; 159 writer = null; 160 } 161 return EVAL_PAGE; 162 } 163 164 public void release() { 165 super.release(); 166 url = null; 167 fullURL = null; 168 connection = null; 169 buffer = null; 170 input = false; 171 output = true; 172 reader = null; 173 writer = null; 174 } 175 176 177 public void setUrl(String url) { 180 this.url = url; 181 } 182 183 public void setOutput(boolean output) { 184 this.output = output; 185 } 186 187 public void setInput(boolean input) { 188 this.input = input; 189 190 } 192 193 194 public void setEncoding(String encoding) { 195 this.encoding = encoding; 196 } 197 198 public Writer getURLWriter() throws IOException { 199 OutputStream out = getURLOutputStream(); 200 if ( out != null ) { 201 if ( encoding != null ) { 202 return new OutputStreamWriter ( out, encoding ); 203 } 204 else { 205 return new OutputStreamWriter ( out ); 206 } 207 } 208 return null; 209 } 210 211 public OutputStream getURLOutputStream() throws IOException { 212 URLConnection connection = getConnection(); 213 return connection.getOutputStream(); 214 } 215 216 public Reader getURLReader() throws IOException { 217 InputStream in = getURLInputStream(); 218 if ( in != null ) { 219 if ( encoding != null ) { 220 return new InputStreamReader ( in, encoding ); 221 } 222 else { 223 return new InputStreamReader ( in ); 224 } 225 } 226 return null; 227 } 228 229 public InputStream getURLInputStream() throws IOException { 230 URLConnection connection = getConnection(); 231 connection.connect(); 232 return connection.getInputStream(); 233 } 234 235 236 protected URLConnection getConnection() throws IOException { 239 if ( connection == null ) { 240 if ( fullURL == null ) { 241 fullURL = url; 242 } 243 URL theURL = URLHelper.createURL(fullURL, pageContext); 244 245 if ( TRACE ) { 246 log( "Reading URL: " + theURL.toString() ); 247 } 248 249 connection = theURL.openConnection(); 250 configureConnection( connection ); 251 } 252 return connection; 253 } 254 255 protected void configureConnection( URLConnection connection ) throws IOException { 256 258 connection.setAllowUserInteraction(false); 259 connection.setDoInput(output); 260 connection.setDoOutput(input); 261 } 262 263 protected void disconnect( URLConnection connection ) { 264 try { 265 if ( connection instanceof HttpURLConnection ) { 266 HttpURLConnection httpConnection = (HttpURLConnection ) connection; 267 httpConnection.disconnect(); 268 } 269 } 270 catch (Exception e) { 271 } 273 } 274 275 protected void readURL() throws IOException , JspException { 276 Tag parent = getParent(); 277 if ( writer == null && parent instanceof PipeConsumer ) { 278 PipeConsumer consumer = (PipeConsumer) parent; 279 consumer.setReader( getURLReader() ); 280 } 281 else { 282 if ( writer == null ) { 283 writer = pageContext.getOut(); 284 } 285 Reader reader = getURLReader(); 286 try { 287 PipeHelper.pipe( reader, writer ); 288 } 289 finally { 290 try { 291 reader.close(); 292 } 293 catch (Exception e) { 294 } 295 } 296 } 297 } 298 } 299 | Popular Tags |