1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
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 | |
|
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 | |
|
67 | |
|
68 | |
|
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; |
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 | |
} |