00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one 00003 * or more contributor license agreements. See the NOTICE file 00004 * distributed with this work for additional information 00005 * regarding copyright ownership. The ASF licenses this file 00006 * to you under the Apache License, Version 2.0 (the 00007 * "License"); you may not use this file except in compliance 00008 * with the License. You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, 00013 * software distributed under the License is distributed on an 00014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 00015 * KIND, either express or implied. See the License for the 00016 * specific language governing permissions and limitations 00017 * under the License. 00018 */ 00019 package org.apache.xmlrpc.client; 00020 00021 import java.io.IOException; 00022 import java.io.InputStream; 00023 import java.net.HttpURLConnection; 00024 import java.net.URL; 00025 import java.net.URLConnection; 00026 00027 import org.apache.xmlrpc.XmlRpcException; 00028 import org.apache.xmlrpc.XmlRpcRequest; 00029 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig; 00030 import org.apache.xmlrpc.util.HttpUtil; 00031 import org.xml.sax.SAXException; 00032 00033 00037 public class XmlRpcSunHttpTransport extends XmlRpcHttpTransport { 00038 private static final String userAgent = USER_AGENT + " (Sun HTTP Transport)"; 00039 private URLConnection conn; 00040 00044 public XmlRpcSunHttpTransport(XmlRpcClient pClient) { 00045 super(pClient, userAgent); 00046 } 00047 00048 protected URLConnection newURLConnection(URL pURL) throws IOException { 00049 return pURL.openConnection(); 00050 } 00051 00055 protected URLConnection getURLConnection() { 00056 return conn; 00057 } 00058 00059 public Object sendRequest(XmlRpcRequest pRequest) throws XmlRpcException { 00060 XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pRequest.getConfig(); 00061 try { 00062 final URLConnection c = conn = newURLConnection(config.getServerURL()); 00063 c.setUseCaches(false); 00064 c.setDoInput(true); 00065 c.setDoOutput(true); 00066 } catch (IOException e) { 00067 throw new XmlRpcException("Failed to create URLConnection: " + e.getMessage(), e); 00068 } 00069 return super.sendRequest(pRequest); 00070 } 00071 00072 protected void setRequestHeader(String pHeader, String pValue) { 00073 getURLConnection().setRequestProperty(pHeader, pValue); 00074 } 00075 00076 protected void close() throws XmlRpcClientException { 00077 final URLConnection c = getURLConnection(); 00078 if (c instanceof HttpURLConnection) { 00079 ((HttpURLConnection) c).disconnect(); 00080 } 00081 } 00082 00083 protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig) { 00084 return HttpUtil.isUsingGzipEncoding(getURLConnection().getHeaderField("Content-Encoding")); 00085 } 00086 00087 protected InputStream getInputStream() throws XmlRpcException { 00088 try { 00089 URLConnection connection = getURLConnection(); 00090 if ( connection instanceof HttpURLConnection ) { 00091 HttpURLConnection httpConnection = (HttpURLConnection) connection; 00092 int responseCode = httpConnection.getResponseCode(); 00093 if (responseCode < 200 || responseCode > 299) { 00094 throw new XmlRpcHttpTransportException(responseCode, httpConnection.getResponseMessage()); 00095 } 00096 } 00097 return connection.getInputStream(); 00098 } catch (IOException e) { 00099 throw new XmlRpcException("Failed to create input stream: " + e.getMessage(), e); 00100 } 00101 } 00102 00103 protected void writeRequest(ReqWriter pWriter) throws IOException, XmlRpcException, SAXException { 00104 pWriter.write(getURLConnection().getOutputStream()); 00105 } 00106 }