Verify a VOD asset exists via HTTP Provider

We’ve had recent requests for an http provider that will verify existence of a particular VOD asset. Below is the source code as you will need to compile this locally.

Example Requests:
“http://localhost:8086/verify?file=sample.mp4″
“http://localhost:8086/verify?file=subdirectory/sample.mp4″

Setup the HTTP Provider in your VHost.xml under 8086 host port:

<HTTPProvider>
<BaseClass>guru.thewowza.filehttpprovider.VerifyAsset</BaseClass>
<RequestFilters>verify*</RequestFilters>
<AuthenticationMethod>none</AuthenticationMethod>
</HTTPProvider>

 

package guru.thewowza.filehttpprovider;

import java.io.File; 
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wowza.util.HTTPUtils;
import com.wowza.util.URLUtils; 
import com.wowza.wms.application.IApplication;
import com.wowza.wms.application.IApplicationInstance;  
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.IVHost;
import com.wowza.wms.http.HTTProvider2Base;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;

public class VerifyAsset extends HTTProvider2Base { 
	private WMSLogger logger = WMSLoggerFactory.getLogger(getClass()); 

	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		String queryStr = req.getQueryString();
		queryStr = URLUtils.decodeValue(queryStr);
		Map<String, String> queryMap = HTTPUtils.splitQueryStr(queryStr); 
		if (queryMap.containsKey("application")) { 
			if(queryMap.containsKey("file")){ 
				String fileRelativePath = this.cleanFilePath(queryMap.get("file"));
				String applicationNameStr = queryMap.get("application");
				String applicationInstanceNameStr = (queryMap.containsKey("appInstance"))?queryMap.get("appInstance"):"_definst_";
				IApplication application = vhost.getApplication(applicationNameStr);
				if(application!=null && application instanceof IApplication){
					IApplicationInstance appInstance = application.getAppInstance(applicationInstanceNameStr);
					if(appInstance!=null && appInstance instanceof IApplicationInstance){
						String contentDir = appInstance.getStreamStorageDir(); 
						String targetFileStr = contentDir + "/"+fileRelativePath;

						File targetFile = new File(targetFileStr);
						if (targetFile.exists()) {

							logger.info("VerifyAsset["+targetFileStr+"] :: exists");
							this.printToBrowser(resp,"Exists",false,targetFileStr); 
						}
						else{
							logger.info("VerifyAsset["+targetFileStr+"] :: does not exists");
							this.printToBrowser(resp,"File does not exist.",true,targetFileStr); 
						} 
					}
					else{
						this.printToBrowser(resp,"Invalid app instance ...",true,fileRelativePath);
					}
				}
				else{
					this.printToBrowser(resp,"Invalid app ...",true,fileRelativePath);
				}
			}
			else{
				this.printToBrowser(resp,"Could not find file parameter...",true, null); 
			}
		}
		else{ 
			this.printToBrowser(resp,"Invalid base directory...",true, null); 
		}
	}

	private String cleanFilePath(String filePath){
		filePath = filePath.trim();
		if(filePath.startsWith("/")){
			filePath = filePath.substring(1);
		}
		return filePath;
	}

	public void printToBrowser(IHTTPResponse resp, String retStr, boolean isError, String contentPath){
		try { 
			HashMap<String,String> dataObj =  new HashMap<String,String>();
			dataObj.put("response",retStr);
			dataObj.put("file_path",contentPath);
			dataObj.put("success",((isError)?"0":"1"));

			ObjectMapper mapper = new ObjectMapper();
			OutputStream out = resp.getOutputStream();
			byte[] outBytes = mapper.writeValueAsString(dataObj).getBytes(); 
			out.write(outBytes);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Comments are closed.