KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > DefaultXmlRpcTransport


1 /*
2  * Copyright 1999,2005 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
18 package org.apache.xmlrpc;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import org.apache.xmlrpc.util.HttpUtil;
26
27 /**
28  * Interface from XML-RPC to the default HTTP transport based on the
29  * @see java.net.URLConnection class.
30  *
31  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
32  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
33  * @author <a HREF="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
34  * @version $Id: DefaultXmlRpcTransport.java,v 1.4 2005/04/22 10:25:57 hgomez Exp $
35  * @since 1.2
36  */

37 public class DefaultXmlRpcTransport implements XmlRpcTransport
38 {
39     protected URL JavaDoc url;
40     protected String JavaDoc auth;
41     protected URLConnection JavaDoc con;
42
43     /**
44      * Create a new DefaultXmlRpcTransport with the specified URL and basic
45      * authorization string.
46      *
47      * @deprecated Use setBasicAuthentication instead of passing an encoded authentication String.
48      *
49      * @param url the url to POST XML-RPC requests to.
50      * @param auth the Base64 encoded HTTP Basic authentication value.
51      */

52     public DefaultXmlRpcTransport(URL JavaDoc url, String JavaDoc auth)
53     {
54         this.url = url;
55         this.auth = auth;
56     }
57
58     /**
59      * Create a new DefaultXmlRpcTransport with the specified URL.
60      *
61      * @param url the url to POST XML-RPC requests to.
62      */

63     public DefaultXmlRpcTransport(URL JavaDoc url)
64     {
65         this(url, null);
66     }
67
68     public InputStream JavaDoc sendXmlRpc(byte [] request)
69     throws IOException JavaDoc
70     {
71         con = url.openConnection();
72         con.setDoInput(true);
73         con.setDoOutput(true);
74         con.setUseCaches(false);
75         con.setAllowUserInteraction(false);
76         con.setRequestProperty("Content-Length",
77         Integer.toString(request.length));
78         con.setRequestProperty("Content-Type", "text/xml");
79         if (auth != null)
80         {
81             con.setRequestProperty("Authorization", "Basic " + auth);
82         }
83         OutputStream JavaDoc out = con.getOutputStream();
84         out.write(request);
85         out.flush();
86         out.close();
87         return con.getInputStream();
88     }
89
90     /**
91      * Sets Authentication for this client. This will be sent as Basic
92      * Authentication header to the server as described in
93      * <a HREF="http://www.ietf.org/rfc/rfc2617.txt">
94      * http://www.ietf.org/rfc/rfc2617.txt</a>.
95      */

96     public void setBasicAuthentication(String JavaDoc user, String JavaDoc password)
97     {
98         auth = HttpUtil.encodeBasicAuthentication(user, password);
99     }
100
101     public void endClientRequest()
102     throws XmlRpcClientException
103     {
104         try
105         {
106             con.getInputStream().close();
107         }
108         catch (Exception JavaDoc e)
109         {
110             throw new XmlRpcClientException("Exception closing URLConnection", e);
111         }
112     }
113 }
114
Popular Tags