Wowza Community

Simple static file serving HttpProvider

I have just created a simple http provider, maybe it will be useful for you. I’m running WMS on port 80 for rmtpt fallback, and on the same port i wanted to provide crossdomain.xml file for flash clients, so i created a simple provider that can serve this crossdomain.xml file. For other HTTP requests it will send a redirect to the main website, or if no redirect configured it will display a simple text message (like the original helloworld example :))

Configuration is like this in VHost.xml:

<HTTPProvider>
	<BaseClass>pd.http.SimpleStaticFile</BaseClass>
	<Properties>
		<!-- file mapping is url -> filename:content-type -->
		<Property>
			<Name>/crossdomain.xml</Name>
			<Value>/opt/wowza/webroot/crossdomain.xml:text/xml</Value>
		</Property>
		<!-- not mapped is redirected, if redirect is present -->
		<Property>
			<Name>redirect</Name>
			<Value>http://mymainwebsite.com</Value>
		</Property>
		<!-- if no redirect, message is shown, default can be overridden here -->
		<Property>
			<Name>message</Name>
			<Value>unauthorized access</Value>
		</Property>
	</Properties>
</HTTPProvider>

And the source code is like this (sorry, a little long):

package pd.http;
import java.io.FileInputStream;
import java.io.OutputStream;
import com.wowza.wms.application.WMSProperties;
import com.wowza.wms.http.IHTTPProvider;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.HostPort;
import com.wowza.wms.vhost.IVHost;
/**
 * Simple static file serving HTTP Provider with redirect/default message support
 * [Public Domain]
 */
public class SimpleStaticFile implements IHTTPProvider {
	protected static WMSLogger log = WMSLoggerFactory.getLogger(SimpleStaticFile.class);
	
	protected WMSProperties properties;
	
	@Override
	public void onBind(IVHost vhost, HostPort hostPort) {
		log.info("SimpleStaticFile HTTP Provider initialized on "+hostPort.getAddressStr()+":"+hostPort.getPort());
	}
	
	@Override
	public void onUnbind(IVHost vhost, HostPort hostPort) {
		log.info("SimpleStaticFile HTTP Provider stopped on "+hostPort.getAddressStr()+":"+hostPort.getPort());
	}
	@Override
	public void setProperties(WMSProperties properties) {
		this.properties = properties;
	}
	@Override
	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		String url = "/"+req.getRequestURL(); // req.getRequestURL does not contain /
		try {
			String fileoptions = properties.getPropertyStr(url);
			if (fileoptions != null) {
				// fileoptions format is filename:content-type
				String[] split = fileoptions.split(":");
				String filename = split[0];
				String contenttype = split[1];
				resp.setHeader("Content-Type", contenttype);
				FileInputStream in = new FileInputStream(filename);
				OutputStream out = resp.getOutputStream();
				byte[] buf = new byte[1024];
				int read;
				while ( (read = in.read(buf)) > 0 ) {
					out.write(buf, 0, read);
				}
			}
			else {
				// not set, try to fall back to redirect
				String redirect = properties.getPropertyStr("redirect");
				if (redirect != null) {
					// do redirect
					resp.setResponseCode(302); // 302: Found (for redirect)
					resp.setHeader("Location", redirect);
					String output = "<html><body><a href=\""+redirect+"\">"+redirect+"</a></body></html>";
					resp.getOutputStream().write(output.getBytes());
				}
				else {
					// fall back to message
					String message = properties.getPropertyStr("message", "unauthorized access");
					resp.setHeader("Content-Type", "text/plain");
					resp.getOutputStream().write(message.getBytes());
				}
			}
		}
		catch (Exception e) {
			// simplified exception handling, just log error
			log.error("Error handling request "+url, e);
		}
	}
	
}