XmlRpcStreamTransport.java
Go to the documentation of this file.
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.io.OutputStream;
00024 import java.util.zip.GZIPInputStream;
00025 import java.util.zip.GZIPOutputStream;
00026 
00027 import org.apache.xmlrpc.XmlRpcException;
00028 import org.apache.xmlrpc.XmlRpcRequest;
00029 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00030 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
00031 import org.apache.xmlrpc.parser.XmlRpcResponseParser;
00032 import org.apache.xmlrpc.serializer.XmlRpcWriter;
00033 import org.apache.xmlrpc.util.SAXParsers;
00034 import org.xml.sax.ContentHandler;
00035 import org.xml.sax.InputSource;
00036 import org.xml.sax.SAXException;
00037 import org.xml.sax.XMLReader;
00038 
00039 
00044 public abstract class XmlRpcStreamTransport extends XmlRpcTransportImpl {
00045     protected interface ReqWriter {
00050         void write(OutputStream pStream) throws XmlRpcException, IOException, SAXException;
00051     }
00052 
00053     protected class ReqWriterImpl implements ReqWriter {
00054         private final XmlRpcRequest request;
00055 
00056         protected ReqWriterImpl(XmlRpcRequest pRequest) {
00057             request = pRequest;
00058         }
00059 
00064         public void write(OutputStream pStream)
00065                 throws XmlRpcException, IOException, SAXException {
00066             final XmlRpcStreamConfig config = (XmlRpcStreamConfig) request.getConfig();
00067             try {
00068                 ContentHandler h = getClient().getXmlWriterFactory().getXmlWriter(config, pStream);
00069                 XmlRpcWriter xw = new XmlRpcWriter(config, h, getClient().getTypeFactory());
00070                 xw.write(request);
00071                 pStream.close();
00072                 pStream = null;
00073             } finally {
00074                 if (pStream != null) { try { pStream.close(); } catch (Throwable ignore) {} }
00075             }
00076         }
00077     }
00078 
00079     protected class GzipReqWriter implements ReqWriter {
00080         private final ReqWriter reqWriter;
00081         protected GzipReqWriter(ReqWriter pReqWriter) {
00082             reqWriter = pReqWriter;
00083         }
00084 
00085         public void write(OutputStream pStream) throws XmlRpcException, IOException, SAXException {
00086             try {
00087                 GZIPOutputStream gStream = new GZIPOutputStream(pStream);
00088                 reqWriter.write(gStream);
00089                 pStream.close();
00090                 pStream = null;
00091             } catch (IOException e) {
00092                 throw new XmlRpcException("Failed to write request: " + e.getMessage(), e);
00093             } finally {
00094                 if (pStream != null) { try { pStream.close(); } catch (Throwable ignore) {} }
00095             }
00096         }
00097     }
00098 
00101         protected XmlRpcStreamTransport(XmlRpcClient pClient) {
00102                 super(pClient);
00103         }
00104 
00108         protected abstract void close() throws XmlRpcClientException;
00109 
00114         protected abstract boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig);
00115 
00119         protected abstract InputStream getInputStream() throws XmlRpcException;
00120 
00121         protected boolean isCompressingRequest(XmlRpcStreamRequestConfig pConfig) {
00122                 return pConfig.isEnabledForExtensions()
00123                         && pConfig.isGzipCompressing();
00124         }
00125 
00134     protected ReqWriter newReqWriter(XmlRpcRequest pRequest)
00135             throws XmlRpcException, IOException, SAXException {
00136         ReqWriter reqWriter = new ReqWriterImpl(pRequest);
00137         if (isCompressingRequest((XmlRpcStreamRequestConfig) pRequest.getConfig())) {
00138             reqWriter = new GzipReqWriter(reqWriter);
00139         }
00140         return reqWriter;
00141     }
00142 
00143     protected abstract void writeRequest(ReqWriter pWriter)
00144         throws XmlRpcException, IOException, SAXException;
00145     
00146         public Object sendRequest(XmlRpcRequest pRequest) throws XmlRpcException {
00147                 XmlRpcStreamRequestConfig config = (XmlRpcStreamRequestConfig) pRequest.getConfig();
00148                 boolean closed = false;
00149                 try {
00150             ReqWriter reqWriter = newReqWriter(pRequest);
00151                         writeRequest(reqWriter);
00152                         InputStream istream = getInputStream();
00153                         if (isResponseGzipCompressed(config)) {
00154                                 istream = new GZIPInputStream(istream);
00155                         }
00156                         Object result = readResponse(config, istream);
00157                         closed = true;
00158                         close();
00159                         return result;
00160                 } catch (IOException e) {
00161                         throw new XmlRpcException("Failed to read server's response: "
00162                                         + e.getMessage(), e);
00163         } catch (SAXException e) {
00164             Exception ex = e.getException();
00165             if (ex != null  &&  ex instanceof XmlRpcException) {
00166                 throw (XmlRpcException) ex;
00167             }
00168             throw new XmlRpcException("Failed to generate request: "
00169                     + e.getMessage(), e);
00170                 } finally {
00171                         if (!closed) { try { close(); } catch (Throwable ignore) {} }
00172                 }
00173         }
00174 
00175         protected XMLReader newXMLReader() throws XmlRpcException {
00176                 return SAXParsers.newXMLReader();
00177         }
00178 
00179         protected Object readResponse(XmlRpcStreamRequestConfig pConfig, InputStream pStream) throws XmlRpcException {
00180                 InputSource isource = new InputSource(pStream);
00181                 XMLReader xr = newXMLReader();
00182                 XmlRpcResponseParser xp;
00183                 try {
00184                         xp = new XmlRpcResponseParser(pConfig, getClient().getTypeFactory());
00185                         xr.setContentHandler(xp);
00186             xr.parse(isource);
00187                 } catch (SAXException e) {
00188                         throw new XmlRpcClientException("Failed to parse server's response: " + e.getMessage(), e);
00189                 } catch (IOException e) {
00190                         throw new XmlRpcClientException("Failed to read server's response: " + e.getMessage(), e);
00191                 }
00192                 if (xp.isSuccess()) {
00193                         return xp.getResult();
00194                 }
00195                 Throwable t = xp.getErrorCause();
00196         if (t == null) {
00197             throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage());
00198         }
00199         if (t instanceof XmlRpcException) {
00200             throw (XmlRpcException) t;
00201         }
00202         if (t instanceof RuntimeException) {
00203             throw (RuntimeException) t;
00204         }
00205         throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage(), t);
00206         }
00207 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49