All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.automation.remarks.remote.node.VideoServlet Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
package com.automation.remarks.remote.node;

import com.automation.remarks.video.recorder.monte.MonteRecorder;
import org.apache.http.HttpStatus;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

import static com.automation.remarks.remote.utils.RestUtils.updateResponse;
import static com.automation.remarks.video.RecordingUtils.doVideoProcessing;

/**
 * Created by Serhii_Pirohov on 10.05.2016.
 */
public class VideoServlet extends HttpServlet {

    private MonteRecorder videoRecorder;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req, resp);
    }

    private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path = req.getPathInfo();
        MonteRecorder.conf().withDefaultFolder();
        try {
            switch (path) {
                case "/start":
                    String folder = req.getParameter("folder");
                    if(folder != null){
                        MonteRecorder.conf().withVideoFolder(folder);
                    }
                    videoRecorder = new MonteRecorder();
                    videoRecorder.start();
                    updateResponse(resp, HttpStatus.SC_OK, "recording started");
                    break;
                case "/stop":
                    if (videoRecorder == null) {
                        updateResponse(resp, HttpStatus.SC_METHOD_NOT_ALLOWED, "Wrong Action! First, start recording");
                        break;
                    }
                    String fileName = getFileName(req);
                    File video = videoRecorder.stopAndSave(fileName);
                    String filePath = doVideoProcessing(isSuccess(req), video);
                    updateResponse(resp, HttpStatus.SC_OK, "recording stopped " + filePath);
                    break;
            }
        } catch (Exception ex) {
            updateResponse(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR,
                    "Internal server error occurred while trying to start / stop recording: " + ex);
        }
    }

    private String getFileName(HttpServletRequest req) {
        String name = req.getParameter("name");
        if (name == null || "null".equalsIgnoreCase(name)) {
            return "video";
        }
        return name;
    }

    private boolean isSuccess(HttpServletRequest req) {
        String result = req.getParameter("result");
        if (result == null) {
            return false;
        }
        return Boolean.valueOf(result);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy