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.net.HttpURLConnection ; 26 import java.net.InetAddress ; 27 import java.net.MalformedURLException ; 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.TagSupport ; 36 37 42 public class XmlRpcTag extends HttpTag { 43 44 protected static final String HOST_NAME = getHostName(); 45 46 47 private String userAgent = "jakarta:io-tags/1.0"; 48 49 50 private String host = HOST_NAME; 51 52 53 private String contentType = "text/xml"; 54 55 56 public XmlRpcTag() { 57 setAction( "POST" ); 58 } 59 60 public void setUserAgent(String userAgent) { 63 this.userAgent = userAgent; 64 } 65 66 protected void configureConnection( URLConnection connection ) throws IOException { 69 super.configureConnection( connection ); 70 connection.setRequestProperty( "Content-Type", "text/xml" ); 71 connection.setRequestProperty( "User-Agent", userAgent ); 72 connection.setRequestProperty( "Host", pageContext.getRequest().getServerName() ); 73 } 74 75 76 protected static String getHostName() { 77 String answer = null; 78 try { 79 InetAddress address = InetAddress.getLocalHost(); 80 if ( address != null ) { 81 answer = address.getHostName(); 82 if ( answer == null || answer.length() <= 0 ) { 83 answer = address.getHostAddress(); 84 } 85 86 } 87 } 88 catch (Exception e) { 89 if ( WARN ) { 90 System.out.println( "Couldn't resolve hostname" + e ); 91 e.printStackTrace(); 92 } 93 } 94 return ( answer != null ) ? answer : "localhost"; 95 } 96 97 } 98 | Popular Tags |