1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.tcp.protocols; |
12 | |
|
13 | |
import org.mule.ResponseOutputStream; |
14 | |
import org.mule.transport.tcp.TcpProtocol; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.io.InputStream; |
18 | |
import java.io.OutputStream; |
19 | |
import java.net.Socket; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class SafeProtocol implements TcpProtocol |
29 | |
{ |
30 | |
|
31 | |
public static final String COOKIE = "You are using SafeProtocol"; |
32 | 0 | private TcpProtocol delegate = new LengthProtocol(); |
33 | 0 | private TcpProtocol cookieProtocol = new LengthProtocol(COOKIE.length()); |
34 | |
|
35 | |
public Object read(InputStream is) throws IOException |
36 | |
{ |
37 | 0 | if (assertSiblingSafe(is)) |
38 | |
{ |
39 | 0 | Object result = delegate.read(is); |
40 | 0 | if (null == result) |
41 | |
{ |
42 | |
|
43 | 0 | helpUser(); |
44 | |
} |
45 | 0 | return result; |
46 | |
} |
47 | |
else |
48 | |
{ |
49 | 0 | return null; |
50 | |
} |
51 | |
} |
52 | |
|
53 | |
public void write(OutputStream os, Object data) throws IOException |
54 | |
{ |
55 | 0 | assureSibling(os); |
56 | 0 | delegate.write(os, data); |
57 | 0 | } |
58 | |
|
59 | |
public ResponseOutputStream createResponse(Socket socket) throws IOException |
60 | |
{ |
61 | 0 | return new ResponseOutputStream(socket, new ProtocolStream(this, false, socket.getOutputStream())); |
62 | |
} |
63 | |
|
64 | |
private void assureSibling(OutputStream os) throws IOException |
65 | |
{ |
66 | 0 | cookieProtocol.write(os, COOKIE); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private boolean assertSiblingSafe(InputStream is) throws IOException |
75 | |
{ |
76 | 0 | Object cookie = null; |
77 | |
try |
78 | |
{ |
79 | 0 | cookie = cookieProtocol.read(is); |
80 | |
} |
81 | 0 | catch (Exception e) |
82 | |
{ |
83 | 0 | helpUser(e); |
84 | 0 | } |
85 | 0 | if (null != cookie) |
86 | |
{ |
87 | 0 | if (!(cookie instanceof byte[] |
88 | |
&& ((byte[]) cookie).length == COOKIE.length() |
89 | |
&& COOKIE.equals(new String((byte[]) cookie)))) |
90 | |
{ |
91 | 0 | helpUser(); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | return true; |
96 | |
} |
97 | |
} |
98 | 0 | return false; |
99 | |
} |
100 | |
|
101 | |
private void helpUser() throws IOException |
102 | |
{ |
103 | 0 | throw new IOException("You are not using a consistent protocol on your TCP transport. " |
104 | |
+ "Please read the documentation for the TCP transport, " |
105 | |
+ "paying particular attention to the protocol parameter."); |
106 | |
} |
107 | |
|
108 | |
private void helpUser(Exception e) throws IOException |
109 | |
{ |
110 | 0 | throw (IOException) new IOException("An error occurred while verifying your connection. " |
111 | |
+ "You may not be using a consistent protocol on your TCP transport. " |
112 | |
+ "Please read the documentation for the TCP transport, " |
113 | |
+ "paying particular attention to the protocol parameter.").initCause(e); |
114 | |
} |
115 | |
|
116 | |
public void setMaxMessageLength(int maxMessageLength) |
117 | |
{ |
118 | 0 | delegate = new LengthProtocol(maxMessageLength); |
119 | 0 | } |
120 | |
|
121 | |
} |