How to add a HTTP Provider programmatically when a VHost starts

It is possible to add a HTTP Provider when a VHost starts running. This method potentially removes any mistakes and also means only a small amount of configuration is needed in the Server.xml.

In order to add a HTTP Provider you need to write a VHost listener and also have a HTTP Class available, both are shown below

VHost Listener Class

package guru.thewowza.example.autoaddHTTPProvider;

import java.util.List;
import com.wowza.wms.amf.AMFDataList;
import com.wowza.wms.client.IClient;
import com.wowza.wms.http.IHTTPProvider;
import com.wowza.wms.http.IHTTPProvider2;
import com.wowza.wms.request.RequestFunction;
import com.wowza.wms.vhost.HostPort;
import com.wowza.wms.vhost.HostPortList;
import com.wowza.wms.vhost.IVHost;
import com.wowza.wms.vhost.IVHostNotify;

public class VHost implements IVHostNotify {

	public void onVHostClientConnect(IVHost vhost, IClient inClient,RequestFunction function, AMFDataList params) {}

	public void onVHostCreate(IVHost vhost) {}

	public void onVHostInit(IVHost vhost) 	{
		// We check if the VHost is _defaultVHost_ as we do not initialise
		// You could make it add to any VHost if you wish.
    	if ( vhost.getName().equals("_defaultVHost_") )	{
       		HostPortList PortList = vhost.getHostPortsList();
    		for (int ports=0;ports<PortList.size(); ports++ ) {
    			HostPort ThisPort = PortList.get(ports);
    			// If we find port 1935 then we add the HTTP Provider
    			// You could set any port you wish to add the provider to.
    			if ( ThisPort.getPort() == 1935 ) {
    				List  TheseProviders = ThisPort.getHttpProviders();
    				IHTTPProvider2 TestProvider = new HTTPExample();
    				TestProvider.setRequestFilters("httpexample*");
    				// Method is just a text string so can be anyone defined in the
    				// Authentication.xml, basic, none, admin-digest are the most common
    				TestProvider.setAuthenticationMethod("admin-digest");
    				TestProvider.canHandle("httpexample*");
    				// You can add to the beginning with
    				TheseProviders.add(0,TestProvider);
    				// Or add to the end with
    				// TheseProviders.add(TestProvider);
    				// You MUST call init otherwise the provider does not work
    				TestProvider.init();
				}
			}
	    	}
	} 

	public void onVHostShutdownComplete(IVHost vhost) {}

	public void onVHostShutdownStart(IVHost vhost) {}

}

HTTPExample Class

package guru.thewowza.example.autoaddHTTPProvider;

import java.io.*;
import com.wowza.wms.http.*;
import com.wowza.wms.logging.*;
import com.wowza.wms.vhost.*;

public class HTTPExample extends HTTProvider2Base {

	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		if (!doHTTPAuthentication(vhost, req, resp))
			return;

		// This example is straight from the template provided by Wowza within the
		// Exclipse plugin, except we changed the helloStr to something else

		String helloStr = "Hello from The Wowza Guru";
		String retStr = "" + helloStr
				+ "" + helloStr + "";

		try {
			OutputStream out = resp.getOutputStream();
			byte[] outBytes = retStr.getBytes();
			out.write(outBytes);
		} catch (Exception e) {
			WMSLoggerFactory.getLogger(null).error(
					"HTTPExample: " + e.toString());
		}
	}
}

To use this example you do need to add a VHostListener and the following is an example of the VHostListeners in a Server.xml

<VHostListeners>
	<VHostListener>
		<BaseClass>guru.thewowza.example.autoaddHTTPProvider.VHost</BaseClass>
	</VHostListener>
</VHostListeners>

Comments are closed.