Coverage Report - org.mule.transport.tcp.protocols.SafeProtocol
 
Classes in this File Line Coverage Branch Coverage Complexity
SafeProtocol
0%
0/29
0%
0/12
2.125
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.tcp.protocols;
 8  
 
 9  
 import org.mule.ResponseOutputStream;
 10  
 import org.mule.transport.tcp.TcpProtocol;
 11  
 
 12  
 import java.io.IOException;
 13  
 import java.io.InputStream;
 14  
 import java.io.OutputStream;
 15  
 import java.net.Socket;
 16  
 
 17  
 /**
 18  
  * This precedes every message with a cookie.
 19  
  * It should probably not be used in production.
 20  
  * We use ths protocol as the default because previously people tended to use DefaultProtocol without considering packet fragmentation etc.
 21  
  * You should probably change to LengthProtocol.
 22  
  * Remember - both sender and receiver must use the same protocol.
 23  
  */
 24  0
 public class SafeProtocol implements TcpProtocol
 25  
 {
 26  
 
 27  
     public static final String COOKIE = "You are using SafeProtocol";
 28  0
     private TcpProtocol delegate = new LengthProtocol();
 29  0
     private TcpProtocol cookieProtocol = new LengthProtocol(COOKIE.length());
 30  
 
 31  
     public Object read(InputStream is) throws IOException
 32  
     {
 33  0
         if (assertSiblingSafe(is))
 34  
         {
 35  0
             Object result = delegate.read(is);
 36  0
             if (null == result)
 37  
             {
 38  
                 // EOF after cookie but before data
 39  0
                 helpUser();
 40  
             }
 41  0
             return result;
 42  
         }
 43  
         else
 44  
         {
 45  0
             return null;
 46  
         }
 47  
     }
 48  
 
 49  
     public void write(OutputStream os, Object data) throws IOException
 50  
     {
 51  0
         assureSibling(os);
 52  0
         delegate.write(os, data);
 53  0
     }
 54  
 
 55  
     public ResponseOutputStream createResponse(Socket socket) throws IOException
 56  
     {
 57  0
         return new ResponseOutputStream(socket, new ProtocolStream(this, false, socket.getOutputStream()));
 58  
     }
 59  
 
 60  
     private void assureSibling(OutputStream os) throws IOException
 61  
     {
 62  0
         cookieProtocol.write(os, COOKIE);
 63  0
     }
 64  
 
 65  
     /**
 66  
      * @param is Stream to read data from
 67  
      * @return true if further data are available; false if EOF
 68  
      * @throws IOException
 69  
      */
 70  
     private boolean assertSiblingSafe(InputStream is) throws IOException
 71  
     {
 72  0
         Object cookie = null;
 73  
         try
 74  
         {
 75  0
             cookie = cookieProtocol.read(is);
 76  
         }
 77  0
         catch (Exception e)
 78  
         {
 79  0
             helpUser(e);
 80  0
         }
 81  0
         if (null != cookie)
 82  
         {
 83  0
             if (!(cookie instanceof byte[]
 84  
                     && ((byte[]) cookie).length == COOKIE.length()
 85  
                     && COOKIE.equals(new String((byte[]) cookie))))
 86  
             {
 87  0
                 helpUser();
 88  
             }
 89  
             else
 90  
             {
 91  0
                 return true;
 92  
             }
 93  
         }
 94  0
         return false; // eof
 95  
     }
 96  
 
 97  
     private void helpUser() throws IOException
 98  
     {
 99  0
         throw new IOException("You are not using a consistent protocol on your TCP transport. "
 100  
                 + "Please read the documentation for the TCP transport, "
 101  
                 + "paying particular attention to the protocol parameter.");
 102  
     }
 103  
 
 104  
     private void helpUser(Exception e) throws IOException
 105  
     {
 106  0
         throw (IOException) new IOException("An error occurred while verifying your connection.  "
 107  
                 + "You may not be using a consistent protocol on your TCP transport. "
 108  
                 + "Please read the documentation for the TCP transport, "
 109  
                 + "paying particular attention to the protocol parameter.").initCause(e);
 110  
     }
 111  
 
 112  
     public void setMaxMessageLength(int maxMessageLength)
 113  
     {
 114  0
         delegate = new LengthProtocol(maxMessageLength);
 115  0
     }
 116  
 
 117  
 }