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; 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.net.HttpURLConnection; 26 import java.net.MalformedURLException; 27 import java.net.URL; 28 import java.net.URLConnection; 29 30 import javax.servlet.ServletContext; 31 import javax.servlet.jsp.JspException; 32 import javax.servlet.jsp.JspWriter; 33 import javax.servlet.jsp.tagext.BodyContent; 34 import javax.servlet.jsp.tagext.TagSupport; 35 36 /** A JSP Custom tag to make a HTTP request and output the response. 37 * 38 * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a> 39 * @version $Revision: 1.2 $ 40 */ 41 public class HttpTag extends URLTag { 42 43 /** HTTP request method */ 44 private String action = "GET"; 45 46 public HttpTag() { 47 } 48 49 // Tag interface 50 //------------------------------------------------------------------------- 51 public void release() { 52 super.release(); 53 action = null; 54 } 55 56 57 // Properties 58 //------------------------------------------------------------------------- 59 public void setAction(String action) { 60 this.action = action; 61 setInput( isInputAction( action ) ); 62 } 63 64 // Implementation methods 65 //------------------------------------------------------------------------- 66 protected void configureConnection( HttpURLConnection connection ) throws IOException { 67 super.configureConnection(connection); 68 69 HttpURLConnection httpConnection = (HttpURLConnection) connection; 70 connection.setRequestMethod( action ); 71 } 72 73 /** @return true if the given HTTP action should result in some 74 * information being sent as part of the request, such as the POST action 75 */ 76 protected boolean isInputAction(String action) { 77 return "POST".equalsIgnoreCase( action ) || "PUT".equalsIgnoreCase( action ); 78 } 79 } 80