package com.mooo.aimmac23.hub.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.exec.StreamPumper;
import org.apache.http.HttpStatus;
import org.openqa.grid.internal.ExternalSessionKey;

import com.mooo.aimmac23.hub.HubVideoRegistry;

public class HubVideoDownloadServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String sessionId = req.getParameter("sessionId");
		
		if(sessionId == null) {
			resp.setStatus(HttpStatus.SC_BAD_REQUEST);
			resp.getWriter().write("Missing parameter: 'sessionId'");
			return;
		}
		
		File video = HubVideoRegistry.getVideoForSession(new ExternalSessionKey(sessionId));
		
		if(video == null || !video.exists()) {
			resp.setStatus(HttpStatus.SC_NO_CONTENT);
			resp.getWriter().write("Video content not found for sessionId: " + sessionId);
			return;
		}
		
		FileInputStream fileStream = new FileInputStream(video);
		try {
			resp.setContentType("video/mp4");
			new StreamPumper(fileStream, resp.getOutputStream()).run();
			return;
		}
		finally {
			fileStream.close();
		}
	}
}

@@ -1292,16 +1292,63 @@
 /mp4%22);%0A
+%09%09%09resp.setContentLength((int)video.length());%0A
 %09%09%09new S
@@ -1459,10 +1459,608 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09@Override%0A%09protected void doHead(HttpServletRequest req, HttpServletResponse resp)%0A%09%09%09throws ServletException, IOException %7B%0A%09%09String sessionId = req.getParameter(%22sessionId%22);%0A%09%09%0A%09%09if(sessionId == null) %7B%0A%09%09%09resp.setStatus(HttpStatus.SC_BAD_REQUEST);%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09File video = HubVideoRegistry.getVideoForSession(new ExternalSessionKey(sessionId));%0A%09%09%0A%09%09if(video == null %7C%7C !video.exists()) %7B%0A%09%09%09resp.setStatus(HttpStatus.SC_NO_CONTENT);%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09resp.setStatus(HttpStatus.SC_OK);%0A%09%09resp.setContentType(%22video/mp4%22);%0A%09%09resp.setContentLength((int)video.length());%0A%09%09return;%0A%09%7D%0A
 %7D%0A

Implement HTTP Head; Also make the content-length header give the size of the video