Commit 30209c3b authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

[JM-1500] Partially reverted the previous adjustment. Now following both...

[JM-1500] Partially reverted the previous adjustment.  Now following both documentation on adobe's site -and- the "http style" request handling.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@10929 b35dd754-fafc-0310-a699-88a17e54d16e
parent efdb1618
......@@ -13,117 +13,153 @@
package org.jivesoftware.openfire;
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.http.FlashCrossDomainServlet;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.ServletHandler;
/**
* Sets up the "legacy" flash cross domain servlet, served off port 5229.
*
* @author Daniel Henninger
*
*/
import com.openbase.jdbc.i;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class FlashCrossDomainHandler extends BasicModule {
private ServerSocket serverSocket;
public static String CROSS_DOMAIN_TEXT = "<?xml version=\"1.0\"?>" +
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">" +
"<cross-domain-policy>" +
"<allow-access-from domain=\"*\" to-ports=\"";
private Server crossDomainServer;
private Connector crossDomainConnector;
private ContextHandlerCollection contexts;
private int servletPort = 5229;
public static String CROSS_DOMAIN_END_TEXT = "\" /></cross-domain-policy>";
public FlashCrossDomainHandler() {
super("Flash CrossDomain Handler");
// Configure Jetty logging to a more reasonable default.
System.setProperty("org.mortbay.log.class", "org.jivesoftware.util.log.util.JettyLog");
// JSP 2.0 uses commons-logging, so also override that implementation.
System.setProperty("org.apache.commons.logging.LogFactory", "org.jivesoftware.util.log.util.CommonsLogFactory");
contexts = new ContextHandlerCollection();
}
public Integer getPort() {
if (crossDomainConnector != null) {
return crossDomainConnector.getLocalPort();
}
else {
return null;
}
}
public void start() {
configureCrossDomainServer(servletPort);
Thread thread = new Thread(new Runnable() {
public void run() {
try {
startServer();
}
catch (Exception e) {
Log.error(e);
}
}
}, "Flash Cross Domain");
try {
crossDomainServer.start();
}
catch (Exception e) {
Log.error("Error starting cross domain service", e);
}
thread.start();
}
public void stop() {
if (crossDomainServer != null) {
try {
crossDomainServer.stop();
}
catch (Exception e) {
Log.error("Error stoping cross domain service", e);
try {
if (serverSocket != null) {
serverSocket.close();
}
}
}
private String getBindInterface() {
String interfaceName = JiveGlobals.getXMLProperty("network.interface");
String bindInterface = null;
if (interfaceName != null) {
if (interfaceName.trim().length() > 0) {
bindInterface = interfaceName;
}
catch (IOException e) {
Log.error(e);
}
return bindInterface;
}
private void createConnector(int port) {
crossDomainConnector = null;
if (port > 0) {
SelectChannelConnector connector = new SelectChannelConnector();
public int getPort() {
return serverSocket != null ? serverSocket.getLocalPort() : 0;
}
private void startServer() throws Exception {
try {
// Listen on a specific network interface if it has been set.
connector.setHost(getBindInterface());
connector.setPort(port);
crossDomainConnector = connector;
String interfaceName = JiveGlobals.getXMLProperty("network.interface");
InetAddress bindInterface = null;
int port = 5229;
if (interfaceName != null) {
if (interfaceName.trim().length() > 0) {
bindInterface = InetAddress.getByName(interfaceName);
}
}
serverSocket = new ServerSocket(port, -1, bindInterface);
Log.debug("Flash cross domain is listening on " + interfaceName + " on port " + port);
}
}
private synchronized void configureCrossDomainServer(int port) {
crossDomainServer = new Server();
createConnector(port);
if (crossDomainConnector == null) {
crossDomainServer = null;
catch (IOException e) {
Log.error("Could not listen on port: 5229.", e);
return;
}
else {
crossDomainServer.addConnector(crossDomainConnector);
}
createCrossDomainHandler(contexts, "/");
while (true) {
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
clientSocket = serverSocket.accept();
clientSocket.setSoTimeout(10000); // 10 second timeout
crossDomainServer.setHandlers(new Handler[]{contexts, new DefaultHandler()});
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String request = "";
request = read(in);
System.out.println("Request was "+request);
if (request.contains("<policy-file-request/>") || request.contains("GET /crossdomain.xml")) {
out.write(CROSS_DOMAIN_TEXT +
XMPPServer.getInstance().getConnectionManager().getClientListenerPort() +
CROSS_DOMAIN_END_TEXT+"\u0000");
}
}
catch (IOException e) {
if (XMPPServer.getInstance().isShuttingDown()) {
break;
}
Log.error(e);
}
finally {
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
if (clientSocket != null) {
clientSocket.close();
}
}
}
}
/**
* Safely read a string from the reader until a zero character or a newline is received o
r the 200 character is reached.
*
* @return the string read from the reader.
*/
protected String read(BufferedReader in) {
StringBuffer buffer = new StringBuffer();
int codePoint;
boolean zeroByteRead = false;
try {
do {
codePoint = in.read();
private void createCrossDomainHandler(ContextHandlerCollection contexts, String crossPath) {
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(FlashCrossDomainServlet.class, "/crossdomain.xml");
ContextHandler crossContextHandler = new ContextHandler(contexts, crossPath);
crossContextHandler.setHandler(handler);
if (codePoint == 0 || codePoint == '\n') {
zeroByteRead = true;
}
else if (Character.isValidCodePoint(codePoint)) {
buffer.appendCodePoint(codePoint);
}
}
while (!zeroByteRead && buffer.length() < 200);
}
catch (Exception e) {
Log.debug("Exception (read): " + e.getMessage());
}
return buffer.toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment