Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
The MIT License Copyright (c) 2010 Tad Glines Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 package com.glines.socketio.server.transport;
 
 
 
 
 
 public class FlashSocketTransport extends WebSocketTransport {
 	public static final String TRANSPORT_NAME = "flashsocket";
 	public static final String FLASHPOLICY_SERVER_HOST_KEY = "flashPolicyServerHost";
 	public static final String FLASHPOLICY_SERVER_PORT_KEY = "flashPolicyServerPort";
 	public static final String FLASHPOLICY_DOMAIN_KEY = "flashPolicyDomain";
 	public static final String FLASHPOLICY_PORTS_KEY = "flashPolicyPorts";
 
 	private static final String FLASHFILE_NAME = "WebSocketMain.swf";
 	private static final String FLASHFILE_PATH =  + "/" + ;
 	private ExecutorService executor = Executors.newCachedThreadPool();
 	private Future<?> policyAcceptorThread = null;
 	private String flashPolicyServerHost = null;
 	private short flashPolicyServerPort = 843;
 	private String flashPolicyDomain = null;
 	private String flashPolicyPorts = null;
 
 
 	public FlashSocketTransport(int bufferSizeint maxIdleTime) {
 		super(bufferSizemaxIdleTime);
 	}
 
 	public String getName() {
 		return ;
 	}
 
 	public void init(ServletConfig config) {
 		if (port != null) {
 		}
 		if ( != null &&  != null &&  != null) {
 			try {
 			} catch (IOException e) {
 				// Ignore
 			}
 		}
 	}
 
 	public void destroy() {
 	}
 
	public void handle(HttpServletRequest request,
			Transport.InboundFactory inboundFactory,
			SocketIOSession.Factory sessionFactory)
			throws IOException {
		String path = request.getPathInfo();
    	if (path == null || path.length() == 0 || "/".equals(path)) {
    		response.sendError(."Invalid " +  + " transport request");
    		return;
    	}
    	if (path.startsWith("/")) path = path.substring(1);
    	String[] parts = path.split("/");
    	if ("GET".equals(request.getMethod()) && .equals(parts[0])) {
    		if (!.equals(path)) {
        		super.handle(requestresponseinboundFactorysessionFactory);
    		} else {
				response.setContentType("application/x-shockwave-flash");
				InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/glines/socketio/" + );
				OutputStream os = response.getOutputStream();
				try {
					IO.copy(isos);
catch (IOException e) {
					// TODO: Do we care?
				}
    		}
    	} else {
    		response.sendError(."Invalid " +  + " transport request");
    	}
	}

Starts this server, binding to the previously passed SocketAddress.
	public void startFlashPolicyServer() throws IOException {
		final String POLICY_FILE_REQUEST = "<policy-file-request/>";
		 = ServerSocketChannel.open();
		// Spawn a new server acceptor thread, which must accept incoming
		// connections indefinitely - until a ClosedChannelException is thrown.
			public void run() {
				try {
					while (true) {
						final SocketChannel serverSocket = .accept();
							public void run() {
								try {
									serverSocket.configureBlocking(true);
									Socket s = serverSocket.socket();
									StringBuilder request = new StringBuilder();
									int c;
									while ((c = in.read()) != 0 && request.length() <= POLICY_FILE_REQUEST.length()) {
										request.append((char)c);
									}
									if (request.toString().equalsIgnoreCase(POLICY_FILE_REQUEST) ||
										 != null &&  != null) {
										out.println("<cross-domain-policy><allow-access-from domain=\""++"\" to-ports=\""++"\" /></cross-domain-policy>");
										out.write(0);
										out.flush();
									}
									serverSocket.close();
catch (IOException e) {
									// TODO: Add loging
finally {
									try {
										serverSocket.close();
catch (IOException e) {
										// Ignore error on close.
									}
								}
							}
						});
					}
catch (ClosedChannelException e) {
					return;
catch (IOException e) {
					throw new IllegalStateException("Server should not throw a misunderstood IOException"e);
				}
			}
		});
	}
	private void stopFlashPolicyServer() {
		if ( != null) {
			try {
catch (IOException e) {
				// Ignore
			}
		}
		if ( != null) {
			try {
catch (InterruptedException e) {
				throw new IllegalStateException();
catch (ExecutionException e) {
				throw new IllegalStateException("Server thread threw an exception"e.getCause());
			}
				throw new IllegalStateException("Server acceptor thread has not stopped.");
			}
		}
	}
New to GrepCode? Check out our FAQ X