Coverage Report - org.apache.xmlrpc.client.XmlRpcLocalStreamTransport
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcLocalStreamTransport
90% 
100% 
1,636
 
 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  
 package org.apache.xmlrpc.client;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.OutputStream;
 23  
 
 24  
 import org.apache.xmlrpc.XmlRpcException;
 25  
 import org.apache.xmlrpc.XmlRpcRequest;
 26  
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 27  
 import org.apache.xmlrpc.server.XmlRpcServer;
 28  
 import org.apache.xmlrpc.server.XmlRpcStreamServer;
 29  
 
 30  
 
 31  
 /** Another local transport for debugging and testing. This one is
 32  
  * similar to the {@link org.apache.xmlrpc.client.XmlRpcLocalTransport},
 33  
  * except that it adds request serialization. In other words, it is
 34  
  * particularly well suited for development and testing of XML serialization
 35  
  * and parsing.
 36  
  */
 37  
 public class XmlRpcLocalStreamTransport extends XmlRpcStreamTransport {
 38  48
         private class LocalStreamConnection {
 39  
                 ByteArrayOutputStream ostream, istream;
 40  
         }
 41  22
         private class LocalServer extends XmlRpcStreamServer {
 42  
                 public Object execute(XmlRpcRequest pRequest) throws XmlRpcException {
 43  35
                         XmlRpcServer server = ((XmlRpcLocalClientConfig) pRequest.getConfig()).getXmlRpcServer();
 44  35
                         return server.execute(pRequest);
 45  
                 }
 46  
                 protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws IOException {
 47  35
                         LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 48  35
                         return new ByteArrayInputStream(lsc.ostream.toByteArray());
 49  
                 }
 50  
                 protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws IOException {
 51  35
                         LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 52  35
                         lsc.istream = new ByteArrayOutputStream();
 53  35
                         return lsc.istream;
 54  
                 }
 55  
                 protected void closeConnection(Object pConnection) throws IOException {
 56  35
                         LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 57  35
                         if (lsc.istream != null) {
 58  35
                                 try { lsc.istream.close(); } catch (Throwable ignore) {}
 59  
                         }
 60  35
                 }
 61  
                 
 62  
         }
 63  
 
 64  22
         private final XmlRpcStreamServer localServer = new LocalServer();
 65  
 
 66  
         /** Creates a new instance.
 67  
          * @param pClient The client, which is controlling the transport.
 68  
          */
 69  
         public XmlRpcLocalStreamTransport(XmlRpcClient pClient) {
 70  22
                 super(pClient);
 71  22
         }
 72  
 
 73  
         protected Object newConnection(XmlRpcStreamRequestConfig pConfig) throws XmlRpcClientException {
 74  48
                 return new LocalStreamConnection();
 75  
         }
 76  
 
 77  
         protected void closeConnection(Object pConnection) throws XmlRpcClientException {
 78  48
                 LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 79  48
                 if (lsc.ostream != null) {
 80  48
                         try { lsc.ostream.close(); } catch (Throwable ignore) {}
 81  
                 }
 82  48
         }
 83  
 
 84  
         protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcClientException {
 85  48
                 LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 86  48
                 lsc.ostream = new ByteArrayOutputStream();
 87  48
                 return lsc.ostream;
 88  
         }
 89  
 
 90  
         protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection)
 91  
                         throws XmlRpcException {
 92  35
                 LocalStreamConnection lsc = (LocalStreamConnection) pConnection;
 93  
                 try {
 94  35
                         localServer.execute(pConfig, pConnection);
 95  0
                 } catch (IOException e) {
 96  0
                         throw new XmlRpcException(e.getMessage(), e);
 97  
                 }
 98  35
                 return new ByteArrayInputStream(lsc.istream.toByteArray());
 99  
         }
 100  
 
 101  
         protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig, Object pConnection) {
 102  35
                 return pConfig.isGzipRequesting();
 103  
         }
 104  
 
 105  
         protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection, byte[] pContent) throws XmlRpcException {
 106  0
                 throw new IllegalStateException("Not implemented");
 107  
         }
 108  
 }