001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.gravity.websocket; 023 024import java.io.IOException; 025import java.nio.ByteBuffer; 026import java.nio.channels.ClosedByInterruptException; 027import java.nio.channels.ServerSocketChannel; 028import java.nio.channels.SocketChannel; 029 030import org.granite.logging.Logger; 031 032 033public class PolicyFileServer implements Runnable { 034 035 private static final Logger log = Logger.getLogger(PolicyFileServer.class); 036 037 038 private int serverPort = 843; 039 private String[] allowDomains = {}; 040 private String[] allowPorts = {}; 041 private Thread policyServer = null; 042 043 public void setServerPort(int serverPort) { 044 this.serverPort = serverPort; 045 } 046 047 public void setAllowDomains(String[] domains) { 048 this.allowDomains = domains; 049 } 050 051 public void setAllowPorts(String[] ports) { 052 this.allowPorts = ports; 053 } 054 055 public void start() { 056 policyServer = new Thread(this, "FlashPolicyFileServer:" + serverPort); 057 policyServer.start(); 058 } 059 060 public void stop() { 061 if (policyServer != null) 062 policyServer.interrupt(); 063 } 064 065 public void run() { 066 ServerSocketChannel server = null; 067 try { 068 server = ServerSocketChannel.open(); 069 server.socket().bind(new java.net.InetSocketAddress(serverPort)); 070 log.info("Flash socket policy server started on port " + serverPort); 071 } 072 catch (IOException e) { 073 log.error(e, "Could not init flash socket policy server on port " + serverPort); 074 return; 075 } 076 while (true) { 077 SocketChannel socket = null; 078 try { 079 socket = server.accept(); 080 081 ByteBuffer buf = ByteBuffer.allocate(100); 082 int size = socket.read(buf); 083 if (size == 23) { 084 byte[] req = new byte[size]; 085 buf.get(req, 0, size); 086 String request = new String(req, "UTF-8"); 087 088 log.info("Received policy file request %s", request); 089 090 String policyFile = "<?xml version=\"1.0\"?>\n" 091 + "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">" 092 + "<cross-domain-policy>"; 093 for (int i = 0; i < allowDomains.length; i++) 094 policyFile += " <allow-access-from domain=\"" + allowDomains[i] + "\" to-ports=\"" + allowPorts[i] + "\"/>"; 095 policyFile += "</cross-domain-policy>"; 096 byte[] bytes = policyFile.getBytes("UTF-8"); 097 098 socket.write(ByteBuffer.wrap(bytes)); 099 } 100 } 101 catch (ClosedByInterruptException e) { 102 log.info("Flash socket policy server stopped"); 103 break; 104 } 105 catch (IOException e) { 106 log.error(e, "Could not send policy file"); 107 } 108 finally { 109 if (socket != null) { 110 try { 111 socket.close(); 112 } 113 catch (IOException e) { 114 log.error(e, "Could not close socket"); 115 } 116 } 117 } 118 } 119 } 120 121}