How to parse post or get variables in an HTTP Provider

HTTP Provider that parses post variables.

package com.wowza.forum.httpprovider;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import com.wowza.wms.application.WMSProperties;
import com.wowza.wms.http.HTTPServerVersion;
import com.wowza.wms.http.IHTTPProvider;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.HostPort;
import com.wowza.wms.vhost.IVHost;

public class HTTPProviderServerControls implements IHTTPProvider {

	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {

		StringBuffer ret = new StringBuffer();
		ret.append("<?xml version="1.0"?>
<WowzaMediaServer>");

		if (req.getMethod().equalsIgnoreCase("post")) {
			req.parseBodyForParams(true);
		}

		Map<String, List<String>> params = req.getParameterMap();
		if (params.containsKey("action")) {
			String action = params.get("action").get(0);
			ret.append("<Action>" + action + "</Action>");
			if (action.equalsIgnoreCase("doSomething")) {
				vhost.getApplication("live").getAppInstance("_definst_").startMediaCasterStream("myStream", "rtp");
			}
		}
		ret.append("</WowzaMediaServer>");
		try
		{
			resp.setHeader("Content-Type", "text/xml");

			OutputStream out = resp.getOutputStream();
			byte[] outBytes = ret.toString().getBytes();
			out.write(outBytes);
		}
		catch (Exception e)
		{
			WMSLoggerFactory.getLogger(HTTPServerVersion.class).error("HTTPProviderStreamReset.onHTTPRequest: "+e.toString());
			e.printStackTrace();
		}
	}

	public void onBind(IVHost vhost, HostPort hostPort) {
	}

	public void onUnbind(IVHost vhost, HostPort hostPort) {
	}

	public void setProperties(WMSProperties properties) {
	}

}
This one is more full-featured:
 
package com.wowza.forum.httpprovider;

import java.io.*;
import java.util.List;
import java.util.Map;

import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.http.*;
import com.wowza.wms.logging.*;
import com.wowza.wms.vhost.*;

public class HTTPIPCamManager extends HTTPProvider2Base {

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

		Map<String, List<String>> params = req.getParameterMap();

		String action = "";
		String app = "";
		String streamName = "";
		String mediacasterType = "";

		if (req.getMethod().equalsIgnoreCase("post")) {
			req.parseBodyForParams(true);
		}
		if (params.containsKey("action"))
			action = params.get("action").get(0);

		if (params.containsKey("app"))
			app = params.get("app").get(0);

		if (params.containsKey("streamName"))
			streamName = params.get("streamName").get(0);

		if (params.containsKey("mediacasterType"))
			mediacasterType = params.get("mediacasterType").get(0);

		IApplicationInstance appInstance = vhost.getApplication(app).getAppInstance("_definst_");

		String report = action + " " + streamName;

		try
		{
		if (action.equalsIgnoreCase("start"))
		{
			appInstance.startMediaCasterStream(streamName, mediacasterType);
		}
		else if (action.equalsIgnoreCase("stop"))
		{
			appInstance.stopMediaCasterStream(streamName);
		}
		}
		catch(Exception e)
		{
			report = e.getMessage();
		}

		String retStr = "<html><head><title>MediaCaster HTTP Provider</title></head><body>" + report + "</body></html>";

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

	}

}

Originally Published: 10-03-2010.

If you're having problems or want to discuss this article, post in our forum.