View Javadoc
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  public class SafeProtocol implements TcpProtocol
25  {
26  
27      public static final String COOKIE = "You are using SafeProtocol";
28      private TcpProtocol delegate = new LengthProtocol();
29      private TcpProtocol cookieProtocol = new LengthProtocol(COOKIE.length());
30  
31      public Object read(InputStream is) throws IOException
32      {
33          if (assertSiblingSafe(is))
34          {
35              Object result = delegate.read(is);
36              if (null == result)
37              {
38                  // EOF after cookie but before data
39                  helpUser();
40              }
41              return result;
42          }
43          else
44          {
45              return null;
46          }
47      }
48  
49      public void write(OutputStream os, Object data) throws IOException
50      {
51          assureSibling(os);
52          delegate.write(os, data);
53      }
54  
55      public ResponseOutputStream createResponse(Socket socket) throws IOException
56      {
57          return new ResponseOutputStream(socket, new ProtocolStream(this, false, socket.getOutputStream()));
58      }
59  
60      private void assureSibling(OutputStream os) throws IOException
61      {
62          cookieProtocol.write(os, COOKIE);
63      }
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          Object cookie = null;
73          try
74          {
75              cookie = cookieProtocol.read(is);
76          }
77          catch (Exception e)
78          {
79              helpUser(e);
80          }
81          if (null != cookie)
82          {
83              if (!(cookie instanceof byte[]
84                      && ((byte[]) cookie).length == COOKIE.length()
85                      && COOKIE.equals(new String((byte[]) cookie))))
86              {
87                  helpUser();
88              }
89              else
90              {
91                  return true;
92              }
93          }
94          return false; // eof
95      }
96  
97      private void helpUser() throws IOException
98      {
99          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         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         delegate = new LengthProtocol(maxMessageLength);
115     }
116 
117 }