|
1 | 1 | package org.java_websocket; |
2 | 2 |
|
| 3 | +import java.net.InetSocketAddress; |
| 4 | + |
3 | 5 | import org.java_websocket.drafts.Draft; |
4 | 6 | import org.java_websocket.exceptions.InvalidDataException; |
| 7 | +import org.java_websocket.exceptions.InvalidHandshakeException; |
5 | 8 | import org.java_websocket.framing.Framedata; |
6 | 9 | import org.java_websocket.framing.Framedata.Opcode; |
7 | 10 | import org.java_websocket.framing.FramedataImpl1; |
@@ -79,12 +82,23 @@ public void onWebsocketPong( WebSocket conn, Framedata f ) { |
79 | 82 | * This is specifically implemented for gitime's WebSocket client for Flash: |
80 | 83 | * http://github.com/gimite/web-socket-js |
81 | 84 | * |
82 | | - * @return An XML String that comforms to Flash's security policy. You MUST |
| 85 | + * @return An XML String that comforts to Flash's security policy. You MUST |
83 | 86 | * not include the null char at the end, it is appended automatically. |
| 87 | + * @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained e.g because the websocket is not connected. |
84 | 88 | */ |
85 | 89 | @Override |
86 | | - public String getFlashPolicy( WebSocket conn ) { |
87 | | - return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0"; |
| 90 | + public String getFlashPolicy( WebSocket conn ) throws InvalidDataException { |
| 91 | + InetSocketAddress adr = conn.getLocalSocketAddress(); |
| 92 | + if(null == adr){ |
| 93 | + throw new InvalidHandshakeException( "socket not bound" ); |
| 94 | + } |
| 95 | + |
| 96 | + StringBuffer sb = new StringBuffer( 90 ); |
| 97 | + sb.append( "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" ); |
| 98 | + sb.append(adr.getPort()); |
| 99 | + sb.append( "\" /></cross-domain-policy>\0" ); |
| 100 | + |
| 101 | + return sb.toString(); |
88 | 102 | } |
89 | 103 |
|
90 | 104 | } |
0 commit comments