Fix adding models in the webinterface
This commit is contained in:
parent
4c408501bb
commit
fe7b263d2e
|
@ -1,4 +1,11 @@
|
||||||
package ctbrec.servlet;
|
package ctbrec.servlet;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -7,46 +14,47 @@ import java.net.URLConnection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
@Slf4j
|
||||||
import javax.servlet.http.HttpServlet;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
public class StaticFileServlet extends HttpServlet {
|
public class StaticFileServlet extends HttpServlet {
|
||||||
|
|
||||||
private String classPathRoot;
|
private final String classPathRoot;
|
||||||
private Map<String, String> mimetypes = new HashMap<>();
|
private final Map<String, String> mimetypes = new HashMap<>();
|
||||||
private boolean contextAware = true;
|
private final boolean contextAware;
|
||||||
|
|
||||||
public StaticFileServlet(String classPathRoot) {
|
public StaticFileServlet(String classPathRoot) {
|
||||||
|
this(classPathRoot, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StaticFileServlet(String classPathRoot, boolean contextAware) {
|
||||||
this.classPathRoot = classPathRoot;
|
this.classPathRoot = classPathRoot;
|
||||||
|
this.contextAware = contextAware;
|
||||||
mimetypes.put("css", "text/css");
|
mimetypes.put("css", "text/css");
|
||||||
mimetypes.put("js", "application/javascript");
|
mimetypes.put("js", "application/javascript");
|
||||||
}
|
}
|
||||||
|
|
||||||
public StaticFileServlet(String classPathRoot, boolean contextAware) {
|
|
||||||
this(classPathRoot);
|
|
||||||
this.contextAware = contextAware;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
|
||||||
|
try {
|
||||||
String request = req.getRequestURI();
|
String request = req.getRequestURI();
|
||||||
if (contextAware) {
|
if (contextAware) {
|
||||||
String contextPath = getServletContext().getContextPath();
|
String contextPath = getServletContext().getContextPath();
|
||||||
request = request.substring(contextPath.length());
|
request = request.substring(contextPath.length());
|
||||||
}
|
}
|
||||||
serveFile(request, resp);
|
serveFile(request, resp);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
req.getRequestDispatcher("/404").forward(req, resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
|
||||||
doGet(req, resp);
|
doGet(req, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void serveFile(String resource, HttpServletResponse resp) throws IOException {
|
private void serveFile(String resource, HttpServletResponse resp) throws IOException {
|
||||||
InputStream resourceAsStream = getClass().getResourceAsStream(classPathRoot + resource);
|
try (InputStream resourceAsStream = getClass().getResourceAsStream(classPathRoot + resource)) {
|
||||||
if (resourceAsStream == null) {
|
if (resourceAsStream == null) {
|
||||||
|
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||||
throw new FileNotFoundException();
|
throw new FileNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,18 +65,21 @@ public class StaticFileServlet extends HttpServlet {
|
||||||
resp.setContentType(mimetype);
|
resp.setContentType(mimetype);
|
||||||
resp.setStatus(HttpServletResponse.SC_OK);
|
resp.setStatus(HttpServletResponse.SC_OK);
|
||||||
OutputStream out = resp.getOutputStream();
|
OutputStream out = resp.getOutputStream();
|
||||||
int length = 0;
|
int length;
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
while ((length = resourceAsStream.read(buffer)) >= 0) {
|
while ((length = resourceAsStream.read(buffer)) >= 0) {
|
||||||
out.write(buffer, 0, length);
|
out.write(buffer, 0, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String guessMimeType(String resource) {
|
private String guessMimeType(String resource) {
|
||||||
try {
|
try {
|
||||||
String extension = resource.substring(resource.lastIndexOf('.') + 1);
|
String extension = resource.substring(resource.lastIndexOf('.') + 1);
|
||||||
return mimetypes.get(extension);
|
return mimetypes.get(extension);
|
||||||
} catch(Exception e) {}
|
} catch (Exception e) {
|
||||||
|
// fail silently
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -326,7 +326,7 @@ public class HttpServer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
|
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
|
||||||
if (code == 404) {
|
if (code == SC_NOT_FOUND) {
|
||||||
writer.write("<html><head><title>404</title><style>* {font-family: sans-serif}</style></head><body><h1>404</h1><p>Looking for <a href=\"" + contextPath + "/static/index.html\">CTB Recorder</a>?</p></body>");
|
writer.write("<html><head><title>404</title><style>* {font-family: sans-serif}</style></head><body><h1>404</h1><p>Looking for <a href=\"" + contextPath + "/static/index.html\">CTB Recorder</a>?</p></body>");
|
||||||
} else {
|
} else {
|
||||||
super.handleErrorPage(request, writer, code, message);
|
super.handleErrorPage(request, writer, code, message);
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import ctbrec.*;
|
import ctbrec.*;
|
||||||
import ctbrec.io.BandwidthMeter;
|
import ctbrec.io.BandwidthMeter;
|
||||||
import ctbrec.io.json.ObjectMapperFactory;
|
import ctbrec.io.json.ObjectMapperFactory;
|
||||||
|
import ctbrec.io.json.dto.ModelDto;
|
||||||
import ctbrec.io.json.mapper.ModelMapper;
|
import ctbrec.io.json.mapper.ModelMapper;
|
||||||
import ctbrec.io.json.mapper.RecordingMapper;
|
import ctbrec.io.json.mapper.RecordingMapper;
|
||||||
import ctbrec.recorder.Recorder;
|
import ctbrec.recorder.Recorder;
|
||||||
|
@ -59,15 +60,29 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("Request: {}", json);
|
log.debug("Request: {}", json);
|
||||||
|
RequestDto requestDto = mapper.readValue(json, RequestDto.class);
|
||||||
|
ModelDto modelDto = requestDto.getModel();
|
||||||
|
if (requestDto.getAction() == null) {
|
||||||
|
sendError(resp, SC_BAD_REQUEST, "{\"status\": \"error\", \"msg\": \"action is missing\"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (requestDto.getAction().startsWith("startBy")) {
|
||||||
|
log.debug("Starting recording for model {}", modelDto.getUrl());
|
||||||
|
if (requestDto.getAction().equals("startByUrl")) {
|
||||||
|
startByUrl(requestDto);
|
||||||
|
} else {
|
||||||
|
startByName(requestDto);
|
||||||
|
}
|
||||||
|
var response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||||
|
responseWriter.write(response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Request request = Mappers.getMapper(RequestMapper.class).toRequest(mapper.readValue(json, RequestDto.class));
|
Request request = Mappers.getMapper(RequestMapper.class).toRequest(mapper.readValue(json, RequestDto.class));
|
||||||
Model model = request.getModel();
|
Model model = request.getModel();
|
||||||
Optional.ofNullable(model).ifPresent(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
Optional.ofNullable(model).ifPresent(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
||||||
Recording recording = request.getRecording();
|
Recording recording = request.getRecording();
|
||||||
if (request.getAction() == null) {
|
switch (requestDto.getAction()) {
|
||||||
sendError(resp, SC_BAD_REQUEST, "{\"status\": \"error\", \"msg\": \"action is missing\"}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (request.getAction()) {
|
|
||||||
case "start":
|
case "start":
|
||||||
log.debug("Starting recording for model {} - {}", model.getName(), model.getUrl());
|
log.debug("Starting recording for model {} - {}", model.getName(), model.getUrl());
|
||||||
log.trace("Model marked: {}", model.isMarkedForLaterRecording());
|
log.trace("Model marked: {}", model.isMarkedForLaterRecording());
|
||||||
|
@ -78,18 +93,6 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
||||||
String response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
String response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||||
responseWriter.write(response);
|
responseWriter.write(response);
|
||||||
break;
|
break;
|
||||||
case "startByUrl":
|
|
||||||
log.debug("Starting recording for model {}", model.getUrl());
|
|
||||||
startByUrl(request);
|
|
||||||
response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
|
||||||
responseWriter.write(response);
|
|
||||||
break;
|
|
||||||
case "startByName":
|
|
||||||
log.debug("Starting recording for model {}", model.getUrl());
|
|
||||||
startByName(request);
|
|
||||||
response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
|
||||||
responseWriter.write(response);
|
|
||||||
break;
|
|
||||||
case "stop":
|
case "stop":
|
||||||
GlobalThreadPool.submit(() -> {
|
GlobalThreadPool.submit(() -> {
|
||||||
try {
|
try {
|
||||||
|
@ -276,8 +279,8 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
||||||
resp.getWriter().write(jsonResponse.toString());
|
resp.getWriter().write(jsonResponse.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startByUrl(Request request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
private void startByUrl(RequestDto request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||||
String url = request.getModel().getUrl();
|
String url = request.getModel().getUrl().toString();
|
||||||
for (Site site : sites) {
|
for (Site site : sites) {
|
||||||
Model model = site.createModelFromUrl(url);
|
Model model = site.createModelFromUrl(url);
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
|
@ -288,8 +291,8 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
||||||
throw new IllegalArgumentException("No site found to record " + url);
|
throw new IllegalArgumentException("No site found to record " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startByName(Request request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
private void startByName(RequestDto request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||||
String[] input = request.getModel().getUrl().split(":");
|
String[] input = request.getModel().getUrl().toString().split(":");
|
||||||
if (input.length != 2) {
|
if (input.length != 2) {
|
||||||
throw new IllegalArgumentException("Invalid input. Should be site:model");
|
throw new IllegalArgumentException("Invalid input. Should be site:model");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue