KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.HttpURLConnection JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.MalformedURLException 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.TagSupport JavaDoc;
36
37 /** A helper JSP Custom tag which makes a SOAP HTTP POST request.
38   *
39   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
40   * @version $Revision: 1.2 $
41   */

42 public class XmlRpcTag extends HttpTag {
43
44     protected static final String JavaDoc HOST_NAME = getHostName();
45     
46     /** The userAgent used */
47     private String JavaDoc userAgent = "jakarta:io-tags/1.0";
48     
49     /** The host */
50     private String JavaDoc host = HOST_NAME;
51     
52     /** The content type used */
53     private String JavaDoc contentType = "text/xml";
54     
55     
56     public XmlRpcTag() {
57         setAction( "POST" );
58     }
59
60     // Properties
61
//-------------------------------------------------------------------------
62
public void setUserAgent(String JavaDoc userAgent) {
63         this.userAgent = userAgent;
64     }
65
66     // Implementation methods
67
//-------------------------------------------------------------------------
68
protected void configureConnection( URLConnection JavaDoc connection ) throws IOException JavaDoc {
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     /** @return the host name of the current JVM */
76     protected static String JavaDoc getHostName() {
77         String JavaDoc answer = null;
78         try {
79             InetAddress JavaDoc 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 JavaDoc 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