KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > io > URLTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.io;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.JspWriter JavaDoc;
34 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
35 import javax.servlet.jsp.tagext.Tag JavaDoc;
36
37 /** A JSP Custom tag to make a URL request and output the response.
38   *
39   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
40   * @version $Revision: 1.7 $
41   */

42 public class URLTag extends AbstractTag implements PipeProducer, PipeConsumer {
43
44     /** URL property to request */
45     private String JavaDoc url;
46     
47     /** The current full URL including any <io:param> tags */
48     private String JavaDoc fullURL;
49     
50     /** The encoding to use (defaults to JVM encoding) */
51     private String JavaDoc encoding;
52     
53     /** URL connection */
54     private URLConnection JavaDoc connection;
55     
56     /** Writer to output to */
57     private Writer JavaDoc writer;
58     
59     /** Reader used as source of data written or POSTed to the URL */
60     private Reader JavaDoc reader;
61     
62     /** Buffer used to pipe output to JSP writer */
63     private byte[] buffer = new byte[ 32*1024 ];
64
65     /** Whether or not we should read from the URL */
66     private boolean output = true;
67
68     /** Whether or not we should write to the URL */
69     private boolean input = false;
70
71     public URLTag() {
72     }
73     
74
75     /** Adds a HTTP header to the current request
76       */

77     public void addHeader( String JavaDoc name, String JavaDoc value ) throws JspException JavaDoc {
78         if ( TRACE ) {
79             log( "Setting header: " + name + ": " + value );
80         }
81         try {
82             getConnection().setRequestProperty( name, value );
83         }
84         catch (IOException JavaDoc e) {
85             handleException(e);
86         }
87     }
88
89     /** Adds a HTTP query parameter to the current url
90       */

91     public void addParameter( String JavaDoc name, String JavaDoc value ) {
92         if ( fullURL == null ) {
93             fullURL = url;
94         }
95         String JavaDoc prefix = ( fullURL.indexOf( '?' ) < 0 ) ? "?" : "&";
96         fullURL += prefix + name + "=" + value;
97         
98         // XXXX: may wish to add some encoding code here...
99

100         if ( TRACE ) {
101             log( "Adding parameter: " + name + ": " + value );
102             log( "URL is now: " + fullURL );
103         }
104     }
105
106     
107     // PipeProducer interface
108
//-------------------------------------------------------------------------
109
public void setWriter(Writer JavaDoc writer) {
110         this.writer = writer;
111     }
112     
113     // PipeConsumer interface
114
//-------------------------------------------------------------------------
115
public void setReader(Reader JavaDoc reader) {
116         this.reader = reader;
117         this.input = true;
118     }
119
120     
121     // Tag interface
122
//-------------------------------------------------------------------------
123
public int doStartTag() throws JspException JavaDoc {
124         fullURL = null;
125         connection = null;
126         return EVAL_BODY_INCLUDE;
127     }
128     
129     public int doEndTag() throws JspException JavaDoc {
130         Writer JavaDoc 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 JavaDoc e) {
146             handleException(e);
147         }
148         finally {
149             // we don't disconnect as our outer tag may read us after the doEndTag()
150
// disconnect( connection );
151
if ( writer != null ) {
152                 try {
153                     writer.close();
154                 }
155                 catch (Exception JavaDoc 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     // Properties
178
//-------------------------------------------------------------------------
179
public void setUrl(String JavaDoc 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         //log( "input has been set to: " + this.input );
191
}
192     
193     /** Sets the character encoding */
194     public void setEncoding(String JavaDoc encoding) {
195         this.encoding = encoding;
196     }
197     
198     public Writer JavaDoc getURLWriter() throws IOException JavaDoc {
199         OutputStream JavaDoc out = getURLOutputStream();
200         if ( out != null ) {
201             if ( encoding != null ) {
202                 return new OutputStreamWriter JavaDoc( out, encoding );
203             }
204             else {
205                 return new OutputStreamWriter JavaDoc( out );
206             }
207         }
208         return null;
209     }
210     
211     public OutputStream JavaDoc getURLOutputStream() throws IOException JavaDoc {
212         URLConnection JavaDoc connection = getConnection();
213         return connection.getOutputStream();
214     }
215
216     public Reader JavaDoc getURLReader() throws IOException JavaDoc {
217         InputStream JavaDoc in = getURLInputStream();
218         if ( in != null ) {
219             if ( encoding != null ) {
220                 return new InputStreamReader JavaDoc( in, encoding );
221             }
222             else {
223                 return new InputStreamReader JavaDoc( in );
224             }
225         }
226         return null;
227     }
228     
229     public InputStream JavaDoc getURLInputStream() throws IOException JavaDoc {
230         URLConnection JavaDoc connection = getConnection();
231         connection.connect();
232         return connection.getInputStream();
233     }
234
235     
236     // Implementation methods
237
//-------------------------------------------------------------------------
238
protected URLConnection JavaDoc getConnection() throws IOException JavaDoc {
239         if ( connection == null ) {
240             if ( fullURL == null ) {
241                 fullURL = url;
242             }
243             URL JavaDoc 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 JavaDoc connection ) throws IOException JavaDoc {
256         //log( "#### configureConnection(): output: " + output + " input: " + input );
257

258         connection.setAllowUserInteraction(false);
259         connection.setDoInput(output);
260         connection.setDoOutput(input);
261     }
262     
263     protected void disconnect( URLConnection JavaDoc connection ) {
264         try {
265             if ( connection instanceof HttpURLConnection JavaDoc ) {
266                 HttpURLConnection JavaDoc httpConnection = (HttpURLConnection JavaDoc) connection;
267                 httpConnection.disconnect();
268             }
269         }
270         catch (Exception JavaDoc e) {
271             // ignore errors, we are just trying to close gracefully
272
}
273     }
274     
275     protected void readURL() throws IOException JavaDoc, JspException JavaDoc {
276         Tag JavaDoc 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 JavaDoc reader = getURLReader();
286             try {
287                 PipeHelper.pipe( reader, writer );
288             }
289             finally {
290                 try {
291                     reader.close();
292                 }
293                 catch (Exception JavaDoc e) {
294                 }
295             }
296         }
297     }
298 }
299
Popular Tags